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

Why does it not print "No CBP" when a part is touched?

I'm trying to do something with .Touched, where if there is an object called "CBP" it prints "CBP", but if there isn't an object with "CBP" it prints "No CBP". When I try it out in game it doesnt work. The damage portion works, but not the printing portion.

script.Parent.Touched:connect(function(hit)
	if hit.Parent:findFirstChild("Humanoid") then 
		hit.Parent.Humanoid:TakeDamage(1)
		if hit.Parent.HumanoidRootPart:FindFirstChild("CBP") == false then 
			print("No CBP")
	elseif hit.Parent.HumanoidRootPart:FindFirstChild("CBP") == true then 
			print("CBP")
		end
	end
end)

1 answer

✓ Accepted answer

The issue here is that you are checking to see if an instance is true or false.

The :FindFirstChild() method returns an instance. You can't check to see if an instance is true or false. You can, however, check to see if the object exists by doing this check:

local CBP = hit.Parent.HumanoidRootParent:FindFirstChild("CBP")
if CBP then -- if it exists
	print("CBP")
else -- does not exist
	print("No CBP")
end

In the above script, I am not checking whether it is true or false, but rather if it exists or is nil. If the FindFirstChild() method does not find an object, it returns nil, not false.

Hope this helps.

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