• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

TFS 1.2 How can i edit this code so it would spawn only one Boss

henkas

Well-Known Member
Joined
Jul 8, 2015
Messages
993
Solutions
5
Reaction score
55
Hello im using script that spawns boss every hour and it has 10% chance to be spawned every one hour but i want it to edit because it has one issue, how can i stop it from spawning that same boss if it was already spawned?

Lua:
local timeToRemove = 3600 * 1000 -- 1 hour 

local function removeBoss(uid)
    local monster = Monster(uid)
    if not monster then
        return
    end

    monster:remove()
    broadcastMessage("1 Hour has passed, and no one defeated the Daily Boss!", MESSAGE_EVENT_ADVANCE)
end

function onThink(interval)
    local state = Game.getGameState()
    if state == GAME_STATE_CLOSED or state == GAME_STATE_CLOSING then
        return true
    end

    local rand = math.random(1, 100)
    if rand <= 10 then
        local creature = monsterSystem[math.random(#monsterSystem)]
        local monster = Game.createMonster(creature.name, creature.pos)
        if monster then
            broadcastMessage(creature.msg, MESSAGE_EVENT_ADVANCE)

            Creature(monster):registerEvent("AutoEvent")
            addEvent(removeBoss, timeToRemove, monster:getId())
        end
        return true
    end
    return true
end
 
Solution
Alright, this should do what you want. The only problem with it is: if all bosses are spawned it will do an infinite loop (I stopped by by only allowing the loop to go through (boss amount * 2 times.) which is a bad work around in my opinion. If you can think of a better way go for it.

Lua:
local areas = {
    [1] = {chanceSpawn = 35,
        bosses = {
            {"Prisoner 1", Position(1654, 106, 7)},
            {"Prisoner 2", Position(1661, 106, 7)},
            {"Prisoner 3", Position(1661, 118, 7)},
            {"Prisoner 4", Position(1654, 118, 7)}
        }
    }
   
}

local BOSSES_SPAWNED = {}

for i = 1, #areas do
    BOSSES_SPAWNED[i] = {}
end

function onThink(interval)
    for i = 1, #areas do
        if math.random(100)...
I think this is what your looking for?

1) Spawns a random boss in one of the areas
2) Removes that boss from being able to be spawned again
3) Removes the boss after 1 hour if not killed.

NOTE There is multiple areas set up for different parts of the map. So if you have more than 1 area of bosses set up like this. The bosses are linked to the area. They are not all linked together.

Lua:
local areas = {
    [1] = {chanceSpawn = 100, -- 100% chance to spawn one of the bosses
        bosses = {
            {"Name", Position(1000, 1000, 7)},
            {"Name", Position(1000, 1000, 7)}
        }
    },
    [2] = {chanceSpawn = 35,
        bosses = {
            {"Name", Position(1000, 1000, 7)},
            {"Name", Position(1000, 1000, 7)},
            {"Name", Position(1000, 1000, 7)}
        }
    }
  
}

local possibleBosses = {
    [1] = {1, 2}, -- Should be the same as the amount of bosses in each area. (4 bosses = 1, 2, 3, 4)
    [2] = {1, 2, 3}
}

