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

While loop still running even though it's condition has changed, how to fix?

local mood = script.Parent.Mood.Value

while mood == "Paranoid" do
	local Dialog = game:GetService("Chat"):Chat(script.Parent.Head, say[math.random(1, #say)], Enum.ChatColor.Blue)
	wait(math.random(5,9))
end

while mood == "Scared" do
	local Dialog = game:GetService("Chat"):Chat(script.Parent.Head, say2[math.random(1, #say2)], Enum.ChatColor.Blue)
	wait(math.random(2,3))
end

i'm trying to make an NPC say different stuff depending on the StringValue inside it, it's naturally set to "Paranoid", but when i change it ingame (Solo Test) to "Scared", it doesn't change, instead it keeps running the while loop, how do i fix this?

1 answer

When running a while loop its yielding the code, so the second while loop never gets executed. If you REALLY want to run them both at the same time you can use coroutines to create a new thread for each one of them.

Coroutines tutorial

Additionally, when the code go over a while loop when the condition is not met, if the condition suddenly meets it wont start the loop. So you might want to use a Changed event.

Changed API

3 comments

0

So i did coroutine.wrap on both loops, and did them run only when a variable's properties is changed, but nothing works, i've noticed that it won't work at all if any condition is not met.

0

As I mentioned, you need to use the changed event to run it when the value changes.

0

And i did, yet nothing works.

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