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

In pairs loop gone wrong?

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

✓ Accepted answer

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!

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