Beginner Scripting Problem

Hey why does one work and the other doesn’t?

local KillBrick = script.Parent


-- This one does not work
if KillBrick.Touched then
	function balls (touchPart)
		
		local humanoid = touchPart.Parent:FindFirstChild("Humanoid")

		if humanoid then
			humanoid.Health = 0
		end
		
	end
end



-- This one works as intended
KillBrick.Touched:Connect(function(touchPart)

	local humanoid = touchPart.Parent:FindFirstChild("Humanoid")

	if humanoid then
		humanoid.Health = 0
	end

end)

This is because that if statement would only run once when the script first runs. The Touched event, however, fires multiple times when something is touching the part.

1 Like

Because if KillBrick.Touched only runs once (I think, not sure if Killbrick.Touched even fires).

Killbrick.Touched:Connect runs each time the KillBrick is touched.

It’s because you are creating the function “balls()” inside of a touched event, and never calling/firing the function.

You can always use the function method if you would like, but what’s preferable is the 2nd method, as it’s easier to access and a function is not needed for any of them.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.