Hi,
I am trying to code a way to find out when a player is within a certain distance to a moving NPC so I can do something, I am also trying to show the distance on the NPC visibly by having a circle on the floor around the NPN…
thanks
Hi,
I am trying to code a way to find out when a player is within a certain distance to a moving NPC so I can do something, I am also trying to show the distance on the NPC visibly by having a circle on the floor around the NPN…
thanks
You’d have to use .RenderStepped or another render event to do this, as well as the .Magnitude property of Vector3s.
local runService = game:GetService('RunService')
local players = game:GetService('Players')
local localPlayer = players.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
local destinationNPC = workspace.NPC
local maxRadius = 10
runService.RenderStepped:Connect(function()
local npcPosition = destinationNPC.HumanoidRootPart.Position
local magnitude = (humanoidRootPart.Position - destPosition).Magnitude
if magnitude <= 10 then -- if the distance is 10 or less then
print(localPlayer, 'is', magnitude, 'studs away from the NPC!')
end
end)