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

Sword area giving multiple swords and only removing 1?

I'd like some help on a script i'm working on. Its a script that gives a sword when you enter an area then removes it when you leave. It gives the sword and removes the sword. But the problem I'm having is when you enter the area and then un equip the sword it gives you another, and when you leave it only removes 1 and you have to equip the second one for that one to remove aswell. This is not limited to 2 sword though. If u keep unequipping the swords u can get many duplicates. For removing the swords its still 1 by 1. Heres my code:

while wait() do
	for i, v in pairs(game.Players:GetChildren()) do
		local currentswords = v.Character:FindFirstChild("Sword")
		if v and v.Character ~= nil and v.Character:FindFirstChild("Head") and _G.Check(v.Character.Head.Position, workspace.SFArea.Pad) == true and not v.Character:FindFirstChild("Sword", true) then
			Sw = game.Lighting.Sword:clone()
			Sw.Parent = v.Character
		elseif v and v.Character ~= nil and v.Character:FindFirstChild("Head") and _G.Check(v.Character.Head.Position, workspace.SFArea.Pad) == false and v.Character:FindFirstChild("Sword", true) then
			v.Character:FindFirstChild("Sword", true):remove()
		else
			
			
		end
	end
end

Thanks for your time.

1 answer

The cause of this could be the while wait() do as it doesn't have time to check for the sword and creates 2 swords as it think's the player doesn't have one. Try making it wait(0.5) or something.

if you want to allow it to remove all you can do this:

for _, p in pairs(v.Character:GetDescendants()) do
 if p.Name == "Sword" then
    p:Destroy()
  end
end

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