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

While true do function only works once, why?

Hello, I'm making a self-check-in system for a hotel game and I needed to store if the room is taken or not. For this, I made a Folder with BoolValues. For some reason, the script runs at the start but does not keep testing for the BoolValue. It goes off the starting value and not the updated one, but my print keeps printing.

Here are the scrips:

(0 = not taken | 1 = taken)

while true do
	if script.Parent.Value == 1 then
		game.ReplicatedStorage.RoomTaken["Room 101"].Value = true
	elseif script.Parent.Value == 0 then
		game.ReplicatedStorage.RoomTaken["Room 101"].Value = false
wait(0.5)
	end
end


while true do
	if game.ReplicatedStorage.RoomTaken["Room 101"].Value == false then
		script.Parent.BrickColor = BrickColor.new("Medium green")
		print("Green")
	elseif game.ReplicatedStorage.RoomTaken["Room 101"].Value == true then
		script.Parent.BrickColor = BrickColor.new("Bright red")
		print("Red")
wait(1)
end
end

6 comments

0

Put in CodeBox

0

Done.

0

MMmmh while true do. While TRUE do. Why are we using this? Why not watch for the value changing then call our action.

0

Could you give me an example?

1

1.) while true do is not a function. 2.) It is never a good idea to create an infinite loop with no ending condition. 3.) If there is an event that can take the place of a loop, use it (in your case that would be the Changed event). 4.) Your first if statement in both scenarios has no yield inside of it. That means if your Value does not change fast enough from 1/false, your game will crash.

1

Finally, your main issue is likely that your Value is not 1.) being correctly changed, or 2.) being correctly accessed.

1 answer

Example, see if this works better

 script.Parent.Changed:Connect(function()
    if script.Parent.Value == 1 then
        game.ReplicatedStorage.RoomTaken["Room 101"].Value = true
	print("True")
    elseif script.Parent.Value == 0 then
        game.ReplicatedStorage.RoomTaken["Room 101"].Value = false
	print("False")
	wait(0.5)
        end
    end)
 game.ReplicatedStorage.RoomTaken["Room 101"].Changed:Connect(function()
    if game.ReplicatedStorage.RoomTaken["Room 101"].Value == false then
        script.Parent.BrickColor = BrickColor.new("Medium green")
        print("Green")
    elseif game.ReplicatedStorage.RoomTaken["Room 101"].Value == true then
        script.Parent.BrickColor = BrickColor.new("Bright red")
        print("Red")
wait(1)
	end
end)

8 comments

0

use script.Parent:GetPropertyChangedSignal("Value"):Connect(function()

0

I editted it Expo, but what exactly does this do differently? I know that this SHOULD work as is, but is this more efficient?

0

It isn't working.

0

Did you try it the original way or how it is now

0

Both.

0

Are the values changing? Add prints and see if the script is doing its job. Or just watch the values.

0

I editted the thing

0

:GetPropertyChangedSignal() only listens for a certain property, Changed listens for every property (not really)

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