I am trying to create a obby on roblox and I am having trouble with the code for if a player touches the block to kill that player. It would be some really boring lava jumps without this! thanks for the help!
Simple, so start by creating a part and a script and placing it under the part
First start by making a variable for the part
1 | Lava = script.Parent -- script.Parent will refer to the Part |
Now you want to create a function that kills a players
1 | function Kill(part) -- You use the keyword "function" to declare a function and the word after it is the name of the function and the word between the brackets is called the parameter, in this example it will refer to the object that touched the Lava |
Now to the killing part, but first we need to get the humanoid of the object or the torso/head to kill the player, I prefer getting the torso to do it.
1 | Player = part.Parent -- This will refer to the whole player model |
2 | Torso = Player.Torso -- This will refer to the player's Torso |
3 |
4 | Torso:Destroy() --This will kill the player |
5 |
6 | end --This is used to signify the end of the function |
Now we are done with what the lave is supposed to do, now to make it happen when the player touches the lava
1 | lava.Touched:connect(Kill) -- We use the .Touched event to call our function |
Overall the script is supposed to look like this
01 | Lava = script.Parent -- script.Parent will refer to the Part |
02 |
03 | function Kill(part) -- You use the keyword "function" to declare a function and the word after it is the name of the function and the word between the brackets is called the parameter, in this example it will refer to the object that touched the Lava |
04 |
05 | local Player = part.Parent -- This will refer to the whole player model |
06 | local Torso = Player.Torso -- This will refer to the player's Torso |
07 |
08 | Torso:Destroy() --This will kill the player |
09 |
10 | end --This is used to signify the end of the function |
11 |
12 | Lava.Touched:connect(Kill) -- We use the .Touched event to call our function |
I recommend you check out Roblox's tutorials so yoo can get better at scripting
This is to kill a player, put it inside the block.
1 | script.Parent.Touched:connect( function (hit) |
2 | if hit.Parent:FindFirstChild( "Humanoid" ) then --This is to check if the hit object is a player |
3 | hit.Parent:BreakJoints() --This kills him |
4 | end |
5 | end ) |
I mainly use this one, i'm sure this works cause i use this much
1 | script.Parent.Touched:connect( function (hit) -- If a player touches the brick it connects to the function |
2 | local h = hit.Parent:FindFirstChild( "Humanoid" ) -- Finds the humanoid |
3 | if h then -- Just a check if there is a humanoid (Will gives errors if you delete this) |
4 | if h.Health > 0 then -- Checks if the humanoid health is above 0 |
5 | h.Health = 0 -- Sets the health to 0 |
6 | end |
7 | end |
8 | end ) |
Hope this works for you :D, if it works accept this answere :P
1 | script.Parent.Touched:connect( function (hit) -- If a player touches the part it connects to the function |
2 | if hit and hit.Parent and hit.Parent:FindFirstChild( 'Humanoid' ) then |
3 | hit.Parent.Humanoid.Health = 0 |
4 | end ) |
5 |
6 | -- Very Simple |
1 | local trapPart = script.Parent |
2 | local function onPartTouch(otherPart) |
3 | local partParent = otherPart.Parent |
4 | local humanoid = partParent:FindFirstChildWhichIsA( "Humanoid" ) |
5 | if ( humanoid ) then |
6 | humanoid.Health = 0 |
7 | end |
8 | end |
9 | trapPart.Touched:Connect(onPartTouch) |
Just try this:
1 | function onTouch(part) |
2 | local humanoid = part.Parent:FindFirstChild( "Humanoid" ) |
3 | if (humanoid ~ = nil ) then -- if a humanoid exists, then |
4 | humanoid.Health = 0 -- damage the humanoid |
5 | end |
6 | end |
7 |
8 | script.Parent.Touched:Connect(onTouch) |
Put this in your part.
This can be simple, all you have to do is get a block you want to kill a player, put a script into it and paste this into the script :
local debounce = false local HealthLoss = 100 function OnTouched(Part) if Part.Parent ~= nil then if debounce == false and Part.Parent:findFirstChild("Humanoid") ~= nil then debounce = true Part.Parent:findFirstChild("Humanoid"):TakeDamage(HealthLoss) wait(2) debounce = false end end end script.Parent.Touched:connect(OnTouched)
This script will work, though if you want you can change the 100 to whatever you want to make sure the player is dead.
Here is the script I use for a kill block... place the script inside the part where you want the players to die from.
01 | --Ultronlize |
02 | local Brick = script.Parent |
03 |
04 |
05 |
06 | local function PlayerTouched(Part) |
07 | local Parent = Part.Parent |
08 | if game.Players:GetPlayerFromCharacter(Parent) then |
09 | Parent.Humanoid.Health = 0 --kills the player |
10 | end |
11 | end |
12 |
13 | Brick.Touched:connect(PlayerTouched) |
1 | local killbrick = script.Parent |
2 | killbrick.Touched:Connect( function (hit) |
3 | if hit.Parent.Head then -- Checking if it is a player |
4 | hit.Parent.Head:Destroy -- This can kill players that are in god mode or force field |
5 | -- |
6 | end |
7 | end ) |
Locked by youtubemasterWOW
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?