Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

2 comments

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

0

Thanks to both of you I got it working the way I need

This discussion is preserved as it was. New answers, comments, and votes are closed.