Timer Script Not Functioning

LocalScript

local screenGui = script.Parent
local remoteEvent = game.ReplicatedStorage.Events.TimerEnd

local countdownLabel = script.Parent.TextLabel

local countdown = 15

local function updateCountdown()
	countdown = countdown - 1
	countdownLabel.Text = countdown
	if countdown <= 0 then
		remoteEvent:FireServer(game.Players.LocalPlayer)
	end
end

while true do
	updateCountdown()
	wait(1)
end

Script

local remoteEvent = game.ReplicatedStorage.Events.TimerEnd

function remoteEvent.OnServerEvent(player)
	local humanoid = player.Character:WaitForChild("Humanoid")
	humanoid:Die()
end

I’m trying to make a timer so that when the timer ends, it kills the player
The timer itself works, but it goes to the negatives, and it doesn’t kill the player

When you fire a Remote Event the player argument is sent automatically (so no need to put in the LocalPlayer)

:Die() isn’t a valid method of Humanoid (refer to docs Humanoid | Roblox Creator Documentation), you will instead want to set the health to 0

And the reason it goes negative is because you perform the check to see if it’s 0 after you remove 1 from the countdown

3 Likes

This is pretty much the answer great job

The only thing I argue is Die()

Die could work if you make it a function

local function = Die()

Then you’d need to write the function. I recommend a standard kill script I can write if you’d like

The “:” means it will pass the object itself as the first argument (self), in this case humanoid. Now :Die() is not a method of humanoid. With a function as you suggest it would look more like Die(humanoid)

Your right :man_facepalming:. It’s been a moment since I scripted

I’ll take your suggestions, thank you!

how would i make it stop at 0?

Instead of :die() use .Health = 0 and to stop it at 0 do

while true do
	updateCountdown()
	wait(1)

if countdown <= 0 then
     countdown = 0
     break
end
end 

Put the if statement above the deduction

local function updateCountdown()
    if countdown <= 0 then
        remoteEvent:FireServer()
    end
    countdown = countdown - 1
    countdownLabel.Text = countdown
end

i tried that and it still went into the negatives

so add a return under where you fire the remote

This isn’t halting the counter. This is checking if countdown is 0 or smaller, and if so, it’s firing your event. You could add a return, although I’d probably just change it to an if-else

I did add a follow up on adding a return to it, althought idealy it’d just be best to have the while loop in the actual function and break it.

Oh yea, my bad didn’t see. But yes, you’re right.

1 Like

I don’t know if anybody has said this yet, but you don’t need to fire the player as an argument. It is automatically the first argument on the server side.

LocalScript:

local screenGui = script.Parent
local remoteEvent = game.ReplicatedStorage.Events.TimerEnd

local countdownLabel = script.Parent.TextLabel

local countdown = 15

while true do
	countdown -= 1
	countdownLabel.Text = countdown
	if countdown <= 0 then
		remoteEvent:FireServer(game.Players.LocalPlayer)
		break
	end
	task.wait(1)
end

Script:

local remoteEvent = game.ReplicatedStorage.Events.TimerEnd

remoteEvent.OnServerEvent:Connect(function(player)
	local humanoid = player.Character:WaitForChild("Humanoid")
	humanoid.Health = 0
end)

I made something that works, but thanks for your help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.