How do I add game:BindToClose to my script?

So I have this ‘leaderstats’ script in ServerScriptService (not a local script):

local DataStoreService = game:GetService("DataStoreService")
local KoinsStore = DataStoreService:GetDataStore("CoinsStore")

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder") --making leaderstats
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Koins = Instance.new("IntValue") --making coins
	Koins.Name = "Koins"
	Koins.Parent = leaderstats



	local UserId = player.UserId

	local data

	local success, errormessage = pcall(function() --getting loaded data
		data = KoinsStore:GetAsync(UserId)
	end)

	if success then
		Koins.Value = data
	end
end)



game.Players.PlayerRemoving:Connect(function(player)
	local UserId = player.UserId
	local data = player.leaderstats.Koins.Value

	KoinsStore:SetAsync(UserId, data)
end)

Now the issue is, I saw someone saying something about adding game:BindToClose to the script so that if the server suddenly shutdowns or if the player loses connection/gets kicked suddenly, they don’t lose their save of Koins (leaderstats), but I have no idea how to do that, how would I edit my script to make that work?

It would be that in a for loop of the players, like this:

game:BindToClose(function()
    for _, player in pairs(game.Players:GetPlayers()) do
        local UserId = player.UserId
	    local data = player.leaderstats.Koins.Value

	    KoinsStore:SetAsync(UserId, data)
    end
end)
1 Like

A follow up, recommended to use a pcall (protected call) when working with data stores;

game:BindToClose(function()
    for _, player in pairs(game.Players:GetPlayers()) do
        local UserId = player.UserId
	    local data = player.leaderstats.Koins.Value
        local success, err = pcall(function()
	      return  KoinsStore:SetAsync(UserId, data)
        end)
        if not success then
             warn("Something went wrong, maybe try again")
        end 
    end
end)
1 Like

Thank you so much! One more question if you don’t mind, it’s giving me an error at this line, how do I fix this issue?
Screenshot_13

Oh my bad, add a ) to the end that is right above it, so:

game:BindToClose(function()
    for _, player in pairs(game.Players:GetPlayers()) do
        local UserId = player.UserId
	    local data = player.leaderstats.Koins.Value
        local success, err = pcall(function()
	      return  KoinsStore:SetAsync(UserId, data)
        end)
        if not success then
             warn("Something went wrong, maybe try again")
        end 
    end
end)

I’ll edit my previous reply as well

1 Like

Thank you very much!! I’ll also post the full edited script here in case someone needs it in the future:

local DataStoreService = game:GetService("DataStoreService")
local KoinsStore = DataStoreService:GetDataStore("CoinsStore")

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder") --making leaderstats
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Koins = Instance.new("IntValue") --making coins
	Koins.Name = "Koins"
	Koins.Parent = leaderstats



	local UserId = player.UserId

	local data

	local success, errormessage = pcall(function() --getting loaded data
		data = KoinsStore:GetAsync(UserId)
	end)

	if success then
		Koins.Value = data
	end
end)



game.Players.PlayerRemoving:Connect(function(player)
	game:BindToClose(function()
		for _, player in pairs(game.Players:GetPlayers()) do
			local UserId = player.UserId
			local data = player.leaderstats.Koins.Value
			local success, err = pcall(function()
				return  KoinsStore:SetAsync(UserId, data)
			end)
			if not success then
				warn("Something went wrong, maybe try again")
			end 
		end
	end)
end)

Do NOT put the BindToClose in the player removing function, those should be 2 separate things. Full script:

local DataStoreService = game:GetService("DataStoreService")
local KoinsStore = DataStoreService:GetDataStore("CoinsStore")

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder") --making leaderstats
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Koins = Instance.new("IntValue") --making coins
	Koins.Name = "Koins"
	Koins.Parent = leaderstats



	local UserId = player.UserId

	local data

	local success, errormessage = pcall(function() --getting loaded data
		data = KoinsStore:GetAsync(UserId)
	end)

	if success then
		Koins.Value = data
	end
end)



game.Players.PlayerRemoving:Connect(function(player)
	local UserId = player.UserId
	local data = player.leaderstats.Koins.Value
	local success, err = pcall(function()
		return  KoinsStore:SetAsync(UserId, data)
	end)
	if not success then
		warn("Something went wrong, maybe try again")
	end 
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local UserId = player.UserId
		local data = player.leaderstats.Koins.Value
		local success, err = pcall(function()
			return  KoinsStore:SetAsync(UserId, data)
		end)
		if not success then
			warn("Something went wrong, maybe try again")
		end 
	end
end)
1 Like

Ah, I apologize for that mistake, thank you so much for letting me know!