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

How do I keep track a countdown time and display it?

Asked by 5 years ago
Edited 5 years ago

https://gyazo.com/caad2831a7d74a512f4078ceb13ff694

I am trying to do something like this in terms of the countdown time but I have no idea how to go about doing it.

I have a script that creates bool values inside of the Player to keep track of when they are pressed. I have a local script that makes sure the player is pressing a key and the server script it fires to changes the bool values inside of the Player. Additionally, I have a script that listens for when a bool value changes thus make the GUI's turn red, but I have no idea how I would go about showing the cooldown time. Any help would be greatly appreciated. My set-up is shown here:https://gyazo.com/bf7428503f532894c24b0a1b3db94ea5

1 answer

Log in to vote
1
Answered by 5 years ago

Fire a local script with remote events whenever the timer starts. In one of the parameters you can put how long the cooldown is going to be, so the local script knows where to start it. Make another parameter to let the local script know which text to change. Oh and yeah, another one showing what the original text was.

Here's what the local script would look like:

local repStorage = game:GetService("ReplicatedStorage")
local remoteEvent = repStorage:WaitForChild("RemoteEvent")

remoteEvent.OnClientEvent:Connect(function(cooldown,textToChange,originalText)

    local text = script.Parent:WaitForChild(textToChange) -- Make sure to send the name of textToChange instead of the actual text itself, or else this probably wouldn't work.

    local counter = cooldown

    while true do

        if counter <= 0 then

            text.Text = tostring(counter) -- This sets the text of the key to press to the counter.
            counter = counter - 0.1
            wait(0.1)

        else        

            text.Text = originalText -- This sets the text back to normal after it reaches 0.

        end

    end

end)

Server Script:

local repStorage = game:GetService("ReplicatedStorage")
local remoteEvent = repStorage:WaitForChild("RemoteEvent")

remoteEvent:FireClient(player,cooldown,text.Name,text.Text) -- Note that you're going to have to have the player as the first parameter when firing from a server script so that it knows which local player's local script to fire.

If you don't understand remote events, look at this video: https://www.youtube.com/watch?v=4Dc_bri9mjs

0
Hope I didn't make it sound too complicated. xD Knineteen19 307 — 5y
0
Nope I got it! This might sound arrogant considering I was the one asking for help but you made mistake on line 12 in the local script it should be >= 0. But I really am thankful for your help! iGlaciem 72 — 5y
0
But you would be asking if the timer is up or not. If you did >= 0 then it would stop as soon as it starts. I'm glad I helped though! Knineteen19 307 — 5y
0
Oh wait, I'm confusing myself, never mind, you're right. ;P Knineteen19 307 — 5y
Ad

Answer this question