I’m trying to make it so when the player goes on stage 1, the timer starts, and ends on stage 2 which will be changed later. That part works perfectly; however, I’m trying to make it so even if they die, the timer will continue until they’ve reached the marked point. My script isn’t working though.
Local Script in Text Label
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local leaderstatChangedEvent = ReplicatedStorage:WaitForChild("LeaderstatChanged")
local textLabel = script.Parent
local timer = 0
local running = false
local function UpdateTimer()
while running do
wait(0.01)
timer = timer + 0.01
local totalSeconds = math.floor(timer)
local milliseconds = math.floor((timer % 1) * 100)
local hours = math.floor(totalSeconds / 3600)
local minutes = math.floor((totalSeconds % 3600) / 60)
local seconds = totalSeconds % 60
textLabel.Text = string.format("%02d:%02d:%02d:%02d", hours, minutes, seconds, milliseconds)
end
end
leaderstatChangedEvent.OnClientEvent:Connect(function(newValue)
if newValue == 1 and not running then
running = true
UpdateTimer()
elseif newValue == 2 then
running = false
end
end)
Regular Script in ServerScriptService
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local leaderstatChangedEvent = ReplicatedStorage:FindFirstChild("LeaderstatChanged")
local function onLeaderstatChanged(player, newValue)
leaderstatChangedEvent:FireClient(player, newValue)
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local leaderstats = player:WaitForChild("leaderstats")
local stat = leaderstats:WaitForChild("Stage")
stat.Changed:Connect(function(newValue)
onLeaderstatChanged(player, newValue)
end)
end)
end)
Not working. Plus, the timer [numbervalue] keeps getting added when the player dies.
Updated Code [Local Script]:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local leaderstatChangedEvent = ReplicatedStorage:WaitForChild("LeaderstatChanged")
local textLabel = script.Parent
local timerValue = Instance.new("NumberValue")
timerValue.Name = "Timer"
timerValue.Value = 0
timerValue.Parent = player
local running = false
local function UpdateTimer()
while running do
wait(0.01)
timerValue.Value = timerValue.Value + 0.01
local totalSeconds = math.floor(timerValue.Value)
local milliseconds = math.floor((timerValue.Value % 1) * 100)
local hours = math.floor(totalSeconds / 3600)
local minutes = math.floor((totalSeconds % 3600) / 60)
local seconds = totalSeconds % 60
textLabel.Text = string.format("%02d:%02d:%02d:%02d", hours, minutes, seconds, milliseconds)
end
end
leaderstatChangedEvent.OnClientEvent:Connect(function(newValue)
if newValue == 1 and not running then
running = true
UpdateTimer()
elseif newValue == 2 then
running = false
end
end)
leaderstatChangedEvent.OnClientEvent:Connect(function(newValue)
if newValue == 1 and not running then
running = true
UpdateTimer()
elseif newValue == 2 then
running = false
end
end)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local textLabel = script.Parent
local timerValue = Instance.new("NumberValue")
timerValue.Name = "Timer"
timerValue.Value = 0
timerValue.Parent = player
local aliveStatus = Instance.new("BoolValue")
aliveStatus.Name = "IsAlive"
aliveStatus.Value = true
aliveStatus.Parent = player
local running = false
local function UpdateTimer()
while running do
wait(0.01)
timerValue.Value = timerValue.Value + 0.01
local totalSeconds = math.floor(timerValue.Value)
local milliseconds = math.floor((timerValue.Value % 1) * 100)
local hours = math.floor(totalSeconds / 3600)
local minutes = math.floor((totalSeconds % 3600) / 60)
local seconds = totalSeconds % 60
textLabel.Text = string.format("%02d:%02d:%02d:%02d", hours, minutes, seconds, milliseconds)
end
end
local function OnCharacterAdded(character)
aliveStatus.Value = true
end
local function OnCharacterRemoving(character)
aliveStatus.Value = false
end
local function AliveStatusChanged()
if aliveStatus.Value and not running then
running = true
UpdateTimer()
elseif not aliveStatus.Value then
running = false
end
end
player.CharacterAdded:Connect(OnCharacterAdded)
player.CharacterRemoving:Connect(OnCharacterRemoving)
aliveStatus:GetPropertyChangedSignal("Value"):Connect(AliveStatusChanged)
if player.Character then
OnCharacterAdded(player.Character)
else
aliveStatus.Value = false
end
I made it so “while aliveSTatus.Value == true do”, and I called the updatetimer function.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local textLabel = script.Parent
local timerValue = Instance.new("NumberValue")
timerValue.Name = "Timer"
timerValue.Value = 0
timerValue.Parent = player
local aliveStatus = Instance.new("BoolValue")
aliveStatus.Name = "IsAlive"
aliveStatus.Value = true
aliveStatus.Parent = player
local running = false
local function UpdateTimer()
while aliveStatus.Value == true do
wait(0.01)
timerValue.Value = timerValue.Value + 0.01
local totalSeconds = math.floor(timerValue.Value)
local milliseconds = math.floor((timerValue.Value % 1) * 100)
local hours = math.floor(totalSeconds / 3600)
local minutes = math.floor((totalSeconds % 3600) / 60)
local seconds = totalSeconds % 60
textLabel.Text = string.format("%02d:%02d:%02d:%02d", hours, minutes, seconds, milliseconds)
end
end
local function OnCharacterAdded(character)
aliveStatus.Value = true
UpdateTimer()
end
local function OnCharacterRemoving(character)
aliveStatus.Value = false
end
player.CharacterAdded:Connect(OnCharacterAdded)
player.CharacterRemoving:Connect(OnCharacterRemoving)
if player.Character then
OnCharacterAdded(player.Character)
else
aliveStatus.Value = false
end
Yeah you see work smarter not harder, that’s how I roll. I write systems which are short but no one else has the brains like me. Yeahhhh i’m bragging what are yall gonna do abt it?