Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

THE SITUATION

I am attempting a simple task of updating a player's all-time win totals. Here is the code I used based on tutorials I have watched:

 local DataStoreService = game:GetService("DataStoreService")
 local myDataStore = DataStoreService:GetDataStore("myDataStore")

 game.Players.PlayerAdded:Connect(function(player)
     local leaderstats = Instance.new("Folder")
     leaderstats.Name = "leaderstats"
     leaderstats.Parent = player

     local wins = Instance.new("IntValue")
     wins.Name = "Wins"
     wins.Parent = leaderstats
    
     local data
     local success, errormessage = pcall(function()
     data = myDataStore:GetAsync(player.UserId.."-wins")
     end)

     if success then
         wins.Value = data
     else
         print("There was an error while getting your data")
         warn(errormessage)
     end
 end)

 game.Players.PlayerRemoving:Connect(function(player)

     local success, errormessage = pcall(function()
         myDataStore:SetAsync(player.UserId.."-wins",player.leaderstats.Wins.Value)
     end)

     if success then
         print("Player Data successfully saved")
     else
         print("There was an error when saving data")
         warn(errormessage)
     end
 end)

When a player wins a round in my game, I have been able to identify the winner via a variable called 'winning player.' So the command I have tried to update the player's win total is as follows:

 game.Players.winningplayer.leaderstats.Wins.Value = game.Players.winningplayer.leaderstats.Wins.Value + 1

THE PROBLEM

Unfortunately when this happens I get an error saying: winningplayer is not a valid member of Player "Players"

Would really love help with this. Many thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
264 views
Welcome To Ask or Share your Answers For Others

1 Answer

When you use the line game.Players.winningplayer that doesn't unpack the variable to properly find the player.

Try using square brackets instead of a period.

local winner = game.Players[winningplayer]
local wins = winner.leaderstats.Wins
wins.Value = wins.Value + 1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...