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

if humanoid prints error if humanoid is not found?

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

✓ Accepted answer

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.

3 comments

0

I may have misinterpreted the way you meant fly in the opposite direction, if I did let me know.

0

That's exacly what i meant, thanks!

0

Glad to help!

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