I am making a tower defense game and I’m using align position and align orientation to move and rotate the enemies.
All goes well while running the game, but when playing I noticed a stutter when they reached the waypoints on the client.
-
Basically it stutters for about a second once reaching the waypoint and then continues to the next, but when switching to the server it is a smooth as butter.
-
I’ve tried setting the network ownership to the server but it didn’t seem to do anything.
-
searched a good bit but I can’t seem to find the same issue online.
Can anyone help me resolve the issue and also give me suggestions on making my code more performance-friendly?
– << Services >> –
local RS = game:GetService(“ReplicatedStorage”)
local RunS = game:GetService(“RunService”)– << Variables >> –
local baseHealth = RS[“Game Data”].BaseHealth
local events = RS.Events
local enemyFolder = workspace.Enemies– << Module >> –
local enemy = {}– << Configs >> –
local enemyCongifuration = {
– health, speed, rotation, damage
Ducky = {10, 3, 20, 2}
}– << Functions >> –
function enemy.Move(duck, root, map, folder, speed, rotation, damage, isReturningObj)
local waypoints = map.Waypointslocal alignPos = root:FindFirstChildOfClass(“AlignPosition”)
local rotPos = root:FindFirstChildOfClass(“AlignOrientation”)local node = root.Node
if waypoints then
for i = 1, #folder:GetChildren() - 1 dolocal endNode = waypoints[folder.Name][i].WaypointNode if root == nil then return end rotPos.Attachment1 = endNode alignPos.Attachment1 = endNode rotPos.Responsiveness = rotation alignPos.MaxVelocity = speed while RunS.Heartbeat:Wait() do if (node.WorldCFrame.Position - endNode.WorldCFrame.Position).Magnitude < 0.5 then break end end end local baseNode = map.Base.BasePart.BaseNode rotPos.Attachment1 = baseNode alignPos.Attachment1 = baseNode while RunS.Heartbeat:Wait() do if (node.WorldCFrame.Position - baseNode.WorldCFrame.Position).Magnitude < 0.5 then break end end if root == nil then return end baseHealth.Value = baseHealth.Value - damage isReturningObj.Value = true task.wait(2) if isReturningObj.Value == true then for i, v in ipairs(duck:GetDescendants()) do if v:IsA("MeshPart") then v.Material = Enum.Material.ForceField end end end local endPoint = map.EndPoints[math.random(1, #map.EndPoints:GetChildren())] rotPos.Attachment1 = endPoint.EndNode alignPos.Attachment1 = endPoint.EndNode alignPos.MaxVelocity *= 1.5 while RunS.Heartbeat:Wait() do if (node.WorldCFrame.Position - endPoint.EndNode.WorldCFrame.Position).Magnitude < 0.5 then break end end if root == nil then return end
else
warn(“No waypoints found.”)
end
duck:Destroy()
endfunction enemy.Spawn(name, quantity, map)
local enemyExists = RS.Enemies:FindFirstChild(name)if enemyExists then
–moves into workspace
for x = 1, quantity do
task.wait(1)
print(“spawned a duck”)
local folders = map.Waypoints:GetChildren()
local waypointFolder = folders[math.random(1, #folders)]local newEnemy = enemyExists:Clone() local enemyHealth = enemyCongifuration[newEnemy.Name][1] local speed = enemyCongifuration[newEnemy.Name][2] local rotation = enemyCongifuration[newEnemy.Name][3] local damage = enemyCongifuration[newEnemy.Name][4] local isReturning = false --if the enemy is running away then it will be immune to damage local enemyHealthObj local isReturningObj for i, v in pairs(newEnemy:GetChildren()) do if v:IsA("Configuration") then for i, x in pairs(v:GetChildren()) do if x:IsA("NumberValue") and x.Name == "Health" then x.Value = enemyHealth enemyHealthObj = x elseif x:IsA("BoolValue") and x.Name == "IsReturning" then x.Value = isReturning isReturningObj = x end end end end newEnemy.HumanoidRootPart.Position = waypointFolder:FindFirstChild("EnemySpawn").Position newEnemy.Parent = enemyFolder newEnemy.HumanoidRootPart:SetNetworkOwner(nil) enemyHealthObj.Changed:Connect(function(value) if value <= 0 then newEnemy:Destroy() end end) coroutine.wrap(function() enemy.Move(newEnemy, newEnemy.HumanoidRootPart, map, waypointFolder, speed, rotation, damage, isReturningObj) end)() end
else
warn("Enemy does not exist: " … name)
end
endreturn enemy