Hello developers, I'm unsure how to save data in :BindToClose(). I've tried ways such as:
game:BindToClose(function() local Player = game:GetService("Players"):GetPlayers() local playerid = "Player_"..Player.UserId local data = Player.leaderstats.deaths.Value local success, errormessage = pcall(function() HisDataStore:SetAsync(playerid, data) end) if success then print("Data has been successfully saved!") else print("Data has not been saved, negative!") warn(errormessage) end end)
I've tried this way but doesn't work. Gives the error 'attempt to concatenate string with nil' on the line local playerid = "Player_"..Player.UserId
I can see the mistake but I am rather confused how to fix it.
I found out the answer. I used a for loop to iterate the players.
Code:
game:BindToClose(function() local Player = game:GetService("Players") for _, players in pairs(Player:GetPlayers()) do local playerid = "Player_"..players.UserId local data = players.leaderstats.deaths.Value local success, errormessage = pcall(function() HisDataStore:SetAsync(playerid, data) end) if success then print("Data has been successfully saved in studio!") else print("Data has not been saved, negative!") warn(errormessage) end end end)