How would I make tools save when you leave the game

I’m trying to make a script that saves a player tool in the default inventory when they leave and return when they rejoin. it also have to remove the items on death still.

I tried using a data save method but I have so many issues with it that I dropped it.

Any ideas?

1 Like

save them when the player leaves and put them back into their backpack when they spawn for the first time:

local ds = game:GetService("DataStoreService")
local toolds = ds:GetDataStore("Tools")

game.Players.PlayerAdded:Connect(function(plr)
	local alreadygottools = false
	plr.CharacterAdded:Connect(function()
		if alreadygottools then
			return
		end
		local tools = toolds:GetAsync(plr.UserId) or {}
		for i,v in pairs(tools) do
			local toolfolder = game.ReplicatedStorage --wherever the tools are stored
			local tool = toolfolder[v]:Clone()
			tool.Parent = plr.Backpack
		end
		alreadygottools = true
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local tools = {}
	for i,v in pairs(plr.Backpack) do
		table.insert(tools, v.Name)
	end
	toolds:SetAsync(plr.UserId, tools)
end)
3 Likes

Thing is, you have to use DataStores if you want to properly save Player’s Data but you cannot save a Tool Instance so you’ll have to refer to a wrap-around instead (Converting the Tool Name into a string)

You could call Set/Remove/Get Async to obtain the specific Player’s Data, then check if the Player’s Data has a specific tool name or not

local DataStore = game:GetService("DataStoreService")
local CurrentData = DataStore:GetDataStore("DataNameHere")

game.Players.PlayerAdded:Connect(function(Player)
    local Data
    local success, whoops = pcall(function()
        Data = CurrentData:GetAsync(Player.UserId)
    end)

    if success and Data then

        for _, Tool in pairs(Data) do
            local ToolCheck = game.ServerStorage:FindFirstChild(Tool)
            if ToolCheck then
                local ToolClone = ToolCheck:Clone()
                ToolClone.Parent = Player.Backpack
            end
        end
   
    end

end)

game.Players.PlayerRemoving:Connect(function(Player)
    local ToolsToSave = {}
    for _, Tool in pairs(Player.Backpack:GetChildren()) do
        table.insert(ToolsToSave, Tool.Name)
    end

    local success, whoops = pcall(function()
        CurrentData:SetAsync(Player.UserId, ToolsToSave)
    end)

    if success then
        print("Data saved")
    else
        warn(whoops)
    end
end)

You’ll have to do something relevant along the lines of this

6 Likes

I did something like this it worked but tools still remains in the player inventory when they die or respawn.

Could we see the current script that you tried to use?

I have deleted it long before I made this post out of frustration

1 Like