Iterar os jogadores online, insertando-os numa tabela para depois pegar um elemento aleatório dela, na minha opinião, é desnecessário, visto que você poderia somente usar:
local players = getPlayersOnline() local winner = players[math.random(#players)]
Mas, de resto, o código tá legal, especialmente o segundo. xD




info
Grupo: Conde
Registrado: 07/04/15
Posts: 962
Reputação: +258
Gênero: Masculino
O evento se resume a cada x horário configurável, é escolhido dentre os player online, 1 pessoa e ela recebe uma surprise bag (com items aleatórios para ganhar também configurável).
Globalevents/scripts ---> lottery.lua
local config = { lottery_hour = "1 Hours", -- after how many hours should lottery begin, explains itself really... reward_count = 4, -- How much items/rewards? so you want 4 random items then write 4... website = 1 -- Doesn't need explanation } function onThink(interval, lastExecution) local players = getPlayersOnline() local list = {} for i, tid in ipairs(players) do list[i] = 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('[LOTTERY SYSTEM] Winner: '.. getCreatureName(winner) ..', Reward: Suprise Bag' ..'! - Congratulations! (Next Lottery in '.. config.lottery_hour ..')') doPlayerAddItem(winner, 6571,config.reward_count) return TRUE endglobalevents.xml
Actions/scripts --> surprisebag.lua
-- CREATED BY GHETTOBIRD --! local PRESENT_BLUE = {2494, 2472} -- Add more items if you want just seperate them with an item id... local PRESENT_RED = {2160, 2514} -- same as above function onUse(cid, item, fromPosition, itemEx, toPosition) local count = 1 if(item.itemid == 6570) then local randomChance = math.random(1, #PRESENT_BLUE) if(randomChance == 1) then count = 2 elseif(randomChance == 2) then count = 2 end doPlayerAddItem(cid, PRESENT_BLUE[randomChance], count) elseif(item.itemid == 6571) then local randomChance = math.random(1, #PRESENT_RED) if randomChance > 0 and randomChance < 4 then count = 2 end doPlayerAddItem(cid, PRESENT_RED[randomChance], count) end doSendMagicEffect(fromPosition, CONST_ME_GIFT_WRAPS) doRemoveItem(item.uid, 1) return true endactions.xml
Créditos : ghetobird
Editado Abril 11 por ScreaM