Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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.

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. maxximusking890 5 — 4y

2 answers

Log in to vote
3
Answered by
DrShockz 233 Moderation Voter
4 years ago
Edited 4 years ago

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.

0
This answer is better than the other. Only issue is that you don't necessarily need a local script to do this. DeceptiveCaster 3761 — 4y
0
You should edit your answer to say Server Script(yes, you can put those in StarterCharacterScripts) as an exploiter could remove this script Filipalla 504 — 4y
1
My bad. I meant to say ServerScript. DrShockz 233 — 4y
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. DeceptiveCaster 3761 — 4y
0
like @BashCaster said.. User#23252 26 — 4y
Ad
Log in to vote
0
Answered by
Qariter 110
4 years ago
Edited 4 years ago

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.

0
Thanks man, I appreciate it. PurpleTheOval 2 — 4y
0
I tested it in my game and it kicked me! PurpleTheOval 2 — 4y
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. PurpleTheOval 2 — 4y
0
Thats what i mentioned. Qariter 110 — 4y
View all comments (4 more)
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 GeneratedScript 740 — 4y
1
Oops, I linked the wrong page. Here's the right one: https://developer.roblox.com/en-us/api-reference/event/Humanoid/Died GeneratedScript 740 — 4y
0
^ Polling over events is a horrible idea DeceptiveCaster 3761 — 4y
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 Filipalla 504 — 4y

Answer this question