How to prevent gear from duplicating?

I have a script that chooses through a lot of random events. This event gives the player a sword. However, when this event happens more than once the sword is duplicated. I’m trying to prevent the player from having more than one of the same gear.

The script:

game:GetService("Players").PlayerAdded:Connect(function(player)
	wait()
	local Backpack = player:WaitForChild("StarterGear")
	local ToolToClone = game:GetService("ServerStorage"):WaitForChild("ClassicSword")
	if ToolToClone and Backpack then
		ToolToClone:Clone().Parent = Backpack
	end
end)

Note: The rest of the script is long and unnecessary for this post. That’s why I didn’t include the rest here.

You can add a check before you clone the tool to see if the player already has said tool.

For example:

if not Backpack:FindFirstChild(ToolToClone.Name) and not player.Character:FindFirstChild(ToolToClone.Name) then
--continue code here
end
1 Like

I’m still learning to script. Would you mind explaining a bit more?

It’s basically looking for the tool name in players backpack & in the character. if it doesn’t find said tool name then it will continue past the if statement

1 Like

Add a debounce/cooldown, so it gives the player the tool then it has to wait a certain time before they can get the tool again.

Hope this helps!

It’s a loop that lasts forever. Even if I did add a debounce, eventually the player would get a duplicate. (This is not a LocalScript)

I get this error:

ServerScriptService.GameScript:47: attempt to index nil with 'Name'

Here’s what the script is:

if not Backpack:FindFirstChild(ToolToClone.Name) and not player.Character:FindFirstChild(ToolToClone.Name) then
	game:GetService("Players").PlayerAdded:Connect(function(player)
		wait()
		local Backpack = player:WaitForChild("StarterGear")
		local ToolToClone = game:GetService("ServerStorage"):WaitForChild("ClassicSword")
		if ToolToClone and Backpack then
			ToolToClone:Clone().Parent = Backpack
		end
	end)
end

Ok, I think I have got a solution.

To understand this answer, you will need to know about where tools are stored on the player. When a tool is unequipped, it automatically goes into a folder inside the player that is titled “backpack”. When they equip the tool, the location of it goes from the backpack, to the players character model. To see if they already have the tool, we need to scan first to ensure it is not in either of these places. We can do this by using the :FindFirstChild() function.

Here’s how it will work

game:GetService("Players").PlayerAdded:Connect(function(player)
   wait()
   local backpack = player:WaitForChild("Backpack")
   local character = player.Character
   local tool = --Define the tool

   if not backpack:FindFirstChild(tool) and not character:FindFirstChild(tool) then
      --Tool insert code
   end
end)

Hope I could help!

You’re using the ToolToClone variable before you’re setting it, try this;

game:GetService("Players").PlayerAdded:Connect(function(player)
	wait()
	local Backpack = player:WaitForChild("StarterGear")
	local ToolToClone = game:GetService("ServerStorage"):WaitForChild("ClassicSword")
	if ToolToClone and Backpack and not Backpack:FindFirstChild(ToolToClone.Name) then
		ToolToClone:Clone().Parent = Backpack
	end
end)

It doesn’t work for some reason. There’s nothing in the output.

Here’s the script:

game:GetService("Players").PlayerAdded:Connect(function(player)
	wait()
	local backpack = player:WaitForChild("Backpack")
	local character = player.Character
	local tool = game.ServerStorage.ClassicSword

		if not backpack:FindFirstChild(tool) and not character:FindFirstChild(tool) then
		game:GetService("Players").PlayerAdded:Connect(function(player)
			wait()
			local Backpack = player:WaitForChild("StarterGear")
			local ToolToClone = game:GetService("ServerStorage"):WaitForChild("ClassicSword")
			if ToolToClone and Backpack then
				ToolToClone:Clone().Parent = Backpack
			end
		end)
	end
end)

I’m probably missing something really obvious.

Your script has the same problem that @palmtreeninja2 has. There’s nothing in the output. :face_with_raised_eyebrow:

local function givePlayerToolNoDuplicates(player, tool)
    local character = player.Character

    --makes sure the character exists
    if not character then
        return --stops the function
    end
    local humanoid = character:FindFirstChild("Humanoid")

    --makes sure the character is alive
    if (not humanoid) or (humanoid.Health < 1) then
        return
    end
    local backpack = player.Backpack

     --makes sure the tool isn't equipped (in the character) or in the backpack
    if (backpack:FindFirstChild(tool.Name)) or (character:FindFirstChild(tool.Name)) then
        return
    end
    local newTool = tool:Clone() --creates a new tool
    newTool.Parent = backpack --moves the new tool to the player's backpack
end

Try using this function to add tools. Something to note: if the player isn’t alive or already has a tool with the same name the function doesn’t do anything, otherwise it puts the tool in the player’s backpack.

If you want the function to equip the tool, replace the last line (newTool.Parent = backpack) with:

local oldTool = character:FindFirstChildOfClass("Tool")
if oldTool then
    oldTool.Parent = backpack
end
newTool.Parent = character

I was able to get it working using this:

local function GiveSword()
	wait(3)
	for _, Players in pairs(game.Players:GetChildren()) do
		local characters = workspace[Players.Name]
		if not characters:FindFirstChild("ClassicSword") then
			game.ServerStorage.ClassicSword:Clone().Parent = characters
		end

	end
end

GiveSword()

Thanks to @SimonGLOW

1 Like