GUI Timer that kills player once it is up

I am trying to find a way to have a GUI timer kill the player once it expires. The current script is below. The code at the end successfuly prints the message, but it does not kill the player. Is there a way to fix this?

local timer = script.Parent
local minutes = 1  -- minutes
local seconds = 0   -- seconds


-- Function to update the timer text
local function updateTimer()
	if seconds <= 0 then
		if minutes > 0 then
			minutes = minutes - 1
			seconds = 59
		else
			-- When both minutes and seconds are 0, stop the timer
			minutes = 0
			seconds = 0
			timer.Text = "0:00"
			
			
			return
		end
	else
		seconds = seconds - 1
	end

	-- Formatting the timer display with a leading zero for seconds if needed
	if seconds < 10 then
		timer.Text = tostring(minutes)..":0"..tostring(seconds)
	else
		timer.Text = tostring(minutes)..":"..tostring(seconds)
	end
end

-- Repeat the update every second
while minutes > 0 or seconds > 0 do
	updateTimer()
	wait(1)
end

-- Final timer display when it reaches 0
timer.Text = "0:00"
-- Below is the area of focus
print ("Out of time")
local Players = game:GetService("Players")
Players.PlayerAdded:Wait() -- optional
local player = Players:GetPlayers()[math.random(1, #Players:GetPlayers())]

local human = player.Character and player.Character:WaitForChild("Humanoid")
human:TakeDamage(100)

This will wait for a player to join after the timer ends, isn’t needed.

This picks a random player, if you want to fetch the LocalPlayer you would do Players.LocalPlayer.

First of all, dont get a random player, get Players.LocalPlayer. also it might not be working because youre killing the player in a local script. Fire a remote event with the player.

local plr = game.Players.LocalPlayer

event:FireServer(plr)

Then on the server kill the player :slight_smile:

1 Like

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