Hi, for my game I have non-humanoid NPCs. To move them on the server I call a remote tween it and once the tween time has passed I move it to the waypoint on the server. Is there a better way to go about this or make it less laggy?
----------- Client ------------
tweenRemote.OnClientEvent:Connect(function(self,tweenTime,position)
if self._running then
tween = TweenService:Create(self.Npc.PrimaryPart,TweenInfo.new(tweenTime,Enum.EasingStyle.Linear),{Position = position})
local lookAt = CFrame.lookAt(self.Npc.PrimaryPart.Position,Vector3.new(position.X,self.Npc.PrimaryPart.Position.Y,position.Z))
turningtween = TweenService:Create(self.Npc.PrimaryPart,TweenInfo.new(0.2,Enum.EasingStyle.Linear),{CFrame = lookAt})
tween:Play()
turningtween:Play()
elseif not self._running then
if tween then
tween:Cancel()
end
if turningtween then
turningtween:Cancel()
end
end
end)
---------------------Server---------------
connection = RunService.Heartbeat:Connect(function()
if self._running then
if nextWaypointIndex <= #waypoints then
if not tween then
tween = true
oldPosition = self.Npc.PrimaryPart.Position
waitTime = (self.Npc.PrimaryPart.Position - waypoints[nextWaypointIndex].Position).Magnitude/self.walkspeed
tweenRemote:FireAllClients(self,waitTime,waypoints[nextWaypointIndex].Position + Vector3.new(0,self.Npc.PrimaryPart.Size.Y/2))
lastTime = os.clock() + waitTime
end
if lastTime - os.clock() <= 0 then
if tween then
local lookAt = CFrame.lookAt(oldPosition,Vector3.new(waypoints[nextWaypointIndex].Position.X,self.Npc.PrimaryPart.Position.Y,waypoints[nextWaypointIndex].Position.Z))
self.Npc.PrimaryPart.CFrame = CFrame.new(waypoints[nextWaypointIndex].Position + Vector3.new(0,self.Npc.PrimaryPart.Size.Y/2)) * lookAt.Rotation
nextWaypointIndex += 1
tween = false
end
end
else
connection:Disconnect()
blockedConnection:Disconnect()
end
else
connection:Disconnect()
blockedConnection:Disconnect()
end
end)