I made a script so if you touch a part you get a sword. I made it so you'll only get one sword. However, if you equip the sword it would give you an extra sword. How would I fix this? Here is the script:
script.Parent.Touched:Connect(function(hit) local plr = game.Players:GetPlayerFromCharacter(hit.Parent) local sword = game.ReplicatedStorage.Sword:Clone() -- Detects if the part that 'hit' the brick contains a Humanoid wait() if plr.Backpack:FindFirstChild("Sword") then else if hit.Parent.Humanoid then -- Function GetPlayerFromCharacter returns the Character associated with the player (hit.Parent) -- Clones tool into player's backpack sword.Parent = plr.Backpack end end end)
check if the player already has the sword with
local player = game.Players.LocalPlayer local hasSword = false for _,v in pairs (player.Character:GetChildren()) do if v.Name ~= "Sword" then hasSword = true end end for _,v in pairs (player.Backpack:GetChildren()) do if v.Name ~= "Sword" then hasSword = true end end if hasSword then local sword = game.ReplicatedStorage.Sword:Clone() sword.Parent = player.Backpack end
The reason you gotta check the player's backpack and character is that if the item is equipped it's parent is the character if it's not it's in the backpack.
local deb = false script.Parent.Touched:Connect(function(hit) local plr = game.Players:GetPlayerFromCharacter(hit.Parent) local sword = game.ReplicatedStorage.Sword:Clone() local human = hit.Parent:FindFirstChild("Humanoid") -- Detects if the part that 'hit' the brick contains a Humanoid if human then if plr.Backpack:FindFirstChild("Sword") then else if not deb then deb = true sword.Parent = plr.Backpack wait(2) deb = false end end end end)
i just copied and pasted your work here and edited it. (i also added a debounce)