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.
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
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
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)
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)
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
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()