• 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!

Can someone help me get this Lottery System script done?

nicoelperro

New Member
Joined
May 3, 2024
Messages
14
Reaction score
3
Helloo, so, i have this lottery system script, it works perfectly.
The thing is that i'd like it to be a player only thing, the staff also gets the reward, i'd like to change that
i tried but i couldn't figure it out :/ If someone can help me with this i'd be very thankful :)

local config = {
lottery_hora = "30 Minutos", -- Es la siguiente ronda!! Suerte
reward_count = 50, -- How many crystal coins get winner?
website = 0 -- Do you have lottery table in your database? (1 = yes / 0 = no)
}
function onThink(interval, lastExecution)
local players = getPlayersOnline()
local list = {}
for i, tid in ipairs(players) do
list = tid
end
local winner = list[math.random(1, #list)]
if(config.website == 1) then
db.executeQuery("INSERT INTO lottery (name) VALUES ('".. getCreatureName(winner) .."');")
end
doBroadcastMessage('[SISTEMA DE LOTERIA] Ganador: '.. getCreatureName(winner) ..', Recompensa: '.. config.reward_count ..'0k! - Felicitaciones! (siguiente Loteria en '.. config.lottery_hora ..')')
doPlayerAddItem(winner, 2152,config.reward_count)
return TRUE
end
 
Wrong board. This is not a tutorial...

Your script isn't working properly, you are looping through the players, and just overwriting the list table each time. Therefore only one player is stored in the table when it picks the winner (the last player in the loop)...

I've rewritten it, and because you didn't specify which server engine you are using, I have written the code for the latest TFS engine (1.6)

(untested)
LUA:
local config = {
    lottery_hora = 30, -- how often in minutes does the lottery run
    reward_count = 50, -- How many crystal coins does the winner get?
}

function onThink(interval, lastExecution)
    local entries = {}
  
    for _, player in ipairs(Game.getPlayers()) do
        if player:getGroup():getId() == 1 then
            table.insert(entries, player)
        end
    end
  
    if next(entries) == nil then
        return true
    end
  
    local winner = entries[math.random(#entries)]
    doBroadcastMessage(
        ('[SISTEMA DE LOTERIA] Ganador: %s, Recompensa: %d. 0k! - Felicitaciones! (siguiente Loteria en %d minutos)'):format(
            winner:getName(),
            config.reward_count,
            config.lottery_hora
        )
    )
      
    winner:addItem(2152, config.reward_count)
    return true
end
 
Last edited by a moderator:
Your script isn't working properly, you are looping through the players, and just overwriting the list table each time. Therefore only one player is stored in the table when it picks the winner (the last player in the loop)...

I've rewritten it, and because you didn't specify which server engine you are using, I have written the code for the latest TFS engine (1.6)

(untested)
LUA:
local config = {
    lottery_hora = 30, -- how often in minutes does the lottery run
    reward_count = 50, -- How many crystal coins does the winner get?
}

function onThink(interval, lastExecution)
    local entries = {}
   
    for _, player in ipairs(Game.getPlayers()) do
        if player:getAccessLevel() == 1 then
            table.insert(entries, player)
        end
    end
   
    if next(entries) == nil then
        return true
    end
   
    local winner = entries[math.random(#entries)]
    doBroadcastMessage(
        ('[SISTEMA DE LOTERIA] Ganador: %s, Recompensa: %d. 0k! - Felicitaciones! (siguiente Loteria en %d minutos)'):format(
            winner:getName(),
            config.reward_count,
            config.lottery_hora
        )
    )
       
    winner:addItem(2152, config.reward_count)
    return true
end
Did you just improved my script? o and you figured it out just by reading it
TFS is 1.3 or maybe 1.1, it's quite old but i'm not ready to lose my ot to switch to another TFS
Maybe you could help me again? thanks
 
Did you just improved my script? o and you figured it out just by reading it
TFS is 1.3 or maybe 1.1, it's quite old but i'm not ready to lose my ot to switch to another TFS
Maybe you could help me again? thanks
Yes, it's improved, and now works as intended.

The best thing to do is to implement the script, and change the interval in globalscripts.xml to 1 minute, just to test it.
Let me know if it throws any errors. I assume there will be some as older TFS versions won't have some of the new classes/methods that I have used.

This is a version using older functions, still untested, and im not sure if your server has the Player::getGroup() method...
LUA:
local config = {
    lottery_hora = 30,
    reward_count = 50,
}

function onThink(interval, lastExecution)
    local players = getPlayersOnline()
    local list = {}
    for i, tid in ipairs(players) do
        if tid:getGroup():getId() == 1 then --no idea what the old func is for this
            table.insert(list, tid)
        end
    end
    local winner = list[math.random(#list)]
    doBroadcastMessage('[SISTEMA DE LOTERIA] Ganador: '.. getCreatureName(winner) ..', Recompensa: '.. config.reward_count ..'0k! - Felicitaciones! (siguiente Loteria en '.. config.lottery_hora ..' Minutos)')
    doPlayerAddItem(winner, 2152, config.reward_count)
    return true
end
 
Last edited by a moderator:
Back
Top