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

Can I have help for my countdown Gui reach 00:00 please?

Asked by 1 year ago
Edited 1 year ago

Hey guys I want to create a countdown gui and when the countdown is at 00:00, all the players are killed and the countdown restart. Theres the script into the textlabel:

local rep = game:GetService("ReplicatedStorage")
local minutes = rep:WaitForChild("Minutes")
local seconds = rep:WaitForChild("Seconds")
local text = script.Parent

if seconds.Value <= 9 then
    text.Text = tostring(minutes.Value)..":0"..tostring(seconds.Value)
else
    text.Text = tostring(minutes.Value)..":"..tostring(seconds.Value)
end

minutes:GetPropertyChangedSignal("Value"):Connect(function()
    if seconds.Value <= 9 then
        text.Text = tostring(minutes.Value)..":0"..tostring(seconds.Value)
    else
        text.Text = tostring(minutes.Value)..":"..tostring(seconds.Value)
    end
end)

seconds:GetPropertyChangedSignal("Value"):Connect(function()
    if seconds.Value <= 9 then
        text.Text = tostring(minutes.Value)..":0"..tostring(seconds.Value)
    else
        text.Text = tostring(minutes.Value)..":"..tostring(seconds.Value)
    end
end)

and the script in ServerScriptService:

local rep = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local minutesvalue = rep:WaitForChild("Minutes")
local secondsvalue = rep:WaitForChild("Seconds")
local minutes = 10 --minutes
local seconds = 0 --seconds

while true do
    minutesvalue.Value = minutes
    secondsvalue.Value = seconds

    repeat
        if secondsvalue.Value <= 0 then
            minutesvalue.Value = minutesvalue.Value - 1
            secondsvalue.Value = 59
        else
            secondsvalue.Value = secondsvalue.Value - 1
        end
        wait(1)
    until secondsvalue.Value <= 0 and minutesvalue.Value <= 0
    players.LocalPlayer.Character.Humanoid.Health = 0
end

Ty in advance and have a nice day!

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

I sincerely recommend for you to just use seconds only and format the number correctly when you have to display it. You can also use the 02d format to automatically add the '0' to single-digit numbers. Also, you can use the .Changed event for value-instances.

For example:

local rep = game:GetService("ReplicatedStorage")
local seconds = rep:WaitForChild("Seconds") -- Assuming you're using my advice, so you'd only have a "seconds" value
local text = script.Parent

local function formatCountdown(t: number) -- t is the remaining time
    local min = string.format("d", math.floor(t / 60 % 60)) -- apparently displays incorrectly on scriptinghelpers.org, the format used here is actually `02d`
    local sec = string.format("d", math.floor(t % 60))  -- apparently displays incorrectly on scriptinghelpers.org, the format used here is actually `02d`
    return string.format("%s:%s", min, sec))
end

seconds.Changed:Connect(function()
    text.Text = formatCountdown(seconds.Value)
end)

Now, to address your actual problem – your only problem is that you're trying to access LocalPlayer from a Server-Script but if you'd like more advice: I'd never recommend to use while true do, I consider it to simply be bad practice. Instead you could use just repeat in a fashion similar to this:

local rep = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local seconds = rep:WaitForChild("Seconds")

-- Naturally, the `repeat` here would stop the code from further
-- progressing, which means that if we want something to happen
-- after the `repeat` we can simply place it after the actual
-- `repeat` as long as it's in the same scope
repeat
    seconds.Value -= 1 -- decrease by one
until seconds.Value <= 0

-- Here we iterate over each player, as a server-script wouldn't have
-- any access to `LocalPlayer`, since that is limited to the client 
for _,player in pairs(game:GetService("Players"):GetPlayers()) do
    player.Character.Humanoid.Health = 0
end
Ad

Answer this question