While do loop isnt working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to know why my while loop isnt working, here it is:
while script.Parent.Enabled == true do
    wait()
    print("Gui is enabled")
end
  1. What is the issue? Include screenshots / videos if possible!
    Nothing prints in the output whenever the gui is enabled.

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried Devforum and YT

2 Likes

Why do you need this script? I feel like there is a way better way to achieve what you want to do. Also, what type of script is this and where is it located?

3 Likes

its a local script inside of a screengui, this is what im trying to do, its kinda complicated to explain why i need it

1 Like

Just explain it.

Is the screengui enabled or disabled when the game starts? While this script is running

Or are you trying to change it from disabled to enabled while this is running

1 Like
-- Should be a LocalScript not a ServerScript
local Gui = script.Parent

Gui.Changed:Connect(function()
    print("Gui is", Gui.Enabled and "enabled" or "disabled")
end)

why a while loop?

1 Like

Its enabled when the game starts

I have a feeling the GUI is disabled when the startergui gets loaded, the script gets enabled runs the code ends the loop, then the startergui gets enabled.

Try this:

while task.wait(1) do
    if script.Parent.Enabled then
        print("GUI enabled")
    else
        print("GUI disabled")
    end
end
2 Likes

Mind showing what script.Parent is? (as a screenshot)

1 Like

Trust me, the local script is in the gui, im a pretty experience scripter

If your GUI gets disabled then it will break out of the loop which will then stop the loop from running if the GUI then gets re-enabled. To fix this, and have better performance, you can use the GetPropertyChangedSignal method to detect when the Enabled property of the GUI has been changed.

See but i can’t do task wait (1) because its delayed by a second which i can’t have and i also dont wanna put the code in a while true do loop because it lags my game in studio

How about at the very top of your script you print this:

print(script.Parent.Enabled)
1 Like

You can use task.wait() with no parameters just like wait(). task.wait() is executed 60 times a second whereas wait() is executed 33 times a second. Also having the wait() call at the top of the loop will delay the code in the loop when the game is first run.

Checking if a property is equal to a certain value 33 times a second will also affect the performance of the game drastically for the player, where something like Changed or GetPropertyChangedSignal may come in handy :slight_smile:

1 Like

again, I don’t know what you are exactly wanting. Do you want to have it print out 33 times a second, or do you want it to print only when the property has changed?

1 Like

I can’t do that, i want to run some code while the gui is enabled, i just put print there so you could get an idea

I just want it to print when the gui is enabled

Yea that the thing i don’t wanna use while true do or task.wait because it lags my game, thats the whole point of this devforum

You could probably change it to:

while not script.Parent.Enabled do
    task.wait()
end
while script.Parent.Enabled do
    print("GUI enabled")
    task.wait()
end

Not saying this is the best way to do this though, but the spec is a bit vague so…

1 Like

why just do a loop with task.wait()?