Still have questions? Join our Discord server and get real time help.
Log in to vote

How would I get the magnitude between an npc and a player?

Asked by 3 months ago

So I just created a script that opens a gui when the player comes close to a part. I just did the part for an initial test but I don't know how i'll adjust it for npc's. I was thinking to create a ray that detects if an npc is in it's vicinity but I don't know how to go about making that vicinity and I don't how to do it with multiple npc's at once.

LocalScript in StarterPack in a tool

local playergui = game.Players.LocalPlayer.PlayerGui
gui ="ScreenGui", playergui)
    image ="ImageLabel", gui)
    image.BackgroundColor3 =, 255, 255)
    image.BorderColor3 =, 42, 53)
    image.Position =, 0,0.423, 0)
    image.Size =, 100,0, 100)
    image.ImageColor3 =, 255, 255)

while wait(.01) do

local mag = (game.Workspace.Part.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
local playergui = game.Players.LocalPlayer.PlayerGui
local val = 10

if mag < val then
        gui.Parent = playergui
        gui.Parent = nil
Checking magnitude every 0.01 seconds is going to lag the crap out of your game, especially if you end up having more than one npc. AetherProgrammer 105 — 3mo
I answered bellow NinjaXDpro 22 — 3mo

1 answer

Log in to vote
Answered by 3 months ago
Edited 3 months ago

Create a folder in workspace and name it 'NPCs'

plr = game.Players.LocalPlayer
char = plr.Character or plr.CharacterAdded:Wait()

for i,v in pairs (game.Workspace.NPCs:GetChildren()) do
       torso = v.Torso
       local mag = (char.HumanoidRootPart.Position -torso.Position).magnitude

Answer this question