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

In pairs loop gone wrong?

Asked by
Drogb4 15
8 years ago

I honestly have no idea what im doing wrong here, but some things to know are *this is a LocalScript inside a tool and inside StarterPack

there is no output it just does nothing..

function onEquip(mouse)
    wait()
    local player = game.Players.LocalPlayer
    local Armors = game.ReplicatedStorage.Armors
    local tool = script.Parent

    for i,s in pairs(player.Character:GetChildren()) do
        if s:IsA("CharacterMesh") then
            s:Destroy()
        end
    for i,v in pairs(Armors.Newbie:GetChildren()) do
        if v:IsA("CharacterMesh") then
            v:Clone().Parent = player.Character
        end
    end
    end

end

script.Parent.Equipped:connect(onEquip)

1 answer

Log in to vote
3
Answered by 8 years ago

Simple, the 2nd in pairs loop is incorrectly placed. You placed it inside the first for loop, so every time the first for loop loops, the second for loop loops and clones the character meshes into the character. Basically you'll end up with multiple copies of the same character mesh in the character. To fix it, simply move the for loop outside the first one, like this:

for i,s in pairs(player.Character:GetChildren()) do
    if s:IsA("CharacterMesh") then
        s:Destroy()
    end
end
for i,v in pairs(Armors.Newbie:GetChildren()) do
    if v:IsA("CharacterMesh") then
        v:Clone().Parent = player.Character
    end
end

Hope this helped!

Ad

Answer this question