So basically I’m making a tower defense game but using moveto is really laggy and unoptimized
local ServerStorage = game:GetService("ServerStorage")
local PhysicsService = game:GetService("PhysicsService")
local Events = game.ReplicatedStorage.Events
local TakeDamage = Events.TakeDamage
local mob = {}
function mob.Move(mob, map)
local humanoid = mob:WaitForChild("Humanoid")
local waypoints = map.Waypoints
for waypoint=1, #waypoints:GetChildren() do
mob.MovingTo.Value = waypoint
repeat
humanoid:MoveTo(waypoints[waypoint].Position)
until humanoid.MoveToFinished:Wait()
end
mob:Destroy()
if not (mob.Name == "Arrow") then
map.Base.Humanoid:TakeDamage(humanoid.Health)
TakeDamage:FireAllClients()
end
end
function mob.Spawn(name, quantity, map)
local mobExists = ServerStorage.Mobs:FindFirstChild(name)
if mobExists then
for i=1, quantity do
wait(0.5)
local newMob = mobExists:Clone()
newMob.HumanoidRootPart.CFrame = map.Start.CFrame
newMob.Parent = workspace.Mobs
newMob.HumanoidRootPart:SetNetworkOwner(nil)
local movingTo = Instance.new("IntValue")
movingTo.Name = "MovingTo"
movingTo.Parent = newMob
for i, object in ipairs(newMob:GetDescendants()) do
if object:IsA("BasePart") then
object.CollisionGroup = "Mob"
end
end
newMob.Humanoid.Died:Connect(function()
local DeathScript = newMob:FindFirstChild("Death")
if not DeathScript then
task.wait(0.01)
newMob:Destroy()
elseif DeathScript then
newMob.HumanoidRootPart.Anchored = true
task.wait(4)
newMob:Destroy()
end
end)
coroutine.wrap(mob.Move)(newMob, map)
end
else
warn("Requested mob does not exist", name)
end
end
return mob```