Hi so I am making a sword giver/remover and it works fine for one player, but when one player has it in their inventory no one else can get it, anyone know how to fix this?
local arenaRestrictions = script.Parent
local tpPart1 = arenaRestrictions.TPPart1
local tpPart2 = arenaRestrictions.TPPart2
local replicatedStorage = game:GetService("ReplicatedStorage")
local classicSword = replicatedStorage:WaitForChild("Tools").ClassicSword
local givenSword = false
local debounce = false
arenaRestrictions.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and not debounce then
debounce = true
if not givenSword then
givenSword = true
local newSword = classicSword:Clone()
newSword.Parent = hit.Parent
hit.Parent.HumanoidRootPart.CFrame = tpPart2.CFrame
else
for _, sword in pairs(hit.Parent:GetChildren()) do
if sword:IsA("Tool") then
sword:Destroy()
givenSword = false
hit.Parent.HumanoidRootPart.CFrame = tpPart1.CFrame
end
end
end
wait(1)
debounce = false
end
end)
In this case I recommend you’d do a table debounce system instead of a single debounce, as givenSword will be true for everyone if one player hits it
local arenaRestrictions = script.Parent
local tpPart1 = arenaRestrictions.TPPart1
local tpPart2 = arenaRestrictions.TPPart2
local replicatedStorage = game:GetService("ReplicatedStorage")
local classicSword = replicatedStorage:WaitForChild("Tools").ClassicSword
local givenSword = {}
local debounce = false
arenaRestrictions.Touched:Connect(function(hit)
local char = hit.Parent
if char:FindFirstChild("Humanoid") and not debounce then
debounce = true
if not givenSword[char.Name] then
givenSword[char.Name] = true
local newSword = classicSword:Clone()
newSword.Parent = char
char.HumanoidRootPart.CFrame = tpPart2.CFrame
else
for _, sword in pairs(hit.Parent:GetChildren()) do
if sword:IsA("Tool") then
sword:Destroy()
givenSword[char.Name] = nil
char.HumanoidRootPart.CFrame = tpPart1.CFrame
break
end
end
end
wait(1)
debounce = false
end
end)
Also, I recommend breaking the loop once a sword has been found to prevent unneeded checks
You want to create the givenSword variable inside the Touched event, or if one player touches it and doesn’t get the sword for whatever reason they will only change the variable that was created when the part was touched, or it will be false forever.
Wait what type of script is this? Server script or local script? Try printing out what givenSword[char.Name] contains. Also, when you were testing, were any of oyu holding your sword or no? Cause this only checks if you are holding it, not if it’s in your backpack
local arenaRestrictions = script.Parent
local tpPart1 = arenaRestrictions.TPPart1
local tpPart2 = arenaRestrictions.TPPart2
local replicatedStorage = game:GetService("ReplicatedStorage")
local classicSword = replicatedStorage:WaitForChild("Tools").ClassicSword
local givenSword = {}
local debounce = false
arenaRestrictions.Touched:Connect(function(hit)
local char = hit.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
if plr and not debounce then
debounce = true
if not givenSword[char.Name] then
givenSword[char.Name] = true
local newSword = classicSword:Clone()
newSword.Parent = char
char.HumanoidRootPart.CFrame = tpPart2.CFrame
else
local sword = char:FindFirstChildOfClass("Tool")
if tool and tool.Name == classicSword.Name then
tool:Destroy()
else
for _, sword in pairs(plr.Backpack:GetChildren()) do
if sword.Name == classicSword.Name then
sword:Destroy()
char.HumanoidRootPart.CFrame = tpPart1.CFrame
break
end
end
end
givenSword[char.Name] = nil
end
wait(1)
debounce = false
end
end)