Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do i fix this moveto problem, it s very annoying ?

Asked by 3 years ago

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

0
could you be more specific about what you're trying to do? I'm confused by the wording. barrettr500 53 — 3y

1 answer

Log in to vote
1
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

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).

1
Thank you so much, you have no idea how much scripting knowledge i got from your response, i just had to know what to use and i learned about spawn(), delay() and coroutines from devhub and youtube, now i`m even more advanced thank you again bro i wish you the best ! :D Hate_ZEU 47 — 3y
Ad

Answer this question