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

Why does this Sword spawn infinitely into my backpack?

local part = game.Workspace.Part
LinkedSword = game.Lighting.LinkedSword
while true do
  for i,v in pairs(game.Players:GetChildren()) do
 	   if (v.Character.Torso.Position - part.Position).magnitude <= 15 then
   	     c = game.Lighting.LinkedSword:clone()
    	     c.Parent = v.Backpack
   	   break
 	 end
  end

 wait()
 end

When I go near the part, the "LinkedSword" clones itself infinitely into my backpack. How do I modify it so it'll become only 1 sword that is cloned into my backpack?

1 answer

✓ Accepted answer
local Part=workspace.part
local Sword=game.Lighting.LinkedSword

while wait() do
	for i,v in pairs(game.Players:GetChildren()) do
		if (v.Character.Torso.Position-Part.Position).magnitude<=15 then
			if not v.Backpack:FindFirstChild(Sword.Name) then
				Sword:Clone().Parent=v.Backpack
			end
		end
	end
end

That should work quite nicely.

The reason it was cloning it infinitely was because you were using a while loop, which repeatedly does something infinitely until you stop it with 'break' or 'return'. What I did was I made it so it only cloned the sword if the player didn't already have the sword.

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