How do you make a player die after time runs out?

The tutorial I watched taught how to make the player die if they don’t sit down within a time limit but how do I make it so the player will die if they don’t reach an area (this sphere) fast enough?


Basically if the character is not colliding with the big sphere they will die just as if they weren’t sitting in the chair.

Here’s the code for the timer:

local player = game.Players.LocalPlayer
local TimerEvent = game.ReplicatedStorage.TimerEvent
local ScreenGui = player.PlayerGui.ScreenGui

local stopTimer = false

TimerEvent.OnClientEvent:Connect(function(timer)
	ScreenGui.TimerFrame.TextLabel.Text = timer
	ScreenGui.TimerFrame.TextLabel.Visible = true
	wait(1)
	repeat
		timer = timer - 1
		ScreenGui.TimerFrame.TextLabel.Text = timer
		wait(1)
	until
	timer <= 0 or stopTimer == true
	ScreenGui.TimerFrame.TextLabel.Visible = false	
	stopTimer = false
end)

And here’s the code for the actual game + killing script:

local CreateDialogueEvent = game.ReplicatedStorage.CreateDialogueEvent
local TimerEvent = game.ReplicatedStorage.TimerEvent

-- Other Useful Functions

local randomPlayerName
local randomPlayerId

local function getPlayerImage(player_id)
	local content, isReady = game:GetService("Players"):GetUserThumbnailAsync(player_id, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
	return content
end

local function getRandomPlayer()
	local players = game.Players:getPlayers()
	local number = math.random(1,#players)
	local randomPlayer = players[number]
	randomPlayerName = randomPlayer.Name
	randomPlayerId = randomPlayer.UserId
end

----------------------------------------

-- put your challenge functions here

local function challeng_example() -- THIS IS JUST AN EXAMPLE! IT WON'T RUN
	local teacher_Image = "rbxassetid://4841171880"
	CreateDialogueEvent:FireAllClients(teacher_Image,"Hello Students!")
	wait(5)
	getRandomPlayer()
	CreateDialogueEvent:FireAllClients(getPlayerImage(randomPlayerId),"I think this is the School!")
end

local function challenge_intro()
	getRandomPlayer()
	CreateDialogueEvent:FireAllClients(getPlayerImage(randomPlayerId),"Does someone live here?")
end

local function challenge_1()
	
	local function checkIfPlayerSit()
		for i,v in pairs(game.Players:getPlayers()) do
			if v.Character then
				if v.Character.Humanoid.Sit ~= true then
					v.Character.Humanoid.Health = 0
				end
			end
		end
	end
	
	local mortimer_Image = "rbxassetid://7499399754"
	CreateDialogueEvent:FireAllClients(mortimer_Image,"I doubt it. We should go check it out.")
	wait (5)
	CreateDialogueEvent:FireAllClients(mortimer_Image,"Oh yeah, if you don't come with me you will fall apart and die.")
	TimerEvent:FireAllClients(20)
	wait (20)
	checkIfPlayerSit()
end

local function challenge_2()

end

local function challenge_3()

end

local function challenge_4()

end

local function challenge_ending()

end

--------------------------------------------------

local function startGame() -- This is your main function where we order the challenges
	challenge_intro()
	challenge_1()
	challenge_2()
	challenge_3()
	challenge_4()
	challenge_ending()
end

wait(10) -- waits 10 seconds
startGame() -- runs the startGame function
1 Like

I think that code is a little too complicated for me to understand but here’s a suggestion:
Give each player a true or false value (BoolValue), when they touch the area before the timer runs out, the value will turn true, because the value will obv be false unless they hit the certain area. When the timer does run out the remaining players that have a false value will die. Also, the Timer should be a seperate script with it’s own number value so that when another script checks the timer and it hits a certain number, then it will activate. Not sure if this makes sense but hope it gives you an idea. If you would like a more brief and proper explanation don’t be afraid to ask me. If you still want the Dialogue to fire you can find a way for it to work but thats all i can do. It made more sense in my head.

script.parent.Touched:connect(function(e)
if e.name == “Area” then
BoolValue.Value = true
etc

------------------ for timer

while true do
wait()
if timer.value == 0 and if boolvalue.value == false then
Player.Humanoid.health = 0

This is just an brief example of what I mean
hope this helps in any way or form

3 Likes

You could use this:
WorldRoot:GetPartBoundsInBox (roblox.com)
And make its size the same as your sphere, althought the hitbox would be a cube, or you could also use ZonePlus v3.2.0 | Construct dynamic zones and effectively determine players and parts within their boundaries - Resources / Community Resources - DevForum | Roblox

I tried this but it comes up with this error: Workspace.Mortimer192.TimerScript:25: Expected ‘end’ (to close ‘function’ at line 22), got ; did you forget to close ‘then’ at line 23?

This is the code:

local player = game.Players.LocalPlayer
local TimerEvent = game.ReplicatedStorage.TimerEvent
local ScreenGui = player.PlayerGui.ScreenGui
local BoolValue = false

local stopTimer = false

TimerEvent.OnClientEvent:Connect(function(timer)
	ScreenGui.TimerFrame.TextLabel.Text = timer
	ScreenGui.TimerFrame.TextLabel.Visible = true
	wait(1)
	repeat
		timer = timer - 1
		ScreenGui.TimerFrame.TextLabel.Text = timer
		wait(1)
	until
	timer <= 0 or stopTimer == true
	ScreenGui.TimerFrame.TextLabel.Visible = false	
	stopTimer = false
end)

script.parent.Touched:connect(function(e)
	if e.name == 'Area' then
		BoolValue.Value = true
	end

local player = game.Players.LocalPlayer
local TimerEvent = game.ReplicatedStorage.TimerEvent
local ScreenGui = player.PlayerGui.ScreenGui
local BoolValue = false

local stopTimer = false

TimerEvent.OnClientEvent:Connect(function(timer)
ScreenGui.TimerFrame.TextLabel.Text = timer
ScreenGui.TimerFrame.TextLabel.Visible = true
wait(1)
repeat
timer = timer - 1
ScreenGui.TimerFrame.TextLabel.Text = timer
wait(1)
until
timer <= 0 or stopTimer == true
ScreenGui.TimerFrame.TextLabel.Visible = false
stopTimer = false
end)

script.parent.Touched:connect(function(e)
if e.name == ‘Area’ then
BoolValue.Value = true
end
end)

Simple mistake you forgot the last end value

1 Like

now it says touched isn’t a valid member of workspace (in line 22)

Oops i didnt see the other error that you said.

if the script is inside a part make sure to capaitilze “Parent”

so it should be

script.Parent.Touched:connect(function(e)
if e.name == ‘Area’ then
BoolValue.Value = true
end
end)

1 Like

okay there are no errors now but how do I check that the bool is actually changing to true?

right after the line that makes the boolvalue true, you can do:

print(boolvalue.value)
so you can make sure it’s true, or it that’s not what you mean then i am not sure

1 Like

nothing appears in the output. I moved the bool script to a separate local script so it might be because there’s no trigger to make the script play in the first place?

local BoolValue = false

script.Parent.Touched:connect(function(e)
	if e.name == Area then
		BoolValue.Value = true
	end
end)

print(BoolValue.Value)

wait wait I thought u was using this:
image
Were you not? im not sure if what you’re doing will work

1 Like

where do i put the boolvalue? i tried putting it in the workspace, area and the local script but none of those did anything

what do you mean by this? do I put the code in the timer script or a new one?

You put the Bool value in each separate player

1 Like

by putting it in starter player?

Yes, more specifically, inside of StarterCharacterScripts, if you are still confuse you can allow me to check the game and fix it right up for you, if you are uncomfortable with that we can keep explain here.

1 Like

the output is still displaying nothing

I think that is a coding problem that you set wrong. If you allow me, I can go in and find the codes and fix them because it’ll be easier, if not, go back and find which script is suppose to print if the the BoolValue is true or not and change it.

1 Like

Two functions that check what has touched and touchended with the sphere. when a humanoidrootpart touches it, add the player to the list, same with removing. When time is out just go through each player not in the list and kill them.

doesn’t the humanoid root part have to touch the area? this is the code:

local BoolValue = false

script.Parent.Touched:connect(function(e)
	if e.name == Area then
		BoolValue.Value = true
	end
end)

print(BoolValue.Value)