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

Why doesn't this While Loop Execute?

This is a much, much simplified version of a script I'm working on. But the logic still applies. Why won't the while loop execute? The function prints "1", but the while loop never starts.

function FigureChanged(object)
	index = nil
	if object.Name == "Player1" then
		index = 1
	end
	print(index)
end
workspace.ChildAdded:Connect(FigureChanged)

while index == 1 and wait(.1) do
	print"here"
end

2 answers

✓ Accepted answer

What about this?

function FigureChanged(object)
    index = nil
    if object.Name == "Player1" then
        index = 1
    end
    print(index)
		while index == 1 and wait(.1) do
   		 print"here"
end
end
workspace.ChildAdded:Connect(FigureChanged)

2 comments

0

This works, thank you very much! I do wonder why the original code didn't work though.

0

No problem, glad I could help.

Try this,

function FigureChanged(object)
    index = nil
    if object.Name == "Player1" then
        index = 1
    end
    print(index)
end
workspace.ChildAdded:Connect(FigureChanged)

while index == 1 do
    wait(.1)
    print"here"
end

1 comment

0

Unfortunately, this didn't work either. The function prints the "1", but the while loop never executes.

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