function onThink(interval)
    for i = 1, #areas do
        if math.random(100) <= areas[i].chanceSpawn then

        local POSSIBLEBOSS = possibleBosses[i]
      
            if #POSSIBLEBOSS > 0 then
                local BOSSID = math.random(#POSSIBLEBOSS)
                POSSIBLEBOSS[BOSSID] = nil
                local BOSS = areas[i].bosses[BOSSID]
              
                if BOSS then
                    local MONS = Game.createMonster(BOSS[1], BOSS[2])
                    addEvent(removeBoss, 60 * 60 * 1000, MONS:getId())
                end
            end
        end
    end
    return true
end

function removeBoss(id)
    local monster = Monster(id)
    if monster then
        monster:remove()
    end
end
Thanks i will test it out. I just gonna remove the feature that removes the boss after 1 hour. Gonna let you know how it works, if it works fine will mark your comment as a solution
 
Okay i did some testing so it has two issues code looks like this now. The issues are for some reason it spawned 3, Prisoner 1 but Prisoner 2, 3, 4 was just fine it spawned just one how it should because its already spawned. Then i killed Prisoner 2 and after killing it, it doesnt spawned it again even tho it spawns the function
Lua:
<globalevent name="Spawn Prisoner" interval="70" script="spawnprisoner.lua" />
Lua:
local areas = {
    [1] = {chanceSpawn = 35,
        bosses = {
            {"Prisoner 1", Position(1654, 106, 7)},
            {"Prisoner 2", Position(1661, 106, 7)},
            {"Prisoner 3", Position(1661, 118, 7)},
            {"Prisoner 4", Position(1654, 118, 7)}
        }
    }
   
}

local possibleBosses = {
    [1] = {1, 2, 3, 4} -- Should be the same as the amount of bosses in each area. (4 bosses = 1, 2, 3, 4)
}

function onThink(interval)
    for i = 1, #areas do
        if math.random(100) <= areas[i].chanceSpawn then

        local POSSIBLEBOSS = possibleBosses[i]
       
            if #POSSIBLEBOSS > 0 then
                local BOSSID = math.random(#POSSIBLEBOSS)
                POSSIBLEBOSS[BOSSID] = nil
                local BOSS = areas[i].bosses[BOSSID]
               
                if BOSS then
                    local MONS = Game.createMonster(BOSS[1], BOSS[2])
                end
            end
        end
    end
    return true
end
 
You are doing a terrible job of explaining what you need.... If you don't kill him he wont spawn again, but if you do he will spawn again? Is that what your saying? Explain what you need man clearly.
 
You are doing a terrible job of explaining what you need.... If you don't kill him he wont spawn again, but if you do he will spawn again? Is that what your saying? Explain what you need man clearly.
I though im really clear, i dont know how you came up with idea that it shouldnt spawn the boss even when hes killed i even made it clear when i explained how it should work, because think about it, u kill all of the bosses and server doesnt reload in like 1 month, so basically no one would be able to kill bosses anymore because it wont spawn them because they were killed long time ago, it just simple common sense i think. You can see in that image i never said that it shouldnt spawned them i said dont spawn them until they are not killed or he disappears for some reason (lets say admin removes it) so it means they should be spawned again when they are killed, i hope i made it clear now

hmm.png
 
Alright, this should do what you want. The only problem with it is: if all bosses are spawned it will do an infinite loop (I stopped by by only allowing the loop to go through (boss amount * 2 times.) which is a bad work around in my opinion. If you can think of a better way go for it.

Lua:
local areas = {
    [1] = {chanceSpawn = 35,
        bosses = {
            {"Prisoner 1", Position(1654, 106, 7)},
            {"Prisoner 2", Position(1661, 106, 7)},
            {"Prisoner 3", Position(1661, 118, 7)},
            {"Prisoner 4", Position(1654, 118, 7)}
        }
    }
   
}

local BOSSES_SPAWNED = {}

for i = 1, #areas do
    BOSSES_SPAWNED[i] = {}
end

function onThink(interval)
    for i = 1, #areas do
        if math.random(100) <= areas[i].chanceSpawn then
            
            local loops = #areas[i].bosses * 2
            while loops > 0 do
                local BOSS_RAND = math.random(#areas[i].bosses)
                local BOSS_PICKED = areas[i].bosses[BOSS_RAND]
                
                if not BOSSES_SPAWNED[i][BOSS_RAND] then
                    local MONS = Game.createMonster(BOSS_PICKED[1], BOSS_PICKED[2])
                    BOSSES_SPAWNED[i][BOSS_RAND] = MONS:getId()
                    addEvent(removeBoss, 60 * 60 * 1000, MONS:getId())
                    break
                end
                
                local monster = Monster(BOSSES_SPAWNED[i][BOSS_RAND])
                
                if not monster then
                    local MONS = Game.createMonster(BOSS_PICKED[1], BOSS_PICKED[2])
                    BOSSES_SPAWNED[i][BOSS_RAND] = MONS:getId()
                    addEvent(removeBoss, 60 * 60 * 1000, MONS:getId())
                    break
                end
                
                loops = loops - 1
            end
        end
    end
    return true
end

function removeBoss(id)
    local monster = Monster(id)
    if monster then
        monster:remove()
    end
end
Post automatically merged:

Just edited the code, make sure you get the newest one
 
Last edited:
Solution
Alright, this should do what you want. The only problem with it is: if all bosses are spawned it will do an infinite loop (I stopped by by only allowing the loop to go through (boss amount * 2 times.) which is a bad work around in my opinion. If you can think of a better way go for it.

Lua:
local areas = {
    [1] = {chanceSpawn = 35,
        bosses = {
            {"Prisoner 1", Position(1654, 106, 7)},
            {"Prisoner 2", Position(1661, 106, 7)},
            {"Prisoner 3", Position(1661, 118, 7)},
            {"Prisoner 4", Position(1654, 118, 7)}
        }
    }

}

local BOSSES_SPAWNED = {}

for i = 1, #areas do
    BOSSES_SPAWNED[i] = {}
end

function onThink(interval)
    for i = 1, #areas do
        if math.random(100) <= areas[i].chanceSpawn then
         
            local loops = #areas[i].bosses * 2
            while loops > 0 do
                local BOSS_RAND = math.random(#areas[i].bosses)
                local BOSS_PICKED = areas[i].bosses[BOSS_RAND]
             
                if not BOSSES_SPAWNED[i][BOSS_RAND] then
                    local MONS = Game.createMonster(BOSS_PICKED[1], BOSS_PICKED[2])
                    BOSSES_SPAWNED[i][BOSS_RAND] = MONS:getId()
                    break
                end
             
                local monster = Monster(BOSSES_SPAWNED[i][BOSS_RAND])
             
                if not monster then
                    local MONS = Game.createMonster(BOSS_PICKED[1], BOSS_PICKED[2])
                    BOSSES_SPAWNED[i][BOSS_RAND] = MONS:getId()
                    break
                end
             
                loops = loops - 1
            end
        end
    end
    return true
end
Aight thanks will test it out

Edit - works like a charm
 
Last edited:
Back
Top