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?
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.
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
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.