Obviously a while statement but im trying to make it so while "TopBar" is Visible it executes the while statement. But when I try it, it only executes when "TopBar" becomes visible. Am I able to fix this?
for i,v in pairs(game.Players:GetPlayers()) do while v.PlayerGui.Intermission.TopBar.Visible do v.PlayerGui.Intermission.ImageButton.Visible = true v.PlayerGui.Intermission.Forest.Visible = true v.PlayerGui.Intermission.Votes.Visible = true end end
Running a while loop without a wait can cause a game script timeout, even with a wait function, a while loop can cause yielding. Observe:
while true do wait(1) print('wow')--> a lot of wows end; print('potato')-- no potatos
Notice how as time goes on, the print function in the while loop always prints wow
but never potato
. Since the while loop has to repeat the code block, it will never go beyond.
A solution is to place the loop into a new thread, it is similar to making a new script and executing it without interrupting other scripts.
Functions like spawn
or coroutine.wrap
can make new threads. (note coroutine.wrap has a different way of placing code into a different thread)
spawn(function() while true do wait(1) print('wow')--> a lot of wows end; end); print('potato')-- potato
As I said in the comments, that is a For Loop
not a While Loop
To do this, you would use a While Loop, but also doing a Boolean Check.
Something like this, I would assume.
while wait() do if script.Parent.TopBar.Visible == true then script.Parent.ImageButton.Visible = true script.Parent.Forest.Visible = true script.Parent.Votes.Visible = true else script.Parent.ImageButton.Visible = false script.Parent.Forest.Visible = false script.Parent.Votes.Visible = false end end
You would put this in a LocalScript inside of Intermission ScreenGui.
This will loop through the code every 0.03 seconds, checking if TopBar is visible. If TopBar is visible, it will make those 3 other elements visible as well. If it's not visible, then it will not have those 3 other elements visible.
To do your method, you would put a ServerScript in ServerScriptService and write this.
for _,player in pairs(game:GetService("Players"):GetChildren()) do while wait() do if player:WaitForChild("Intermission").TopBar.Visible == true then script.Parent.ImageButton.Visible = true script.Parent.Forest.Visible = true script.Parent.Votes.Visible = true else script.Parent.ImageButton.Visible = false script.Parent.Forest.Visible = false script.Parent.Votes.Visible = false end end end
Either way really works. Both are efficient, I think?
Another way to do this is
script.Parent.TopBar:GetPropertyChangedSignal("Visible"):Connect(function(value)
But, this way would be easier as it's an always active loop. Refer to the Loop Resource
for more information on loops.
Hope this helped! If this worked, let me know by selecting this as an answer!