Marco Oliveira
Well-Known Member
- Joined
- Jan 5, 2019
- Messages
- 76
- Solutions
- 3
- Reaction score
- 78
- Location
- Minas Gerais - Brazil
- GitHub
- omarcopires
- Twitch
- omarcopires
This script defines a global event named "lottery" using the function GlobalEvent("lottery"). The function lottery.onThink is called every interval of time and performs the following actions:
In data\lib\core\player.lua add:
- Gets a list of all players in the game.
- Creates a list of eligible players, excluding those who are administrators.
- If there are eligible players, randomly selects one of them as the lucky player.
- Sets a random prize between 1,000,000 and 3,000,000 gold coins.
- Adds the prize to the lucky player's bank balance.
- Sends a message to all players announcing the winner and the prize. The event is scheduled to occur every 6 hours using the method lottery:interval(6 * 60 * 60 * 1000), and is registered using lottery:register().
Lua:
local lottery = GlobalEvent("lottery")
function lottery.onThink(interval)
local players = Game.getPlayers()
local eligiblePlayers = {}
for i = 1, #players do
local player = players[i]
if not player:getGroup():getAccess() then -- check if player is not an admin
table.insert(eligiblePlayers, player)
end
end
if #eligiblePlayers > 0 then
local luckyPlayer = eligiblePlayers[math.random(#eligiblePlayers)]
local prize = math.random(1000000, 3000000)
luckyPlayer:addBankMoney(prize)
Game.broadcastMessage("[LOTTERY SYSTEM] Congratulations! " .. luckyPlayer:getName() .. " won " .. prize .. " gold coins in the lottery.", MESSAGE_EVENT_ADVANCE)
end
return true
end
lottery:interval(6 * 60 * 60 * 1000)
lottery:register()
In data\lib\core\player.lua add:
Lua:
function Player.addBankMoney(self, amount)
self:setBankBalance(self:getBankBalance() + amount)
end