Jump to content

HELP ON AI BOSS SCRIPT


josedabela

Recommended Posts

Posted

Guys i have a boss script that uses slothbot and extra health and i want to make the boss jump is a player is in a high place can someone help?

 

-- server.lua
-- Sistema de bosses completo com loot, respawn correto, dano por ataque e animação controlada

local attackCooldown = {} -- cooldown de ataque por boss
local nextSpawn = {}      -- timestamp do próximo spawn permitido de cada boss

-- Função para spawnar o boss
local function spawnBoss(bossID)
    local boss = Config.Bosses[bossID]
    if not boss then return end

    -- Checa se o boss já existe
    if boss.ped and isElement(boss.ped) then return end

    local ped = exports.slothbot:spawnBot(
        boss.position[1], boss.position[2], boss.position[3], boss.position[4] or 0,
        boss.skin, 0, 0,
        nil, boss.weapon, "guard", nil
    )

    if ped and isElement(ped) then
        boss.ped = ped
        setElementInterior(ped, boss.interior)
        setElementDimension(ped, boss.dimension)
        setElementHealth(ped, boss.baseHealth)

        exports.extra_health:setElementExtraHealth(ped, boss.extraHealth)
        setElementData(ped,"extra_health",boss.extraHealth)
        setElementData(ped,"extra_health_current",boss.extraHealth)
        setElementData(ped,"boss",true)
        setElementData(ped,"boss:id",bossID)
        setElementData(ped,"boss:name",boss.name)
        setElementData(ped,"boss:reward",boss.reward)
        setElementData(ped,"boss:damage",boss.damage or 5)
        setElementData(ped,"boss:runAnimation",boss.runAnimation or false)

        -- 🔇 Muta o boss automaticamente
        triggerClientEvent("Boss_STFU", getRootElement(), ped)
    end
end

-- Spawn automático respeitando respawnTime
setTimer(function()
    local now = os.time()
    for bossID,boss in pairs(Config.Bosses) do
        if (not boss.ped or not isElement(boss.ped)) then
            if (not nextSpawn[bossID] or now >= nextSpawn[bossID]) then
                spawnBoss(bossID)
            end
        end
    end
end, 10000, 0)

-- Função de loot
local function dropBossLoot(boss, ped, killer)
    if not boss.reward or not boss.reward.items then return end
    if getElementType(killer) ~= "player" then return end

    local x,y,z = getElementPosition(ped)
    local marker = createMarker(x,y,z-1,"cylinder",2,255,0,0,25)
    local objeto = createObject(2969,x,y,z-0.9)
    setElementCollisionsEnabled(objeto,false)
    setObjectScale(objeto,1.5)

    addEventHandler("onMarkerHit", marker, function(hitElement)
        if getElementType(hitElement) ~= "player" then return end
        if isPedInVehicle(hitElement) then return end

        triggerClientEvent(hitElement,"addBox",hitElement,"Pressione 'E' para saquear!","info")

        bindKey(hitElement,"e","down",function(p)
            if not isElement(p) then return end
            unbindKey(p,"e","down")
            setPedAnimation(p,"BOMBER","BOM_Plant",2000,false,true,false,false)

            setTimer(function()
                if not isElement(p) then return end
                local itensLoot = {}
                local msg = "Não encontraste nada!"
                for _, loot in ipairs(boss.reward.items) do
                    local roll = math.random(100)
                    if roll <= (loot.chance or 100) then
                        local qtd = loot.amount or 1
                        table.insert(itensLoot,{itemID=loot.id or loot.itemID,quantidade=qtd})
                    end
                end

                for _, item in ipairs(itensLoot) do
                    exports["[arena]Inventario"]:giveItem(p,item.itemID,item.quantidade)
                end

                if #itensLoot > 0 then msg = "Você saqueou o boss!" end
                if isElement(objeto) then destroyElement(objeto) end
                if isElement(marker) then destroyElement(marker) end

                setPedAnimation(p,false)
                triggerClientEvent(p,"addBox",p,msg,#itensLoot>0 and "success" or "warning")
            end,2000,1)
        end)
    end)

    addEventHandler("onMarkerLeave", marker, function(hitElement)
        if getElementType(hitElement) ~= "player" then return end
        unbindKey(hitElement,"e","down")
    end)

    setTimer(function()
        if isElement(objeto) then destroyElement(objeto) end
        if isElement(marker) then destroyElement(marker) end
    end,3*60000,1)
end

-- Morte do boss
addEventHandler("onPedWasted", root, function(_, killer)
    local ped = source
    if not getElementData(ped,"boss") then return end

    local bossID = getElementData(ped,"boss:id")
    local boss = Config.Bosses[bossID]
    if not boss then return end

    -- Atualiza próximo spawn baseado no respawnTime da config
    nextSpawn[bossID] = os.time() + boss.respawnTime

    -- Recompensa em dinheiro
    if isElement(killer) and getElementType(killer) == "player" then
        local reward = boss.reward
        if reward.money then
            givePlayerMoney(killer,reward.money)
            outputChatBox("Recebeste $"..reward.money.." por matar "..boss.name.."!",killer,0,255,0)
        end

        -- 🔹 Aqui dispara XP pro sistema de XP
        triggerEvent("onBossWasted", root, killer, bossID)
    end

    -- Loot do boss
    dropBossLoot(boss,ped,killer)

    -- Limpa o boss
    if isElement(boss.ped) then destroyElement(boss.ped) end
    boss.ped = nil
end)

-- Limpeza ao reiniciar resource
addEventHandler("onResourceStop", resourceRoot, function()
    for _, boss in pairs(Config.Bosses) do
        if boss.ped and isElement(boss.ped) then
            destroyElement(boss.ped)
            boss.ped = nil
        end
    end
end)

Posted

That is a good direction, but there is one important detail:

The example on isLineOfSightClear is useful for obstacle detection / jump logic, yes. However, setPedControlState is a client function, and for server-side peds it is not automatically synchronized, so using it directly on a slothbot ped may look broken for other players.

So if this boss is fully server-side through slothbot, you may need:
- obstacle / height detection with isLineOfSightClear or processLineOfSight
- then trigger the jump from the ped syncer client, or handle it with a custom movement solution instead of only server-side control states

Also, if the goal is specifically “jump when the player is on a high place”, then line of sight alone may not be enough. You may also want to compare the player Z position with the boss Z position and only trigger jump when the target is above a certain height difference.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...