Hello, I have made a script that tweens the NPC to the next waypoint. But the problem is whenever I switch to the server view, or when a new player joins in-game, the zombies glitch until they reach the final waypoint and get destroyed (The new ones work). But how could I fix this problem? I am currently tweening the HumanoidRootPart on the server, and all of the parts of the zombies are Massless.
The code in a module script:
local uniqueSeed = Random.new()
task.spawn(function()
zombie:PivotTo(CFrame.new(workspace.Spawn.Position.X, workspace.Spawn.Position.Y + zombie:FindFirstChild("Height").Value, workspace.Spawn.Position.Z))
local hrp = zombie:FindFirstChild("HumanoidRootPart")
local ZOMBIE_OFFSET = uniqueSeed:NextNumber(-0.34, 0.34)
local WalkSpeed = zombie:FindFirstChild("WalkSpeed")
local Animator = zombie:WaitForChild("AnimationController"):WaitForChild("Animator")
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://14755070010"
anim.Parent = Animator
local animation = Animator:LoadAnimation(anim)
animation.Looped = true
animation:Play()
animation:AdjustSpeed(1 * WalkSpeed.Value/3.9)
for waypoint = 1, #waypoints:GetChildren() do
task.wait()
local Distance = (hrp.Position - waypoints[waypoint].Position).Magnitude
local cframeLookat = CFrame.lookAt(hrp.Position, Vector3.new(waypoints[waypoint].Position.X, hrp.Position.Y, waypoints[waypoint].Position.Z))
local rx, ry, rz = cframeLookat:ToOrientation()
local dx, dy, dz = math.deg(rx), math.deg(ry), math.deg(rz)
local orientation = Vector3.new(dx, dy, dz)
local Time = GetTime(Distance, WalkSpeed.Value)
local moveTween = TweenService:Create(hrp, TweenInfo.new(Time*1.25, Enum.EasingStyle.Linear), {Position = Vector3.new(waypoints[waypoint].Position.X + (ZOMBIE_OFFSET*2), hrp.Position.Y, waypoints[waypoint].Position.Z + (ZOMBIE_OFFSET*2))})
moveTween:Play()
local orientationTween = TweenService:Create(hrp, TweenInfo.new(Time/Distance*3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Orientation = orientation})
orientationTween:Play()
moveTween.Completed:Wait()
end
zombie:Destroy()
end)
I don’t exactly know what the problem is but it could be a serious issue in the future if a player joined late and saw this. ((This system has no Humanoids so don’t suggest anything with MoveTo() or Humanoid properties!!))