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

How do I make my script not execute if the players health is 0?

Asked by 6 years ago

Heres the script:

01local Teleport = "Tele4"
02 
03function Touch(hit)
04    if script.Parent.Locked == false and script.Parent.Parent:findFirstChild(Teleport).Locked == false then
05         script.Parent.Locked = true
06        script.Parent.Parent:findFirstChild(Teleport).Locked = true
07 
08    local Pos = script.Parent.Parent:findFirstChild(Teleport)          
09        hit.Parent:moveTo(Pos.Position) wait(1) script.Parent.Locked = false script.Parent.Parent:findFirstChild(Teleport).Locked = false
10 
11    end
12end
13 
14script.Parent.Touched:connect(Touch)

What I'm trying to do is make it if the players health is 0 (dead) then this script will not teleport them anywhere. Can someone help me with this?

1 answer

Log in to vote
0
Answered by
xPolarium 1388 Moderation Voter
6 years ago
Edited 6 years ago

Since Touched event provides you with the object that touched it, you can check if the parent of that part contains a Humanoid. You should always perform this check when you only want players to touch something. From there you can check the Health property of the humanoid is greater than 0.

01script.Parent.Touched:Connect(function(hit)
02    if hit.Parent:FindFirstChild("Humanoid") then
03 
04        --We got a player that touched
05        local humanoid = hit.Parent:FindFirstChild("Humanoid")
06 
07        if humanoid.Health > 0 then
08            --Rest of your code goes here
09        else
10            --If the player is dead then don't do anything
11            return
12        end
13    end
14end)
0
Since it's a void function, you could just do "if humanoid.Health > 0 then" because you're not returning anything, so the "else return" is unnecessary vanilla_wizard 336 — 6y
0
Thanks to both of you I got it working the way I need SobbingSabah 6 — 6y
Ad

Answer this question