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

How to detect player if player is 5 studs away?

Simple, I have a killer npc and I want him to kill any player within 5 stud radius, how do I do it without making a big hitbox part?

1 answer

✓ Accepted answer

There's a thing called magnitude. An example would be:

local distance = Vector3.new(0,10,0)
local distance2 = Vector3.new(0,-10,0)
local magnitude = (distance - distance2).magnitude

print(magnitude)

This would print "20" in the output.

For a more in-depth explanation view the Roblox Wiki.

Anyway, magnitude is used to find the distance between one point to the other.

In your case, you want to find the distance between a player and an enemy.

I won't write an entire script for AI but just use your current code and find a way to put this in:

local magnitude = (enemyName.Position - characterDetected.Position).magnitude
print(magnitude) -- you can delete this if you want

if magnitude <= 5 then
	characterDetected.Humanoid.Health = 0
end

I hope this helps!

6 comments

0

Additional info, magnitude is not only used to get the distance between two vectors, but rather gets the length of a vector.

0

Subtracting 2 vectors creates a new vector from vector 1 (part1's position) to vector 2  (part2's position).

0

Using magnitude gets the length of the new vector, thus getting the distance.

0

Thank you, T3! I actually didn't know that... it's always nice to know more! Thank you!

0

The problem is that my entity has no pathfinding ai it just runs trought the whole map so i dont have anything that can detect player

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