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

Turret Does not get a different target after first target is not in sight?

Asked by
P_4rio 30
2 years ago
Edited 2 years ago

Making a tower defense game and for the life of me cannot figure out how to change the target. I know what I got to do (switch to the next target in the table over and over until you can find a target) but I have no clue how to do it. I have tried myself before and had no success from no changes to the tower outright not working.

local tower = script.Parent

local config = tower.Config

function playAni(ani)
    local Animation = tower.Animation:FindFirstChild(ani)
    tower.AnimationController.Animator:LoadAnimation(Animation):Play()
end

playAni("Idle")

local enabled = false

local tower = script.Parent

local config = tower.Config

while true do
    wait()

    if enabled == false then

        enabled = true

        local stun = false

        local target

        local EnemyList = {}

        for _,v in pairs(workspace.Map.EnemyList:GetChildren()) do
            if v:FindFirstChild("CurrentPoint") then
                EnemyList[#EnemyList + 1] = {Obj = v.CurrentPoint; Value = v.CurrentPoint.Value}
                table.sort(EnemyList, function(a, b)
                    return a.Value > b.Value
                end)

                target = EnemyList[1]
                local targetOBJ = target.Obj
                local targetVAL = target.Value

                if targetOBJ.Parent ~= nil then
                    local unit = targetOBJ.Parent.Zombie.Value
                    local dist = (unit.Root.Position - tower.Root.Position).magnitude
                    if dist < config.Range.Value and stun == false then
                        if unit:FindFirstChild("Hidden").Value == false or unit:FindFirstChild("Hidden").Value == true and config.Hidden.Value == true then
                            tower:SetPrimaryPartCFrame(CFrame.lookAt(tower.Root.Position, unit.Root.Position))
                            tower.FireSound:Play()
                            playAni("Fire")
                            unit:FindFirstChild("Health").Value = unit:FindFirstChild("Health").Value - config.Damage.Value
                            unit = targetOBJ.Parent.Zombie.Value
                            wait(config.Firerate.Value / 10)
                        end
                    end
                end
            end
        end
        enabled = false
    end
end

1 answer

Log in to vote
0
Answered by
P_4rio 30
2 years ago

I was just being dumb I actually just had to add an else to the if statement on line 46 to remove the first zombie on the table and re-sort it.

Ad

Answer this question