Hello, I have recently put a "coin system" into my game. This system will save your coins when you leave the game. I am not that advanced at coding so I am following the youtuber "AlvinBlox" for a tutorial. The entire script seems to be working except one tiny part. Here is said script:
game.Players.PlayerRemoving:Connect(function(player) local ID = CurrencyName..":"..player.UserId DataStore:SetAsync(ID.player.leaderstats.CurrencyName.Value) print("Data Saved") end)
When I leave the game, it says that the player is nil. I suspect that it is because the player is not in the game anymore so the server can't save the progress (I am not sure though). I am simply looking for a fix for this glitch.
game.Players.PlayerRemoving:Connect(function(player) local ID = CurrencyName..":"..player.UserId DataStore:SetAsync(ID.player.leaderstats.CurrencyName.Value) print("Data Saved") end)
If you look closely at line 3 it says ID.player it really should be ID, player so this would be the script you would use
game.Players.PlayerRemoving:Connect(function(player) local ID = CurrencyName..":"..player.UserId DataStore:SetAsync(ID, player.leaderstats.CurrencyName.Value) print("Data Saved") end)
Nvm I looked at the tutorial again and I had made a mistake.