CFrame based NPC walking going crazy

Update: It actually faces towards where it’s going. But on some paths it goes to the 30th dimension!

I have had weird issues like this before with tweens.

I believe it has something to do with this code:

        tween:Play()
		tween.Completed:Wait()

And you are creating multiple tweens on one model within a for loop.

For whatever reason, I find that tween.Completed:Wait() doesn’t always work as a good wait. I recommend replacing it (temporarily) with like wait(10) so you can see precisely how the tweens are affecting the character’s position over time.

Also, it tweens keep acting up, you could always try CFrame.Lerp(), which is designed exactly for this

Edit:
I think I may see a potential reason why it’s acting up. After you try the temporary wait(10) I mentioned above, try switching the wait(10) back to tween.Completed:Wait() and then comment out the following code block (temporarily):

        local charPos = restockerNPC:GetPivot().Position
		local lookAt = goalPosition * Vector3.new(1,1,1)
		local lookTowards = CFrame.lookAt(charPos, lookAt)
		restockerNPC:PivotTo(lookTowards)
1 Like

I can use Body Movers on a Humanoid after setting this:

humanoid:ChangeState(Enum.HumanoidStateType.Physics)

But I don’t know about Animations…

That’s the very thing I’m trying to avoid, Physics.

Here’s a video

I tried using CFrame:Lerp() but It’s not linear like the tweens. Basically it stutters.

Try commenting out this code block, like I said in my earlier reply:

        local charPos = restockerNPC:GetPivot().Position
		local lookAt = goalPosition * Vector3.new(1,1,1)
		local lookTowards = CFrame.lookAt(charPos, lookAt)
		restockerNPC:PivotTo(lookTowards)

Our first goal is to determine precisely what code is causing the problem and as such, we need to be able to rule out snippets of it.

It worked! I just commented out the code block you mentioned and then I added a second vector3 to the CFrame variable. Updated Code:

local function restockerGoTo(pos)
	local path = pathfindingService:CreatePath({
		AgentRadius = 5;
		Costs = {
			DoorMass = math.huge
		}
	})
	path:ComputeAsync(restockerNPCHum.RootPart.Position, pos)

	for i, v in ipairs(path:GetWaypoints()) do
		local direction = (restockerNPCHum.RootPart.Position - v.Position) * Vector3.new(1, 0, 1)
		local calculatedTime = (direction.Magnitude/restockerNPCHum.WalkSpeed)
		local goalPosition = v.Position + Vector3.new(0, restockerNPCSize.Y/2, 0)
		local calculatedCFrame = CFrame.new(goalPosition, restockerNPCHum.RootPart.Position + direction) * CFrame.Angles(0, math.rad(180), 0)
		local tween = tweenService:Create(restockerNPCHum.RootPart, TweenInfo.new(calculatedTime, Enum.EasingStyle.Linear), {CFrame = calculatedCFrame})
		tween:Play()
		wait(calculatedTime)
		--local charPos = restockerNPC:GetPivot().Position
		--local lookAt = goalPosition * Vector3.new(1,0,1)
		--local lookTowards = CFrame.lookAt(charPos, lookAt)
		--restockerNPC:PivotTo(lookTowards)
	end
end

Note:
I used * CFrame.Angles(0,math.rad(180),0) because if do restockerNPCHum.RootPart.Position - direction instead of restockerNPCHum.RootPart.Position + direction it goes to the -80th dimension.

2 Likes