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

How do I execute something WHILE something is happening?

Asked by 4 years ago
Edited 4 years ago

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
0
This is not a While Loop, this is a For Loop. killerbrenden 1537 — 4y
0
accidentally posted wrong thing, fixed it finn383 2 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

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
Ad
Log in to vote
-1
Answered by 4 years ago
Edited 4 years ago

As I said in the comments, that is a For Loop not a While Loop

Loop Resource

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!

0
Thanks for the imput but I tried what you're doing and got the same effect it only executes when it becomes visible not while it visible (by default its not visible). The for loop is for each individual player, but within that for loop is the while statement. I'm still stuck :/ finn383 2 — 4y
0
What did you want to happen? You wanted something to happen when it became visible, and it should work? killerbrenden 1537 — 4y
0
No I said I wanted it to happen while it's visible not when it becomes visible is this possible? finn383 2 — 4y
0
I did that? I wrote, if it's visible then everything else is visible, if it's not visible then everything else is not visible........ And why did I get downvoted? killerbrenden 1537 — 4y
View all comments (2 more)
0
My rep is too low to downvote, but you even said "You wanted something to happen when it became visible" when that's the opposite of what I asked. Thats basically what I did also and both codes do exactly the same thing, still only executes when it BECOMES visible :/ finn383 2 — 4y
0
What do you want then? Because I clearly read multiple times that you want something to happen while it's visible......? killerbrenden 1537 — 4y

Answer this question