I need this datastore to save when the players teleports to a new place in the game but it only saves when the player leaves the whole game and there are barely any resources on this subject can somebody please help me

local ds = game:GetService("DataStoreService"):GetDataStore("ToolSave")
game.Players.PlayerAdded:connect(function(plr)
	local key = "id-"..plr.userId
	pcall(function()
		local tools = ds:GetAsync(key)
		if tools then
			for i,v in pairs(tools) do
				local tool = game.ServerStorage.Tools:FindFirstChild(v)					
				if tool then
					tool:Clone().Parent = plr:WaitForChild("Backpack")							
					tool:Clone().Parent = plr:WaitForChild("StarterGear")
					
						
				end
			end
		end
	end)
end)
game.Players.PlayerRemoving:connect(function(plr)
	local key = "id-"..plr.userId
	pcall(function()
		local toolsToSave = {}
		for i,v in pairs(plr.Backpack:GetChildren()) do
			if v then
				table.insert(toolsToSave,v.Name)
			end
		end
	ds:SetAsync(key,toolsToSave)
 end)
end)

this is in serverstorage

2 comments

0

but player added is the first function is there a function for like when players teleport to different places in the same game?

0

Do you want if they leave