Right now I have a timer system for each induvial player. The timer system involves a GUI on the players screen.
When the player dies from the line Player.Character.Humanoid.Health = 0, their local timer is set to -1:59. I just am unsure on why it is being set to that value. I would appreciate some help, here is a video on what is happening.
Once the player touches a part, a remote event will fire and trigger a timer (I didn’t show this in the video). But once the player dies, the timer is set to -1:59 when instead it should just reset.
Here is the timer code which is a local script in StarterPlayerScripts:
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("OneMinuteTimer")
local remoteEvent2 = ReplicatedStorage:WaitForChild("EndTimer")
local Checkpoint = workspace:WaitForChild("Checkpoints")
local lastCheckpointTouched = nil
local function check()
local currentCheckpointName = lastCheckpointTouched.Name
local nextCheckpointName = tostring(tonumber(currentCheckpointName) + 1)
local nextCheckpoint = game.Workspace.Checkpoints:FindFirstChild(nextCheckpointName)
if nextCheckpoint then
Player.Character:MoveTo(nextCheckpoint.Position + Vector3.new(0, 1, 0))
end
end
local function remove()
for _, tool in ipairs(Player.Backpack:GetChildren()) do
if tool:IsA("Tool") then
tool:Destroy()
end
end
if Player.Character:FindFirstChildOfClass("Tool") then
Player.Character:FindFirstChildOfClass("Tool"):Destroy()
end
end
local clonedgui = Player.PlayerGui:WaitForChild("Timer")
local minutes, seconds = 0, 0
Started = false
local mode = false
local red = Color3.fromRGB(231, 101, 101)
local red2 = Color3.fromRGB(126, 69, 69)
local white = Color3.fromRGB(255,255,255)
local green1 = Color3.fromRGB(190, 231, 178)
local green2 = Color3.fromRGB(127, 198, 89)
local timersound = game.ReplicatedStorage.TimerSound
function StartTimer()
while minutes >= 0 do
if seconds <= 0 then
minutes = minutes - 1
seconds = 59
else
seconds = seconds - 1
end
if seconds < 10 then
clonedgui.Frame.TextLabel.Text = tostring(minutes)..":0"..tostring(seconds)
else
clonedgui.Frame.TextLabel.Text = tostring(minutes)..":"..tostring(seconds)
end
if minutes <= 0 and seconds <= 10 and seconds >= 9.5 then
clonedgui.Frame.TextLabel.TextColor3 = red
clonedgui.Frame.TextLabel.UIStroke.Color = red2
timersound:Play()
end
if minutes <= 0 and seconds <= 0 then
if mode == false then
print(mode)
remove()
check()
else
print(mode)
Player.Character.Humanoid.Health = 0
end
end
wait(1)
Player.CharacterAdded:Connect(ResetTimer)
end
end
function ResetTimer()
if not Started then
Started = true
task.spawn(StartTimer)
else
minutes, seconds = minutes, seconds
end
end
remoteEvent.OnClientEvent:Connect(function(checkpointPart)
if checkpointPart and checkpointPart:IsA("BasePart") then
if lastCheckpointTouched ~= checkpointPart then
lastCheckpointTouched = checkpointPart
print(lastCheckpointTouched.TimeValue.Value)
if lastCheckpointTouched.TimeValue.Value == 1 then
minutes, seconds = 1, 0
elseif lastCheckpointTouched.TimeValue.Value == 2 then
minutes, seconds = 0, 45
elseif lastCheckpointTouched.TimeValue.Value == 3 then
minutes, seconds = 0, 30
elseif lastCheckpointTouched.TimeValue.Value == 0 then
minutes, seconds = 0, 0
else
print("Hi - Unknown Checkpoint Value")
end
if lastCheckpointTouched.Mode.Value == true then
mode = true
else
mode = false
end
if clonedgui.Frame.TextLabel.TextColor3 == red and clonedgui.Frame.TextLabel.UIStroke.Color == red2 then
clonedgui.Frame.TextLabel.TextColor3 = green1
clonedgui.Frame.TextLabel.UIStroke.Color = green2
end
ResetTimer()
end
end
end)
I also should mention that the part the player steps on to activate the timer has a BoolValue called Mode and a IntValue called TimeValue parented to it.