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
for i = 10, 0, -1 do wait(1) script.Parent.Value = i end
Inside the GUI; LocalScript
local time = workspace:WaitForChild("IntValue") --everytime it's changed, value.Changed:connect(function() --set the text to the value script.Parent.Text = tostring(time.Value) --tostring() simply converts a number to a string. end)
Use loops like
for i = 10, 0, -1 do --Starting number, Ending Number, steps local gui = game.Players.LocalPlayer --local gui2 = workspace.Part.SurfaceGui.TextBox plyr.PlayerGui.ScreenGui.TextBox.Text = i --gui2.Text = i wait(1) 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
Time = 10 -ten seconds repeat wait(1) Time = Time-1 local gui = game.Players.LocalPlayer --local gui2 = workspace.Part.SurfaceGui.TextBox plyr.PlayerGui.ScreenGui.TextBox.Text = Time --gui2.Text = Time until Time <= 0 --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:
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. print(x) wait(1) end
More on loops here: wiki.roblox.com/index.php?title=Loops