"How to return the function if the object that the script is looking is not in the part?"
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
end
Now, there is 4 problems here.
- The placement
- the
return statement
- bad practise
- you seemed to have made it already error in the first place ._.
The Placement:
Because 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).
The return Statement:
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 seemed to have made it already error in the first place ._.":
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.
Bad Practise:
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
else
return
end
end)
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
1 comment
what if i want to retuert the function if the humanoid doesnt exist?, it fills up the output bar and causes lag