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

How do I count down?

Asked by 10 years ago

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.

3 answers

Log in to vote
3
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

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)
0
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? EzraNehemiah_TF2 3552 — 10y
0
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. Perci1 4988 — 10y
0
You could just set ResetGuiOnSpawn or what the property is off. hiimgoodpack 2009 — 7y
Ad
Log in to vote
2
Answered by 10 years ago

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
Log in to vote
1
Answered by
Necrorave 560 Moderation Voter
10 years ago

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

3
@LordDragonZord Posting answers is not a competition; a new answer could give a new insight on a given topic. Redbullusa 1580 — 10y

Answer this question