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

How do you make every players GUI change the same?

Asked by 9 years ago

Okay im trying to make a round script that shows the time left in a GUI. The problem im having is either it only shows up for the first player to join or the time of every GUI is different. I think the reason for this is because it goes through the loop and counts down from 45 before it runs through the loop again. I've been trying for weeks to fix this and have literally gotten no where. Can anyone please tell me how to fix these problems? Thanks!

I've narrowed the script down to the part im having problems with:

    function Intermission()
        for i,v in pairs(game.Players:GetPlayers) do
            v.PlayerGui.ScreenGui.Frame.Visible = true
                for i= 45,1 -1 do
                    v.PlayerGui.ScreenGui.Frame.TextLabel.Text = i
                                end
             end
end
0
No coroutines. BlueTaslem 18071 — 9y
0
Can you explain that? austin10111213 28 — 9y
0
Coroutines let you run functions "at the same time" (there're nuances). A lot of people suggest them to solve problems like this, but they are *very rarely* the correct answer, because they can make code *very* complicated very quickly (and there's usually better solutions). BlueTaslem 18071 — 9y
0
Sorry, my mind went to coroutines as the answer but I realize there's a better solution. Disreguard my statement about coroutines. Goulstem 8144 — 9y

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

A good way to fix this is by switching around your numeric for and your generic for loops.

This way, since there is no yeild while iterating through all players then it will be executed as expected.

function Intermission()
    for i = 45,1 -1 do
        for a,v in pairs(game.Players:GetPlayers) do
            v.PlayerGui.ScreenGui.Frame.Visible = true
            v.PlayerGui.ScreenGui.Frame.TextLabel.Text = i
        end
        wait(1)
    end
end
0
Thanks so much for the Help! austin10111213 28 — 9y
Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

First, ignore the loop.

Let's just make a function that tells everyone a number:

function tellNumber( num )
    for _, v in pairs(game.Players:GetPlayers()) do
        v.PlayerGui.ScreenGui.Frame.TextLabel.Text = num
        -- EDIT: Typo'd `i` for `num`
    end
end

Intermission every second sets the text of everyone. In other words, every second, we tellNumber:

function Intermission()
    for time = 45, 0, -1 do
        tellNumber( time )
        wait(1)
    end
end

If we were to inline the function, it would look like this:

function Intermission()
    for time = 45, 0, -1 do
        for _, v in pairs(game.Players:GetPlayers()) do
            v.PlayerGui.ScreenGui.Frame.TextLabel.Text = time
        end
        wait(1)
    end
end

which is basically just the loops "inside out" from how you originally wrote them.

(I think the code is much clearer and simpler with the function separate though!)


0
Thanks so much for the Help! austin10111213 28 — 9y
1
In your first code block you never defined 'i'. Goulstem 8144 — 9y
0
Blue, great to have you back. Shawnyg 4330 — 9y

Answer this question