You might need to add a BindToClose
event, as so:
game:BindToClose(function()
for i, v in pairs(game.Players:GetPlayers()) do
playerData.LoadPlayerData(v)
end
end)
Keep the PlayerRemoving
event as well.
You might need to add a BindToClose
event, as so:
game:BindToClose(function()
for i, v in pairs(game.Players:GetPlayers()) do
playerData.LoadPlayerData(v)
end
end)
Keep the PlayerRemoving
event as well.
As @Fondatix says, you may need a BindToClose event, and in that case would need to disconnect the PlayerRemoving
event to prevent overwriting the data twice.
local Connection = game:GetService("Players").PlayerRemoving:Connect(function(player)
playerData.LoadPlayerData(player)
end)
game:BindToClose(function()
Connection:Disconnect()
for i, v in pairs(game:GetService("Players"):GetPlayers()) do
spawn(playerData.LoadPlayerData, v)
end
wait(30)
end)
Can you try printing something under where you defined âDATATOSAVEâ? Your code may be yielding here, also you shouldnât need to use WaitForChild on the server.
Whatâs wrong with using WaitForChild
on the server?
Firstly, if he already defined and set up the folder on the Get
call, he shouldnât need to use WaitForChild
at all. Secondly, its crucial not to yield while Setting data as you may need every second, or it may not set the data on time as OP is reporting. This is why I personally use as little amount of objects as possible.
It will yield if the item doesnât exist, also since the server has already loaded the game WaitForChild isnât needed.
Did you mean to put âLoadPlayerDataâ or âSavePlayerDataâ? I assume âSavePlayerDataâ Also, my studio just freezes when I leave the game.
I can confirm it is not yielding at this point.
Thatâs what BindToClose essentially does; its not usually used in studio, better to comment that out or set a condition.
if not game:GetService("RunService"):IsStudio() then
game:BindToClose(function()
Connection:Disconnect()
for i, v in pairs(game:GetService("Players"):GetPlayers()) do
spawn(playerData.LoadPlayerData, v)
end
wait(30)
end)
end
Is there a way to test if my script works then? I donât think I can insert IntValues in-gameâŚ
Oh, and do you mean âLoadPlayerDataâ or âSavePlayerDataâ?
I began getting this error when I the BindToClose : âServerScriptService.PlayerData:135: attempt to index local âplayerâ (a number value)â