How would I count down on a GUI/SurfaceGUI?
I hope this isn't requesting a script, but I need help on it for my game.
I would like to point out that directly changing the gui's text with a for loop will cause the timer to reset after death.
To make the timer stay the same for everyone, regardless if they die, we must create an external object to keep track of the time. In this case, we can simply use an IntValue in workspace.
Inside the IntValue; server Script
1 | for i = 10 , 0 , - 1 do |
2 | wait( 1 ) |
3 | script.Parent.Value = i |
4 | end |
Inside the GUI; LocalScript
1 | local time = workspace:WaitForChild( "IntValue" ) |
2 |
3 | --everytime it's changed, |
4 | value.Changed:connect( function () |
5 | --set the text to the value |
6 | script.Parent.Text = tostring (time.Value) |
7 | --tostring() simply converts a number to a string. |
8 | end ) |
Use loops like
1 | for i = 10 , 0 , - 1 do --Starting number, Ending Number, steps |
2 | local gui = game.Players.LocalPlayer |
3 | --local gui2 = workspace.Part.SurfaceGui.TextBox |
4 | plyr.PlayerGui.ScreenGui.TextBox.Text = i |
5 | --gui2.Text = i |
6 | wait( 1 ) |
7 | end |
Don't forget to edit it to make it work.
LocalPlayer only works in localscripts and local scripts can only work it's a child of Player or in a person's character.
You could also do repeats
01 | Time = 10 -ten seconds |
02 | repeat |
03 | wait( 1 ) |
04 | Time = Time- 1 |
05 | local gui = game.Players.LocalPlayer |
06 | --local gui2 = workspace.Part.SurfaceGui.TextBox |
07 | plyr.PlayerGui.ScreenGui.TextBox.Text = Time |
08 | --gui2.Text = Time |
09 | until Time < = 0 |
10 | --Chunk |
Using a loop of some sort.
I would recommend a for
loop simply because of how easy it is to use.
Create a condition that ends the loop in a desirable fashion using a negative increment.
Quick example:
1 | for x = 10 , 0 , - 1 do --First part is the variable used, second is the condition it needs to reach, third is the amount it changes each time. |
2 | print (x) |
3 | wait( 1 ) |
4 | end |
More on loops here: wiki.roblox.com/index.php?title=Loops