Script just stops without any error message in the output

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 WaitForChildat 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.

1 Like

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
1 Like

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)”