Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by
chomboghai 2044 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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 trueor 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.

Ad

Answer this question