#Enemies get stuck at path part #2

27 messages · Page 1 of 1 (latest)

umbral tide
#

not letting me send the script

#
local function playWalkingAnimation(enemy, animId)
    local humanoid = enemy:WaitForChild("Humanoid")
    if humanoid then 
        local animator = humanoid:WaitForChild("Animator")
        local walkAnimation = Instance.new("Animation")
        walkAnimation.AnimationId = "rbxassetid://" .. animId
        local walkAnimTrack = animator:LoadAnimation(walkAnimation)
        walkAnimTrack:Play()
    end
end

local function moveToNextPoint(enemy, currentpoint)
    local nextPoint = PathFolder:FindFirstChild(tostring(tonumber(currentpoint + 1)))
    if nextPoint then
        local humanoid = enemy:WaitForChild("Humanoid")
        if humanoid then
            humanoid:MoveTo(nextPoint.Position + Vector3.new(0,3,0))
            humanoid.MoveToFinished:Wait()
            moveToNextPoint(enemy, currentpoint)
        end
    else 
        enemy:Destroy()
    end
end

local function spawnEnemy(enemyType)
    local enemyInfo = EnemyConfig.Enemies[enemyType]
    local enemy = enemyInfo.Model:Clone()
    enemy.Parent = EnemiesFolder
    enemy:SetPrimaryPartCFrame(PathFolder ["1"].CFrame + Vector3.new(0,3,0))
    
    playWalkingAnimation(enemy, enemyInfo.WalkAnimation)
    
    moveToNextPoint(enemy, 1)
end

local function StartWave(waveNumber)
    local wave = WaveConfig.Config[waveNumber]
    if wave then 
        for _, enemyGroup in ipairs(wave.Enemies) do 
            for i = 1, enemyGroup.Count do
            task.delay(0, function()
                spawnEnemy(enemyGroup.Type)
            end)
                task.wait(wave.DelayBetweenSpawns)
            end
        end
    end
end

Players.PlayerAdded:Connect(function()
    task.wait(2)
    
    StartWave(1)
end) ```
#
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")

local PathFolder = workspace:WaitForChild("Path")
local EnemiesFolder = workspace:WaitForChild("Enemies")
local EnemyConfig = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("EnemyConfig"))
local WaveConfig = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("WaveConfig"))```
river gullBOT
#

studio** You are now Level 7! **studio

umbral tide
trim escarp
#
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")

local PathFolder = workspace:WaitForChild("Path")
local EnemiesFolder = workspace:WaitForChild("Enemies")
local EnemyConfig = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("EnemyConfig"))
local WaveConfig = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("WaveConfig"))

local animationCache = {}

local function playWalkingAnimation(enemy, animId)
    local humanoid = enemy:FindFirstChild("Humanoid")
    if humanoid then 
        local animator = humanoid:FindFirstChild("Animator")
        if animator then
            local walkAnimTrack
            if animationCache[animId] then
                walkAnimTrack = animator:LoadAnimation(animationCache[animId])
            else
                local walkAnimation = Instance.new("Animation")
                walkAnimation.AnimationId = "rbxassetid://" .. animId
                animationCache[animId] = walkAnimation
                walkAnimTrack = animator:LoadAnimation(walkAnimation)
            end
            walkAnimTrack:Play()
        else
            warn("Humanoid in enemy " .. enemy.Name .. " does not have an Animator.")
        end
    else
        warn("Enemy model " .. enemy.Name .. " does not have a Humanoid.")
    end
end

local function moveToNextPoint(enemy, startPoint)
    local humanoid = enemy:FindFirstChild("Humanoid")
    if not humanoid then 
        warn("Humanoid not found for enemy " .. enemy.Name)
        enemy:Destroy()
        return 
    end

    local currentPoint = startPoint

    while true do
        local nextPoint = PathFolder:FindFirstChild(tostring(currentPoint + 1))
        if nextPoint then
            humanoid:MoveTo(nextPoint.Position + Vector3.new(0,3,0))
            local reached = humanoid.MoveToFinished:Wait()
            if reached then
                currentPoint = currentPoint + 1
            else
                break
            end
        else
            enemy:Destroy()
            break
        end
    end
end

local function spawnEnemy(enemyType)
    local enemyInfo = EnemyConfig.Enemies[enemyType]
    if not enemyInfo then
        warn("EnemyConfig does not contain enemy type: " .. tostring(enemyType))
        return
    end

    local enemy = enemyInfo.Model:Clone()
    enemy.Parent = EnemiesFolder

    local startingPoint = PathFolder:FindFirstChild("1")
    if startingPoint then
        if enemy.PrimaryPart then
            enemy:SetPrimaryPartCFrame(startingPoint.CFrame + Vector3.new(0,3,0))
        else
            warn("Enemy model " .. enemy.Name .. " does not have a PrimaryPart set.")
            enemy:Destroy()
            return
        end
    else
        warn("PathFolder does not contain a starting point named '1'.")
        enemy:Destroy()
        return
    end

    playWalkingAnimation(enemy, enemyInfo.WalkAnimation)
    moveToNextPoint(enemy, 1)
end

local function StartWave(waveNumber)
    local wave = WaveConfig.Config[waveNumber]
    if wave then 
        for _, enemyGroup in ipairs(wave.Enemies) do 
            for i = 1, enemyGroup.Count do
                task.spawn(function()
                    spawnEnemy(enemyGroup.Type)
                end)
                task.wait(wave.DelayBetweenSpawns)
            end
        end
    else
        warn("WaveConfig does not contain a configuration for wave number " .. tostring(waveNumber))
    end
end

local waveStarted = false

Players.PlayerAdded:Connect(function()
    if not waveStarted then
        waveStarted = true
        task.wait(2)
        StartWave(1)
    end
end)
umbral tide
trim escarp
trim escarp
#

but for the stopping

#

the main issue was movetonextpoint

#

you used tonumber for the expression

#

meaning if the numbers were off track then everything fucked

#

tostring(tonumber(currentpoint + 1))

#

solves that issue as itll dynamically keep track per instance

#

local nextPoint = PathFolder:FindFirstChild(tostring(tonumber(currentpoint + 1))) is what you used

#

local nextPoint = PathFolder:FindFirstChild(tostring(currentpoint + 1)) is what it was changes too

#

the other issue was the recursive call in movetonextpoint did not increment currentpoint

#

moveToNextPoint(enemy, currentpoint) is wha tyou had

#

moveToNextPoint(enemy, currentpoint + 1) is what it was changes too

#

this way it could track the current point + going to the next one

#

these 3 things caused the traffic jam

#

as the AI might go from point 1 to 2

#

then get stuck at 2 because its looking for 2 again

#

vs looking for 3

#

make sense? @umbral tide

umbral tide
#

Yeah