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

Tools datastore not saving upon PlayerRemoving?

Hello, so I've been trying to save upon playerremoving but it never wants to work. I even used BindToClose() and I know it works 30 seconds before shutdown they said. But when I leave, the tool wont save.

This is currently my code right now:

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("BackpackSave")
game.Players.PlayerAdded:Connect(function(player)
	pcall(function()
		local tool = dataStore:GetAsync("User-"..player.UserId)
		if tool then
			for i,v in pairs(tool) do
				local toolFound = game.ReplicatedStorage.Items:FindFirstChild(v)
				if toolFound  then
					toolFound:Clone().Parent = player.Backpack
				end
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	pcall(function()
	local toolsSave = {}
	
		for i, tool in pairs (player.Backpack:GetChildren()) do
			if tool then
				table.insert(toolsSave,tool.Name)
			end
		end
		dataStore:SetAsync("User-"..player.UserId,toolsSave)
end)
end)

game:BindToClose(function()
	

	for _, player in pairs(game.Players:GetPlayers()) do -- notice that you can loop through every player left in the server so the function can run for them
		local toolsSave = {}
		for i, tool in pairs (player.Backpack:GetChildren()) do
			if tool then
				table.insert(toolsSave,tool.Name)
			end
		end
		dataStore:SetAsync("User-"..player.UserId,toolsSave)
	end
end)

Any help is appreciated...

1 comment

0

2 answers

✓ Accepted answer

your character is probably spawning after you parent the tool to the backpack, meaning that your backpack gets reset

try parenting a clone of the tool to both player.Backpack and player.StarterGear instead of just player.Backpack

It worked fine for me. Are you sure you have API Services enabled? (Game Settings -> Security -> Enable Studio Access to API Services)? Make sure to restart your studio after you save.

5 comments

0

Apparently, I get "22:58:38.509 - DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = User-28814494" WARN, which I'm aware it happens. But I feel like there's something sketchy in my code.

0

So its not working, and yes I enabled API services.

0

did you press save and restart studio?

0

Apparently, it just rarely works. Any ways to fix this?

0

I would recommend making it also auto-save every few seconds since data stores fail saving on player removed especially when there's only one player in the server

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