How would i tween an NPC? I’ve tried changing the position of the HumanoidRootPart, but it only moves that
Thats because you can only move the NPC through the HumanoidRootPart by changing it’s CFrame
(Or NPC:SetPrimaryPartCFrame() ofc)
you should use use model:MoveTo() on NPC
1 Like
You can use
NPC.Humanoid:MoveTo(-- Example: game.Workspace.Part.Position)
or you can use pathfinding service
MoveTo uses animations and walkspeed, wayyy better then tweening
1 Like
As the above posts say, you should use MoveTo
on the NPC’s Humanoid. Make sure all the parts inside the NPC have had SetNetworkOwner used and put them to nil (to set the server as the owner).
I use this method to manage my new game’s workers. See the video below. Additionally, I’ve attached my Walker class that I use to pathfind my NPCs.
local pathfindingService = game:GetService("PathfindingService")
local Walker = {}
Walker.__index = Walker
function Walker.new()
local self = setmetatable({}, Walker)
return self
end
function Walker:pathfindTo(startPosition, endPosition, humanoid)
local agentParameters = {
AgentRadius = 2.5;
AgentHeight = 5;
AgentWalkableClimb = 2;
AgentCollisionGroupName = "NPC";
CollectionWeights = {};
MaterialWeights = { Water = math.huge };
AgentCanJump = false;
}
local path = pathfindingService:CreatePath(agentParameters)
path:ComputeAsync(startPosition, endPosition)
if (path.Status == Enum.PathStatus.Success) then
local waypoints = path:GetWaypoints()
for _, pathwayPoint in pairs(waypoints) do
self:loopWalkToPosition(humanoid, pathwayPoint.Position, 0)
end
else
print("Error finding path")
print(path.Status)
end
end
function Walker:loopWalkToPosition(humanoid, point, maxRetries)
local targetReached = false
local connection = nil
connection = humanoid.MoveToFinished:Connect(function()
targetReached = true
if (connection) then
connection:Disconnect()
connection = nil
end
end)
humanoid:MoveTo(point)
coroutine.wrap(function()
while not targetReached do
humanoid:MoveTo(point)
wait(6)
end
if (connection) then
connection:Disconnect()
connection = nil
end
end)()
repeat wait() until targetReached
end
return Walker
You would use it like so
local walker = Walker.new()
local humanoid = npc.Humanoid
walker:pathfindTo(Vector3.new(), Vector3.new(5,10,5), humanoid)
3 Likes