Hey, i got a question, what do i need to use in order to fix the script from down below. I want to spawn a dummy clone every 3 second but the for i,v pairs stops me and spawns another dummy only after the first one is done.
-- Services -- local ServerStorage = game:GetService("ServerStorage") local PathfindingService = game:GetService('PathfindingService') local replicatedStorage = game:GetService('ReplicatedStorage') -- Variables -- local IntValuesFolder = replicatedStorage:WaitForChild('IntValues') local EnemiesFolder = ServerStorage:WaitForChild('Enemies') local WaveNumber = IntValuesFolder:WaitForChild('Wave') -- Actual Coding -- while true do wait(3) local clonedEnemy = EnemiesFolder.Attacker:Clone() clonedEnemy.Parent = game.Workspace clonedEnemy.HumanoidRootPart.CFrame = game.Workspace.EnemySpawn.CFrame + Vector3.new(0,2,0) clonedEnemy.Humanoid.WalkSpeed = 5 local path = PathfindingService:CreatePath() path:ComputeAsync(clonedEnemy.HumanoidRootPart.Position, game.Workspace.EndLocation.Position) local wps = path:GetWaypoints() for i, wp in pairs(wps) do clonedEnemy.Humanoid:MoveTo(wp.Position) clonedEnemy.Humanoid.MoveToFinished:Wait(1) end end
Problem:
The reason it is taking longer than 3 seconds to spawn another enemy is because of this line:
clonedEnemy.Humanoid.MoveToFinished:Wait(1)
Explanation:
It is waiting for the spawned enemy to move to the target positions in the path, preventing the script from moving forward.
To fix this, there are two solutions that I use. This solution will solve your issue, although you may notice a slight delay in your enemy moving as spawn() has a builtin wait() into it before the code within it executes:
spawn(function() clonedEnemy.Humanoid.MoveToFinished:Wait(1) end)
The above code is about equal to this:
coroutine.resume(coroutine.create(function() wait() -- this doesn't need to be here, it is showing how spawn() works clonedEnemy.Humanoid.MoveToFinished:Wait(1) end))
Solution:
To solve your issue, I recommend using coroutine. This will allow your while loop to continue moving through the code at the same time your enemy is moving, without a mandatory delay.
Modified Code:
-- Services -- local ServerStorage = game:GetService("ServerStorage") local PathfindingService = game:GetService('PathfindingService') local replicatedStorage = game:GetService('ReplicatedStorage') -- Variables -- local IntValuesFolder = replicatedStorage:WaitForChild('IntValues') local EnemiesFolder = ServerStorage:WaitForChild('Enemies') local WaveNumber = IntValuesFolder:WaitForChild('Wave') -- Actual Coding -- while true do wait(3) local clonedEnemy = EnemiesFolder.Attacker:Clone() clonedEnemy.Parent = game.Workspace clonedEnemy.HumanoidRootPart.CFrame = game.Workspace.EnemySpawn.CFrame + Vector3.new(0,2,0) clonedEnemy.Humanoid.WalkSpeed = 5 local path = PathfindingService:CreatePath() path:ComputeAsync(clonedEnemy.HumanoidRootPart.Position, game.Workspace.EndLocation.Position) local wps = path:GetWaypoints() coroutine.resume(coroutine.create(function() -- added for i, wp in pairs(wps) do clonedEnemy.Humanoid:MoveTo(wp.Position) clonedEnemy.Humanoid.MoveToFinished:Wait(1) end end)) -- added end
Conclusion:
Any more questions you may have can be directed to me via Discord (phxntxsmic#2021)
.