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

I need help removing duplicate tools whenever a player respawns?

Hello, I am a very amateur scripter, to the point where I have to take free scripts and modify them to fit the game I am working on, so I may require some additional help, but besides that, I have a bit of a problem with one of my scripts. The script I have for saving tools duplicates the tools in a player's inventory upon death, I will post the code to that script. Allthough an explanation on why this happens and a fix for the script would be helpful, what I mainly need is a script that removes duplicated tools.

local rs = game:GetService("ReplicatedStorage") local ds = game:GetService("DataStoreService") local store = ds:GetDataStore("saveStore") local library = rs:WaitForChild("Library")

--< directory functions

local dir = {}

local function edit(player, list) dir[player.Name] = list end

local function setup(player, list) for i = 1, #list do local tool = library:FindFirstChild(list[i]) if tool then local clone = tool:Clone() clone.Parent = player.Backpack else print(list[i] .. " not found") end end end

--< player events

game.Players.PlayerAdded:connect(function(player)

local ready = false

player.CharacterAdded:connect(function(char)

local bp = player.Backpack
local data = nil

if ready == false then
	ready = true
	
	data = store:GetAsync(player.UserId)
	
	if data then
		setup(player, data)
		edit(player, data)
	end
end	

char.Humanoid.Died:connect(function()
	char.Humanoid:UnequipTools()
	
	local old = player.StarterGear:GetChildren()
	for i = 1, #old do
		old[i]:Destroy()
	end
	
	local new = player.Backpack:GetChildren()
	for i = 1, #new do
		new[i].Parent = player.StarterGear
	end		
end)	

--< adjuster

local count = 0

local function adjust()
	
if char.Humanoid.Health > 0 then

	local list = {}

	local equipped = char:FindFirstChildOfClass("Tool")
	if equipped then
		table.insert(list, equipped.Name)
	end	

	local tools = bp:GetChildren()
	for i = 1, #tools do
		table.insert(list, tools[i].Name)
	end

	if count ~= #list then
		edit(player, list)
		count = #list
	end
end
end

--< child events

bp.ChildAdded:connect(adjust)	
bp.ChildRemoved:connect(adjust)	

char.ChildAdded:connect(function(child)
	if child.ClassName == "Tool" then
		adjust()
	end
end)

char.ChildRemoved:connect(function(child)
	if child.ClassName == "Tool" then
		adjust()
	end
end)	

end)

end)

game.Players.PlayerRemoving:connect(function(player) store:SetAsync(player.UserId, dir[player.Name]) dir[player.Name] = nil end)

--< safety

game:BindToClose(function() wait(5) end)

2 comments

0

aquacat , Please post the top of your code in Code Block please.

0

I did, It just didnt appear. Sorry, Im new to this.

1 answer

✓ Accepted answer

Please be more specific. You only really explained that you are a new scripter. Are you trying to make all the players spawn with that gear everytime? Are you trying to make a gamepass item??? Please be more exact here since I have no idea why you are trying to use Data Store Service here. Edit: When you use data store service it saves when the player leaves the game.

1 comment

0

Players get gear by buying it in ingame shops, no gamepasses. While the gear does save after a player buys it, and appears again when they die or rejoin, it duplicates after the player dies. Sorry about being unspecific.

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