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

Why does this OnTouched function not work properly?

I wrote a script to slowly damage a player when they stand on a brick. It does not work, and I get no errors. What's the problem?

part = script.Parent

function onTouch(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum then
		hum:WaitForChild("Health")
		while hum.Health ~= 0 do
		hum:TakeDamage(1)
		end
	end
end

part.Touched:connect(onTouch)

2 answers

✓ Accepted answer
hum:WaitForChild("Health")

Health is not a child. It's a property. Since the 'Health' child will never exist, this will wait forever.

You reference it fine the rest of the time, so just taking out that entire line should work.


You have no delay in the while loop, so it will essentially kill you instantly. Use the wait function.


Now it should run, but it might not work as expected because you don't have a debounce. Check out that wiki link for a very good tutorial on the subject.

1 comment

0

Yep, thanks! Dunno what I was thinking. +1

Try while hum.Health > 0 do

1 comment

0

Also, put a print statement inside your loop to see if it gets reached

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