Here is my code below. This code saves a string value on a gui when the player leaves and then loads in the saved string value onto the gui when the player rejoins the game. For some reason, when the player leaves, the string value is not being saved. I tried adding a print line that prints the value saved in the datastore when I rejoined the game, but it just printed nil. Any ideas?
Hello. This happens because the server shuts down when the last player leaves. PlayerRemoving event is not fired.
You should use game:BindToClose to solve this. I’ll link a post explaining that in detail.
It’s still not working. Any other reasons you could think of?
(btw I put the script into a bindtoclose function)
game:BindToClose(function()
-- When game is ready to shutdown
for i, player in pairs(game.Players:GetPlayers()) do
if player then
game.ReplicatedStorage.Events.PlayerisLeaving:FireClient(player)
game.ReplicatedStorage.Events.PlayerisLeavingFireback.OnServerEvent:Connect(function(player, textstring)
CoinQuestDatastore:SetAsync(player.UserId, textstring)
end)
player:Kick("This game is shutting down")
end
end
wait(5)
end)
Also, using RemoteEvent calls on PlayerRemoving or BindToClose will just stop the script at that point. It doesn’t work because the player upon whom the RemoteEvent is called has already been removed from the game.
I’m pretty sure this isn’t true. My old system in Restrictly used a StringValue to save whether the Player was banned or not, before I moved on to booleans. I also saved the reason for the ban.