script.Parent.Touched:Connect(function(hit) if hit.Parent:WaitForChild("Humanoid") == nil then return else --script end
im trying to make the script only work if it collided with a part/model with a humanoid in it, the script work sbut i just want to clean up the output because I cant see some of the things that needed to be printed, it doesnt work even if i use "if not hit.Parent:WaitForChild("Humanoid")", please help me ty
if hit.Parent:WaitForChild("Humanoid") == nil then
what you want to do instead if this:
if hit.Parent:FindFirstChild("Humanoid") then
there are two things you mightve got confused by:
"== nil" this means you want the object to be non-existent in other words it means equal to nothing
also i wouldn't recommend using the :WaitForChild function because if youre trying to get the humanoid from something like a character, the humanoid will already exist so theres no point in waiting. Use the :FindFirstChild function instead because that will go directly into the parent and find your target object.
one last thing - if youre using a conditional and want the condition to be true you can just use
if "example" then
its the same as using "if example == true then"
Hope this helped!!
I think I see what you're doing here. Let's sort out the problem!
YOUR script:
script.Parent.Touched:Connect(function(hit) if hit.Parent:WaitForChild("Humanoid") == nil then return else -- script end
Now, there is 4 problems here.
return
statementBecause you've added the return to the first line, especially if the humanoid is false then the script will just stop there. You'll have to switch those 2 around (and I'll show you why in a moment).
Now, when using return
, you'll either need to end the code if a certain condition is met, or return a value that you'll use later on.
In this script, you're checking if it is nil. And since it finds it is nil it will STOP the script there, because you used return
with no values...That's not what you want I assume? To just stop the script there, assuming you want to do an else?
You never even implemented an "end)" after that first end, especially if you want to close the function. Closing the function means it will stop reading the function at that line.
What I mean by that is the == nil
part, that isn't needed. And you'll see why in a moment...
MY script:
local Part = script.Parent Part.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then -- Notice how I never needed to use == here? -- your script here else return end end) -- Notice how I actually implemented the "end)" I mentioned with the last problem?
You don't actually need to use '==' since you're not checking for more than one value (especially since you're just looking if it's nil). And I've swapped their places because if it was NIL, then it would run on that line and stop the function in its tracks, thus making the else statement error as it would be confused as to why it's there, because the IF statement ends at the return.
I hope this has assisted you into understanding why your script didn't work. If you need more clarification, please ask me in the comments bar under this answer. And don't forget, if this answer has helped you + answered your question, mark it as accepted! <3