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 5 years ago

Heres the script:

local Teleport = "Tele4" 

function Touch(hit) 
    if script.Parent.Locked == false and script.Parent.Parent:findFirstChild(Teleport).Locked == false then
         script.Parent.Locked = true 
        script.Parent.Parent:findFirstChild(Teleport).Locked = true

    local Pos = script.Parent.Parent:findFirstChild(Teleport)           
        hit.Parent:moveTo(Pos.Position) wait(1) script.Parent.Locked = false script.Parent.Parent:findFirstChild(Teleport).Locked = false

    end 
end 

script.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
5 years ago
Edited 5 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.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then

        --We got a player that touched
        local humanoid = hit.Parent:FindFirstChild("Humanoid")

        if humanoid.Health > 0 then
            --Rest of your code goes here
        else
            --If the player is dead then don't do anything
            return
        end
    end
end)
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 — 5y
0
Thanks to both of you I got it working the way I need SobbingSabah 6 — 5y
Ad

Answer this question