Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Countdown script resetting?

So, I made this script that works:

local time = 120 
for i = 1, 120 do  
wait(1) 
time = time - 1  
script.Parent.Text = tostring(time) 
end 

But when you die, the whole clock resets. Can somebody add a line that makes it so it doesn't reset? Thanks!

3 comments

0

What's the purpose of this script?

1

the only way that you could create a global countdown would be to make this a server script and have some way that each player would receive the value from the script

0

Can you tell me the script golden?

2 answers

✓ Accepted answer

Original Answer

The easiest way I can think of doing this would be to clone a GUI into every character that spawns, while keeping the time in a server script. Put this in a script that is in the workspace, and a gui in the script:

local gui = script.TextLabel -- This line assumes you have a TextLabel with the script as it's Parent, change it as you wish.
local time = 120 

for i = 1, 120 do  
wait(1) 
time = time - 1  
end 

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		gui:Clone().Parent = player.PlayerGui
		gui.Text = tostring(time)
	end)
end)

You're countdown script will now count down from 120 to 0 once while putting a GUI on you're screen each time you re spawn.

Resetting and Reapplying

The best way to do this, in my opinion, would be to make the original countdown script itself a function. Then, when the function is called, it will start the countdown.

local gui = script.TextLabel -- This line assumes you have a TextLabel with the script as it's Parent, change it as you wish.
local time = 120 

function countdown() -- Declared the function 'countdown'
	for i = 1, 120 do  
		wait(1) 
		time = time - 1  
	end 
	game.Players.PlayerAdded:connect(function(player)
		player.CharacterAdded:connect(function(character)
			gui:Clone().Parent = player.PlayerGui
			gui.Text = tostring(time)
			if time == 0 then -- Checks if the countdown is done
				gui:Destroy()
				-- Add in an event you wish to happen when the countdown finishes here
				wait()
				time = 120 -- Resets the countdown
			end
		end)
	end)
end

countdown() -- Now every time this is called, it will run the function

Now, you can add on anything to this script and just call the function countdown when you want the countdown to start.

5 comments

0

Thanks !

0

But can this script loop? Like when its done the countdown, does it do it again?

0

Right now the script does not, but you could make it loop. Would you like some help with that too?

0

Yes

0

Check out my new edit, subtitled 'Resetting and Reapplying'

Put this code in the Gui

local time = tonumber(script.Parent.Text)
for i = time, 0, -1 do 
	wait(1)
	script.Parent.Text = tostring(i)
end 

And in a global script

for i = 120, 0, -1 do
	game.StarterGui.CLOCK.Text = i --or whatever its called
	wait(1)
end

1 comment

0

thanks you!

This discussion is preserved as it was. New answers, comments, and votes are closed.