Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I am wondering how I can make it so when you reach 0 health, you get kicked from the game?

I am very new to scripting and not really sure what to do, I'm making a game. I've already tried to use two tutorials but it was unsuccessful.

The attempt that I had tried was when Roblox went down for a little bit, so I lost my progress.

1 comment

0

Hey, I'm a horrible scripter but if you had 0 health as an event, so when that event is fired, it will activate the kick function on the player. I have no idea wether this would work or not lol.

2 answers

✓ Accepted answer

Here is a stable script! (Insert as a ServerScript in StarterCharactersScripts)

local humanoid = script.Parent.Humanoid
local player = script.Parent:GetPlayerFromCharacter()

humanoid.Died:Connect(function()
	player:Kick("You died!")
end)

Ok, now lets run over the code.

Line 1 - 2 - Referencing the humanoid and the player.

local humanoid = script.Parent.Humanoid -- Get's humanoid. (No need for waitforchild)
local player = script.Parent:GetPlayerFromCharacter()  -- Gets the player.

Line 4 - 6 --- Connects player death and kicks.

humanoid.Died:Connect(function() ---- Connects a function when the player dies
	player:Kick("You died!") --- Kicks player with the message "You died!"
end) --- Ends the function duh.

Enjoy the script :) Tell me if there are any errors.

5 comments

0

This answer is better than the other. Only issue is that you don't necessarily need a local script to do this.

0

You should edit your answer to say Server Script(yes, you can put those in StarterCharacterScripts) as an exploiter could remove this script

1

My bad. I meant to say ServerScript.

0

Line 2 on the first script would error. GetPlayerFromCharacter() must be called from the Players service and it must take an Instance as an argument.

0

like @BashCaster said..

I'll help you.

We can add a script to the StarterCharacterScripts (i'm really messy, but idk how it's called, i think it's in starterplayers) and add these lines of code:

This is my way of doing it: First, let's find the humanoid of the character

local humanoid = script.Parent:WaitForChild("Humanoid") -- We'll search for the humanoid if it exists.

Good, now that we found the humanoid, we can run a loopedy-loop untill a humanoid hits 0 hp. You can use a while wait() do but i think it's quite unnecessary. I'd rather run a Repeat.

repeat wait() until humanoid.Health <= 0 -- This line of code will repeat a wait() loop until the humanoid's health is 0 (or under in some cases.)

Now, you can add a optional wait(seconds) if you want the person to stay for seconds before being kicked, but you can also just remove it.

We have to find the Player now. We can use game.Players:GetPlayerFromCharacter(). The character must be defined inside the brackets of it. aka (script.Parent)

After we found the player, we can run a player:Kick("reason here") on them.

Hope i explained something and helped you.

8 comments

0

Thanks man, I appreciate it.

0

I tested it in my game and it kicked me!

0

Just a thing for future notice, I just figured it out, you need to have 'player = game.Players.....' for the player:Kick to work.

0

Thats what i mentioned.

4

It's bad practice to use a 'repeat wait() until' loop for something like this. Humanoids have an event called 'Humanoid.Died', which will fire whenever the Humanoid health reaches 0. https://developer.roblox.com/en-us/api-reference/event/Humanoid/HealthChanged

1

Oops, I linked the wrong page. Here's the right one: https://developer.roblox.com/en-us/api-reference/event/Humanoid/Died

0

^ Polling over events is a horrible idea

0

Events should always be used if there exists one, you can read more about why here https://devforum.roblox.com/t/avoiding-wait-and-why/244015

This discussion is preserved as it was. New answers, comments, and votes are closed.