Below is a short code that I scripted. It gets the distance between the NPC's torso and the player torsos. If it is less than 5, it will print "We're within 5 studs." When I looked at the torso's position while I was walking, I noticed it wasn't changing, is there anything else besides position that I can use to check if the player is within 5 studs of the NPC?
This is in a local script in StarterPlayerScripts.
--Variables
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local torso = character:WaitForChild("Torso")
local npc = game.Workspace.NPC or game.Workspace:WaitForChild("NPC")
function getDistance(distance1, distance2)
local distance = (distance2 - distance1).magnitude
print(distance)
return distance
end
local distance = getDistance(torso.Position, npc.Torso.Position)
while wait(1) do
print(distance)
if distance <= 5 then
print("We're within 5 studs.")
end
end

1 comment
THanks