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

Countdown script resetting?

Asked by 10 years ago

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!

0
What's the purpose of this script? GoldenPhysics 474 — 10y
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 FearMeIAmLag 1161 — 10y
0
Can you tell me the script golden? PyccknnXakep 1225 — 10y

2 answers

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
10 years ago

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.

0
Thanks ! PyccknnXakep 1225 — 10y
0
But can this script loop? Like when its done the countdown, does it do it again? PyccknnXakep 1225 — 10y
0
Right now the script does not, but you could make it loop. Would you like some help with that too? BlackJPI 2658 — 10y
0
Yes PyccknnXakep 1225 — 10y
0
Check out my new edit, subtitled 'Resetting and Reapplying' BlackJPI 2658 — 10y
Ad
Log in to vote
0
Answered by
wazap 100
10 years ago

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
0
thanks you! PyccknnXakep 1225 — 10y

Answer this question