Need help with detecting nearby objects

Hey guys,

recently I’ve been making a mesh deformation NPC, and I wanted to make it so that if it is near an object that is in a certain folder, it tweens itself to that object, sort of like tweening to the closest path point. Anyone know how I could manage to script this?

Thanks.

1 Like

Check these URL:

https://developer.roblox.com/en-us/api-reference/function/Humanoid/MoveTo

And to check of is near use something like this:

local part = script.Parent
local part2 = script.Parent
local magnitude = (part2.Position, part.Position).Magnitude

If the magnitude its a low number, that means that is near. If the magnitude its high number, that means that is far.

wait why would u want it to tween to the certain object when u could use path finding or the moveto function

1 Like

well, the reason I want to tween to different points is because my “NPC” is currently just a mesh with a humanoid (I should have been more specific), and the map it will be walking around has doors, which would make it unable to find a path using PathfindingService.

You can make an invisible NPC and weld the mesh to the humanoidRootPart.

I’ve tried this, but I don’t know how to get it to detect the nearest path point.

does ur NPC have a Humanoid Root Part, /torso
also is it in R6 or R15?

Currently not, unless the main mesh counts as the root part.
image
and I don’t think it’s either R6 or R15.

oh then i really dont know, but does it have a primary part?

Yup,
image

local PathfindingService = game:GetService(“PathfindingService”)
local char = the NPC
local humanoid = char.Humanoid
local destination = EndPartToGo
local path = PathfindingService:CreatePath()

path:ComputeAsync(char.HumanoidRootPart.Position, destination.Position)
local waypoints = path:GetWaypoints()

for _, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
humanoid:MoveTo(waypoint.Position)
end

he said he wanted it to be using tween service, but this is how u properly move an NPC

He can weld the mesh to a NPC or to tween the position to the waypoint.Position.

I think it’d be better to use an actual humanoid, but the script here will make the NPC stuck if there is a door obstructing it’s path.

path finding can detect if a door is in the path heres some waypoints for path finding i did a bit ago:
image

How can pathfinding detect the door?

im not 100% sure but i think its cus the computer is detecting it

also the type that cant detect the door are those dumb NPCs who only use the move to function

put this script into a normal NPC and add a part into workspace named end part

local PathFindingService = game:GetService("PathfindingService") -- get path finding service
local humanoid = script.Parent.Humanoid
local HRP = script.Parent.HumanoidRootPart
local NPC = script.Parent
local path = PathFindingService:CreatePath()
path:ComputeAsync(HRP.Position, game.Workspace.EndPart.Position) -- computes the path
local waypoints = path:GetWaypoints() -- makes the waypoints(where the NPC walks to.)
for i,v in pairs(waypoints) do -- pair loop
	local waypoint = v
	local showpart = Instance.new("Part", workspace)
	showpart.Size = Vector3.new(1,1,1)
	showpart.Position = v.Position
	humanoid:MoveTo(v.Position) -- moves the NPC to the waypoings
	humanoid.MoveToFinished:Wait() -- waits for the NPC to finish walking to the waypoint
	print(i) -- prints the steps
end

I don’t know if it’s the best thing to do, but long ago I made something like this with Raycast, just send a ray to the front, back, left and right. Hopefully helps.