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.
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
@LordDragonZord Posting answers is not a competition; a new answer could give a new insight on a given topic.
This discussion is preserved as it was. New answers, comments, and votes are closed.
3 comments
So isn't tostring(time.Value) the same thing as time.Value? Like "script.Parent.Text = tostring(time.Value)" and "script.Parent.Text = time.Value" do the same thing don't they?
Essentially, yes, but tostring() add an extra check to make sure the script doesn't error because we tried to change a string to a number.
You could just set ResetGuiOnSpawn or what the property is off.