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
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
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!)