Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this Sword spawn infinitely into my backpack?

Asked by
Yeevivor4 155
9 years ago
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

Log in to vote
2
Answered by
Muoshuu 580 Moderation Voter
9 years ago
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.

Ad

Answer this question