Help with tool saving script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I need the player to save their tools on leave and lose them on death.

  1. What is the issue? Include screenshots / videos if possible!

When I reset/die, the tool losing function works fine, but if I leave then joined again I got them back.

Video:

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried replacing StarterGear in the savedata() function with backpack, but if they leave while having a tool equipped it wont save that tool, and I can’t save the tool in the Character since the Character is nil when a player leaves.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local toolstorage = game.ReplicatedStorage.Guns
local blacklist = {"Fists"}

local data = game:GetService("DataStoreService"):GetDataStore("Player_Tools")
local players = game.Players

local function savedata(plr, bool)
	local a = {}
	for _, v in pairs(plr.StarterGear:GetChildren()) do
		if bool == true then
			if not table.find(blacklist, v.Name) then
				table.insert(a, v.Name)
				data:SetAsync(plr.userId, a)
			end
		elseif bool == false then
			if not table.find(blacklist, v.Name) then
				table.remove(a, v.Name)
				data:SetAsync(plr.userId, a)
			end
		end
	end
end

players.PlayerAdded:Connect(function(plr)
	local b = data:GetAsync(plr.userId)
	if b then
		for _, v in pairs(b) do
			toolstorage:FindFirstChild(v):Clone().Parent = plr.Backpack
			toolstorage:FindFirstChild(v):Clone().Parent = plr.StarterGear
		end
	end
	local char = plr.Character or plr.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid", 10)
	hum.Died:Connect(function()
		for i,v in pairs(plr.StarterGear:GetChildren()) do
			if not table.find(blacklist, v.Name) then
				v:Destroy()
			end
		end
		for i,v in pairs(plr.Backpack:GetChildren()) do
			if not table.find(blacklist, v.Name) then
				v:Destroy()
			end
		end
		savedata(plr, false)
	end)
end)

players.PlayerRemoving:Connect(function(plr)
	savedata(plr, true)
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like
local toolstorage = game.ReplicatedStorage.Guns
local blacklist = {"Fists"}

local data = game:GetService("DataStoreService"):GetDataStore("Player_Tools")
local players = game.Players

local function savedata(plr, bool)
	local a = {}
	for _, v in pairs(plr.StarterGear:GetChildren()) do
		if bool == true then
			if not table.find(blacklist, v.Name) then
				table.insert(a, v.Name)
				data:SetAsync(plr.userId, a)
			end
		elseif bool == false then
			if not table.find(blacklist, v.Name) then
				table.remove(a, v.Name)
				data:SetAsync(plr.userId, a)
			end
		end
	end
end

players.PlayerAdded:Connect(function(plr)
	local b = data:GetAsync(plr.userId)
	if b then
		for _, v in pairs(b) do
			toolstorage:FindFirstChild(v):Clone().Parent = plr.Backpack
			toolstorage:FindFirstChild(v):Clone().Parent = plr.StarterGear
		end
	end
	local char = plr.Character or plr.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid", 10)
	hum.Died:Connect(function()
		for i,v in pairs(plr.StarterGear:GetChildren()) do
			if not table.find(blacklist, v.Name) then
				v.Parent = game.ReplicatedStorage.PlrStarterGear
			end
		end
		for i,v in pairs(plr.Backpack:GetChildren()) do
			if not table.find(blacklist, v.Name) then
				v.Parent = game.ReplicatedStorage.PlrBackpack
			end
		end
		savedata(plr, false)
	end)
	plr.CharacterAppearanceLoaded:Connect(function()
		for i,v in pairs(game.ReplicatedStorage.PlrBackpack:GetChildren()) do
			v.Parent = plr.Backpack
		end
		for i,v in pairs(game.ReplicatedStorage.PlrStarterGear:GetChildren()) do
			v.Parent = plr.StarterGear
		end
	end)
end)

Make a folder in replicated storage named PlrStarterGear and PlrBackpack

1 Like

still nope, the tool isn’t gone if you rejoin even if you lose it

can you show me what is inside the players backpack when he dies?

ggfsd

the fists is a starter weapon while others can be bought

local toolstorage = game.ReplicatedStorage.Guns
local blacklist = {"Fists"}

local data = game:GetService("DataStoreService"):GetDataStore("Player_Tools")
local players = game.Players

local function savedata(plr, bool)
	local a = {}
	for _, v in pairs(plr.StarterGear:GetChildren()) do
		if bool == true then
			if not table.find(blacklist, v.Name) then
				table.insert(a, v.Name)
				data:SetAsync(plr.userId, a)
			end
		elseif bool == false then
			if not table.find(blacklist, v.Name) then
				table.remove(a, v.Name)
				data:SetAsync(plr.userId, a)
			end
		end
	end
end

players.PlayerAdded:Connect(function(plr)
	local b = data:GetAsync(plr.userId)
	if b then
		for _, v in pairs(b) do
			toolstorage:FindFirstChild(v):Clone().Parent = plr.Backpack
			toolstorage:FindFirstChild(v):Clone().Parent = plr.StarterGear
		end
	end
	local char = plr.Character or plr.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid", 10)
	hum.Died:Connect(function()
		for i,v in pairs(plr.StarterGear:GetChildren()) do
			if not table.find(blacklist, v.Name) and v.Name == "Fists" then
				v:Destroy()
			end
		end
		for i,v in pairs(plr.Backpack:GetChildren()) do
			if not table.find(blacklist, v.Name) and v.Name == "Fists" then
				v:Destroy()
			end
		end
		local Fists = game.StarterPack:FindFirstChild("Fists",true)
		Fists:Destroy()
		savedata(plr, false)
	end)
end)

players.PlayerRemoving:Connect(function(plr)
	savedata(plr, true)
end)

your script is wrong as the if the tool in StarterPack gets destroyed, all players who join wont have it

then just remove that part bruh or add it back like this

		local Fists = game.StarterPack:FindFirstChild("Fists",true)
		Fists.Parent = game.ReplicatedStorage
		savedata(plr, false)
		wait(game.Players.RespawnTime+0.5)
		Fists.Parent = game.StarterPack

Edit:
it wont remove as i am doing it locally lol i am not doing it as server i am doing it as client

The fists are in the starter pack so it will clone itself automatically

yes yes but try that code as well also as i said i am doing it locally (client) not globally (server)

you can’t access to Datastores on the client. So your script is 100% not going to work

@CKY_DRAGON then use this