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

if humanoid prints error if humanoid is not found?

Asked by 5 years ago

So the question is pretty weird, but let me explain. So in this basic script i need to check weather the colided object is a player or not so i use this. if hit.Parent.Humanoid then --code-- end

But if it hits something else it gives an error like: Humanoid is not a valid part of workspace. So in my efforts to counter this, i tried to put an else statemant in my code. if hit.Parent.Humanoid then --code-- else print("Not found") end

But it still gives the same error and never prints what i set it to print. I also tried else if but it gave the same result. Thanks for any help!

Bonus question: What is the easiest/most efficient way to make a part fly in the opposite direction of you.(Like a fireball for example)

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

There is a simple solution to your question. Use FindFirstChild() in your if statement, as it returns nil if humanoid isn't found.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then 
        print("Humanoid found, hit.Parent is a character")
    else
        print("Not a character")
    end
end)

Bonus question

The easiest way to make a part fly in the opposite direction of the way your character is facing is to use a bodyvelocity.

To make a part fly the way your character is facing, you would do

local bv = Instance.new("BodyVelocity",randomPart)
bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bv.Velocity = character.LowerTorso.CFrame.lookVector * 50 -- change 50 to change the speed of projectile

So to make it fly the opposite way, you would simply change 50 to -50.

local bv = Instance.new("BodyVelocity",randomPart)
bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bv.Velocity = character.LowerTorso.CFrame.lookVector * -50 -- change 50 to change the speed of projectile

Obviously you would need to identify what part the BodyVelocity is a child of and a variable for the character.

0
I may have misinterpreted the way you meant fly in the opposite direction, if I did let me know. MythicalShade 420 — 5y
0
That's exacly what i meant, thanks! tonije123 9 — 5y
0
Glad to help! MythicalShade 420 — 5y
Ad

Answer this question