Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

While true do function only works once, why?

Asked by 5 years ago
Edited 5 years ago

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)

  1. 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
    
    

2.

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
0
Put in CodeBox yHasteeD 1819 — 5y
0
Done. captain_bboy 22 — 5y
0
MMmmh while true do. While TRUE do. Why are we using this? Why not watch for the value changing then call our action. DinozCreates 1070 — 5y
0
Could you give me an example? captain_bboy 22 — 5y
View all comments (2 more)
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. User#25115 0 — 5y
1
Finally, your main issue is likely that your Value is not 1.) being correctly changed, or 2.) being correctly accessed. User#25115 0 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)
0
use script.Parent:GetPropertyChangedSignal("Value"):Connect(function() User#23365 30 — 5y
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? DinozCreates 1070 — 5y
0
It isn't working. captain_bboy 22 — 5y
0
Did you try it the original way or how it is now DinozCreates 1070 — 5y
View all comments (4 more)
0
Both. captain_bboy 22 — 5y
0
Are the values changing? Add prints and see if the script is doing its job. Or just watch the values. DinozCreates 1070 — 5y
0
I editted the thing DinozCreates 1070 — 5y
0
:GetPropertyChangedSignal() only listens for a certain property, Changed listens for every property (not really) User#23365 30 — 5y
Ad

Answer this question