I made a script to kill players that touch lava parts in an obby I'm making. However, using the .Touched event seems to have a glitch. Is there any working alternative that would be fast and efficient? Script:
01 | local CS = game:GetService( "CollectionService" ) |
02 | local Players = game:GetService( "Players" ) |
03 |
04 | function OnHumanoidTouched(otherpart, limb) |
05 |
06 | local player = Players:GetPlayerFromCharacter(limb.Parent) |
07 |
08 | if CS:HasTag(otherpart, "Checkpoint" ) then |
09 | --some checkpoint stuff |
10 |
11 | elseif CS:HasTag(otherpart, "KillBrick" ) then |
12 | player.Character:WaitForChild( "Humanoid" ):TakeDamage( 100 ) |
13 |
14 | end |
15 | end |
Thanks!
You should defiently be checking if the lava part itself is touched rather than checking if the humanoid is touched. As well, there is a more efficient way to set up your .Touched event by getting all of the tagged objects using CollectionService and setting up the connection from there.
As for your jumping issue, this doesn't seem to be a problem in the code I provided below. If it still is an issue, there might be some other code in your game that is causing this problem to occur. GIF can be found here
Try something like this:
01 | local CS = game:GetService( "CollectionService" ) |
02 | local Players = game:GetService( "Players" ) |
03 |
04 | local DAMAGE_AMOUNT = 100 |
05 |
06 | local function killPlayer(Hit) |
07 | -- Let's make sure its an actual player that hit the KillBrick. |
08 | if Players:GetPlayerFromCharacter(Hit.Parent) then |
09 | local Humanoid = Hit.Parent:FindFirstChild( "Humanoid" ) |
10 | if Humanoid then |
11 | Humanoid:TakeDamage(DAMAGE_AMOUNT) |
12 | end |
13 | end |
14 | end |
15 |
Theres a simpler way to do this, i think what your doing i may not be right but youve made a scipt in the player so when the player touches a lava brick they die, instead what you should do is making a simple script in the lava brick so when a person touches the brick the brick finds a humanoid and deals damage to it.
heres a simple script to do what i explained:
1 | script.Parent.Touched:Connect( function (hit) |
2 | if hit.Parent:findFirstChild( "Humanoid" ) then |
3 | local humanoid = hit.Parent.Humanoid |
4 | local damage = math.huge |
5 | humanoid:TakeDamage(damage) |
6 | end |
7 | end ) |
just paste this script inside a lava brick. what its doing it when it gets touches it looks for a humanoid in the object that touched it. if it finds the object it deals damage to it.
hope this helped.