Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Correct place to add a BindToClose()?

Ive been having trouble adding a BindToClose() function to this script. Thanks for your help

local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("PlayerSpeed")

game.Players.PlayerRemoving:Connect(function(player)
	local p = player:FindFirstChild("WalkspeedShop")
	if p then
		local savedata = {}
		for i, v in pairs(p:GetChildren()) do
			savedata[v.Name] = v.Value
		end
		SaveDataStore:SetAsync(player.UserId, savedata)
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local WalkspeedShop = Instance.new("Folder")
	WalkspeedShop.Name = "WalkspeedShop"
	WalkspeedShop.Parent = player
	
	local W20 = Instance.new("BoolValue")
	W20.Name = "W20"
	W20.Parent = WalkspeedShop
	
	local W23 = Instance.new("BoolValue")
	W23.Name = "W23"
	W23.Parent = WalkspeedShop
	
	local W25 = Instance.new("BoolValue")
	W25.Name = "W25"
	W25.Parent = WalkspeedShop
	
	local W26 = Instance.new("BoolValue")
	W26.Name = "W26"
	W26.Parent = WalkspeedShop
	
	local W27 = Instance.new("BoolValue")
	W27.Name = "W27"
	W27.Parent = WalkspeedShop
	
	local W28 = Instance.new("BoolValue")
	W28.Name = "W28"
	W28.Parent = WalkspeedShop
	
	local W29 = Instance.new("BoolValue")
	W29.Name = "W29"
	W29.Parent = WalkspeedShop
	
	local W30 = Instance.new("BoolValue")
	W30.Name = "W30"
	W30.Parent = WalkspeedShop
	
	local data = SaveDataStore:GetAsync(player.UserId)
	if data then
		print("Data loaded")
		for i,v in pairs(WalkspeedShop:GetChildren()) do
			v.Value = data[v.Name]
		end
	end
	while wait(240)do
		local p = player:FindFirstChild("WalkspeedShop")
	if p then
		local savedata = {}
		for i, v in pairs(p:GetChildren()) do
			savedata[v.Name] = v.Value
		end
		SaveDataStore:UpdateAsync(player.UserId, savedata)
	end
	end
end)


2 answers

Put the game:BindToClose() outside of the PlayerAdded and PlayerRemoving. The main reason why is because if you put in PlayerAdded, it won't save your data, and if you put it in PlayerRemoving, it also won't save your data because PlayerRemoving doesn't save the last player. According to the wiki (I'll put the sources below), game:BindToClose operates all functions for 30 seconds before the game fully shuts down.

game:BindToClose(function()
	for _, v in pairs(game.Players:GetPlayers()) do
		local userId = v.UserId
		local saveData = {} --This is your data.
		
		local success, errorMessage = pcall(function()
			SaveDataStore:UpdateAsync(userId, saveData)
		end)
		if success then
			print("Data saved and game closed")
		else
			warn("Error while saving data: "..errorMessage)
		end
	end
end)

Source: DataModel:BindToClose()

2 comments

0

I added your code and it works, but i get this message? DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 238723381

0

Use only 2 SetAsyncs (one for player removing and other for game:BindToClose() and one GetAsync. Don't attempt to use more.

Place it anywhere as long as it is not inside another function. I looked through your script as well and spotted a few errors, but I left comments for some of them as the others maybe were just simple mistakes.

local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("PlayerSpeed")

game.Players.PlayerRemoving:Connect(function(player)
	local p = player:FindFirstChild("WalkspeedShop")
	if p then
		local savedata = {}
		for i, v in pairs(p:GetChildren()) do
            		savedata[v.Name] = v.Value
        	end
        	SaveDataStore:SetAsync(player.UserId, savedata)
    	end
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local p = player:FindFirstChild("WalkspeedShop")
		if p then
			local savedata = {}
        		for i, v in pairs(p:GetChildren()) do
            			savedata[v.Name] = v.Value
        		end
        	SaveDataStore:SetAsync(player.UserId, savedata)
    	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local WalkspeedShop = Instance.new("Folder")
	WalkspeedShop.Name = "WalkspeedShop"
	WalkspeedShop.Parent = player

	local W20 = Instance.new("BoolValue")
	W20.Name = "W20"
	W20.Parent = WalkspeedShop

    	local W23 = Instance.new("BoolValue")
    	W23.Name = "W23"
    	W23.Parent = WalkspeedShop

    	local W25 = Instance.new("BoolValue")
    	W25.Name = "W25"
    	W25.Parent = WalkspeedShop

    	local W26 = Instance.new("BoolValue")
    	W26.Name = "W26"
    	W26.Parent = WalkspeedShop

    	local W27 = Instance.new("BoolValue")
    	W27.Name = "W27"
    	W27.Parent = WalkspeedShop

    	local W28 = Instance.new("BoolValue")
    	W28.Name = "W28"
    	W28.Parent = WalkspeedShop

    	local W29 = Instance.new("BoolValue")
    	W29.Name = "W29"
    	W29.Parent = WalkspeedShop

    	local W30 = Instance.new("BoolValue")
    	W30.Name = "W30"
    	W30.Parent = WalkspeedShop

    	local data = SaveDataStore:GetAsync(player.UserId)
    	if data then
        	print("Data loaded")
        	for i,v in pairs(WalkspeedShop:GetChildren()) do
            		v.Value = data[v.Name]
        	end
    	end
    
	while wait(240) do
        	local p = player:FindFirstChild("WalkspeedShop")
    		if p then
        		local savedata = {}
        		for i, v in pairs(p:GetChildren()) do
            			savedata[v.Name] = v.Value
        		end

        		SaveDataStore:UpdateAsync(player.UserId, function(savedata) -- this is how you use UpdateAsync
				return savedata
    			end)
    		end
	end -- I think you were missing this end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.