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

Best way to check if Explosion.Hit did not hit a actual player character?

Asked by 3 years ago
Edited 3 years ago

I was wondering what is the best way to determine if a explosion hit a character so I can differenciate if it hit a part or a person.

but I came up with using FindFirstAncestor(Player.Name) to check if it was a model or not

is there any other better way to see if it actually hit a character model

Because there's a chance that a players name could mess things up like for example if someones name was weld the script would stop there

if there was a part under weld as a child.

See what im sayin?

Rn the best way I found was to

Check if

:FindFirstAncestor is a Player.Name

Check if FindFirstAncestor is a model

check if FindFirstAncestor has a humanoid

anything else I should check for or any other better solutions?

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

This works:

local Players = game:GetService("Players")

explosion.Hit:Connect(function(hit, distance)
   if Players:GetPlayerFromCharacter(hit.Parent) then
      print("Hit character")
   end
end)

So, for this one, I'll use :GetPlayerFromCharacter() and hit.Parent is the character but if not, then it'll return nil and do nothing. This works because if it returns something that means it becomes true even though the function didn't return a bool.

Ad

Answer this question