Hi, I’d like to know ways that I could shorten this timer script.
while wait() do
if Minutes and Seconds and TimerSpeed and Tower and Stages then
repeat
if Seconds.Value <= 9 then
TimerTag.Value = tostring(Minutes.Value)..":0"..tostring(Seconds.Value)
else
TimerTag.Value = tostring(Minutes.Value)..":"..tostring(Seconds.Value)
end
if Seconds.Value <= 0 then
Minutes.Value = Minutes.Value - 1
Seconds.Value = 59
else
Seconds.Value = Seconds.Value - 1
end
wait(TimerSpeed.Value)
until Minutes.Value <= 0 and Seconds.Value <= 0
if Minutes.Value <= 0 and Seconds.Value <= 0 then
TimerTag.Value = "0:00"
end
wait(TimerSpeed.Value)
for i, v in pairs(Players:GetChildren()) do
if v.Character:FindFirstChild("Win") then
v.Character.Win:Destroy()
end
if v:FindFirstChild("Backpack") then
v.Backpack:ClearAllChildren()
end
end
Hey! Setting the TimerTag value can be shortened using string formatting. I’ve written a sample function that takes a “time” parameter, counts down to 0, and then operates on the player. This method can be called from a main game loop, for example. Hope this helps!
-- Total time in seconds
local function timer(time)
while time >= 0 do
-- This will format the time as 0:00 and also 00:00 depending on
-- the number of minutes
TimerTag.Value = string.format("%d:%02d", math.floor(time / 60), time % 60)
time = time - 1
wait(1)
end
for i, v in pairs(Players:GetChildren()) do
if v.Character:FindFirstChild("Win") then
v.Character.Win:Destroy()
end
if v:FindFirstChild("Backpack") then
v.Backpack:ClearAllChildren()
end
end
end
Okay it’s not working but it may be because I didn’t show more of the script that was essential, my bad.
Minutes.Value = 2
Seconds.Value = 0
TimerSpeed.Value = 1
MultiplierVisible.Value = false
Multiplies.Value = 1
Debounce = true
NewTower()
while wait() do
if Minutes and Seconds and TimerSpeed and Tower and Stages then
repeat
if Seconds.Value <= 9 then
TimerTag.Value = tostring(Minutes.Value)..":0"..tostring(Seconds.Value)
else
TimerTag.Value = tostring(Minutes.Value)..":"..tostring(Seconds.Value)
end
if Seconds.Value <= 0 then
Minutes.Value = Minutes.Value - 1
Seconds.Value = 59
else
Seconds.Value = Seconds.Value - 1
end
wait(TimerSpeed.Value)
until Minutes.Value <= 0 and Seconds.Value <= 0
if Minutes.Value <= 0 and Seconds.Value <= 0 then
TimerTag.Value = "0:00"
end
wait(TimerSpeed.Value)
for i, v in pairs(Players:GetChildren()) do
if v.Character:FindFirstChild("Win") then
v.Character.Win:Destroy()
end
if v:FindFirstChild("Backpack") then
v.Backpack:ClearAllChildren()
end
end
Tower:ClearAllChildren()
Minutes.Value = 2
Seconds.Value = 0
TimerSpeed.Value = 1
MultiplierVisible.Value = false
Multiplies.Value = 1
Yes there are multiplier values operating the TimerSpeed in a separate script. I also have a question, since some other scripts have Minute and Second values inside would it still work with the shortened script. Example: if Minutes.Value == 2 and Seconds.Value == 0 then
local TimerSpeed = ReplicatedStorage.Values:WaitForChild("TimerSpeed")
local MultiplierVisible = ReplicatedStorage.Values:WaitForChild("MultiplierVisible")
local Multiplies = ReplicatedStorage.Values:WaitForChild("Multiplies")
local Minutes = ReplicatedStorage.Values:WaitForChild("Minutes")
local Seconds = ReplicatedStorage.Values:WaitForChild("Seconds")
local Winners = {}
script.Parent.Touched:Connect(function(hit)
if not hit.Parent:FindFirstChild("Humanoid") then return end
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not Player then return end
if table.find(Winners, Player.UserId) then return end
table.insert(Winners, Player.UserId)
TimerSpeed.Value = TimerSpeed.Value / 2
DoorMessage:FireAllClients(Player)
MultiplierVisible.Value = true
Multiplies.Value = Multiplies.Value * 2
end)
This should work! I overlooked that you could have been modifying the values from other scripts.
local function timer(time)
while time >= 0 do
TimerTag.Value = string.format("%d:%02d", math.floor(time / 60), time % 60)
time = time - 1
wait(TimerSpeed.Value)
end
for i, v in pairs(Players:GetChildren()) do
if v.Character:FindFirstChild("Win") then
v.Character.Win:Destroy()
end
if v:FindFirstChild("Backpack") then
v.Backpack:ClearAllChildren()
end
end
Tower:ClearAllChildren()
TimerSpeed.Value = 1
MultiplierVisible.Value = false
Multiplies.Value = 1
end
while true do
timer(Minutes.Value * 60 + Seconds.Value)
end
I just read your above post. For the minute and second values in other methods, you could check: if TimerTag.Value == “00:00” or TimerTag.Value == “0:00” then. Or you could set the minute and timer values to 0 when the timer loop reaches 0 and then reset them later on. Whichever way is easiest for you. If you go with the first method, the minutes and seconds variables can be removed.
Hi, thanks for the extra info, I have tested it and found out that the time on the timer isn’t showing due to the script not having a way to show the timer on its gui.