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

How to detect player if player is 5 studs away?

Asked by 1 year ago

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

Log in to vote
1
Answered by 1 year ago

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!

0
Additional info, magnitude is not only used to get the distance between two vectors, but rather gets the length of a vector. T3_MasterGamer 2189 — 1y
0
Subtracting 2 vectors creates a new vector from vector 1 (part1's position) to vector 2  (part2's position). T3_MasterGamer 2189 — 1y
0
Using magnitude gets the length of the new vector, thus getting the distance. T3_MasterGamer 2189 — 1y
View all comments (2 more)
0
Thank you, T3! I actually didn't know that... it's always nice to know more! Thank you! LikeableEmmec 470 — 1y
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 kubapro213748 -13 — 1y
Ad

Answer this question