For loop not working

What do you want to achieve?
I want to clone every object inside of my character in a for a loop, but nothing is cloning inside of the model.

What is the issue?
Nothing is cloning at all. Actually, the for loop isn’t even running! Especially weird since this code was working before. Here’s a pic.

image

There is nothing in the output

Here’s the code:

Client:

--Cloned Character
char.Archivable = true
local cloned_char = char:Clone()
cloned_char.Name = "Clone"
cloned_char.Parent = viewport

-- >>: Clone everything in the character
for i, v in pairs(char:GetChildren()) do
	print("Start")
	local item = v:Clone()
	item.Parent = cloned_char
	if item.Name == "Humanoid" then
		local Human = item
		Human.DisplayName = " "
	end
	if item.Name == "HumanoidRootPart" then
		local HRP = item
		cloned_char.PrimaryPart = HRP
	end
end

This is not the entire script

What solutions have you tried so far?

  • Switching it to elseif to see if something would happen
  • Debugged

Thanks for the help!

is char:GetChildren() empty? did you print it to check its contents?

1 Like

@WeeklyShoe It prints nothing. It’s strange.

The local char is also written correctly.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

I think you should try to make the “char:GetChildren()” in

Local childern = char:GetChildren()
for i,v in pairs (children) do

I usually handled for do loop in this method,and it works.

If it isn’t,try to see if the char is nil.

1 Like

The char variable is probably nil, and if you connected the character added function with player added function, you will have to put a wait() on it.

1 Like

The reason the for loop is seemingly not doing anything is because you’re instantly trying to iterate over the character’s children, which take a very small amount of time to be added. You need at the very least add something like a RunService.Heartbeat:Wait() before the loop and it should work as intended.

Could you please show me an example of what you’re talking about? Thanks for everything btw.

Nevermind that’s all I literally had to do :upside_down_face:

Thanks to @WeeklyShoe @CreativeBuilder346 @Snow_Lober for the help as well.

1 Like

Ah! Sorry about that. I thought by chance that you already knew about game services.

1 Like

I knew about runservice but I had no idea what heartbeat was lol. Thanks anyway!