danielheringer
New Member
- Joined
- Sep 26, 2023
- Messages
- 4
- Reaction score
- 0
I'm trying to create a script that generates some loot boxes on the map in pre-determined or random pixels, so that the player who uses it gets some pre-defined rewards. The reward system works, but I can't make the boxes appear on the map automatically when the server starts. , I also tried to generate a recharge time so that the boxes only appear again 15 minutes after being used. Can someone help to make works?
LUA:
local rewards = {
{itemId = 3035, count = 1},
{itemId = 3035, count = 3},
{itemId = 3035, count = 5},
{itemId = 3035, count = 7},
{itemId = 3035, count = 10},
{itemId = 3035, count = 12},
}
local GIFT_ITEM_AID = 60002
local GIFT_BAG_ITEMID = 39546
local COOLDOWN_STORAGE = 900
local GIFT_BAG_POSITIONS = {
{x = 1068, y = 1104, z = 7},
{x = 1067, y = 1104, z = 7},
{x = 1066, y = 1104, z = 7},
{x = 1065, y = 1104, z = 7},
}
local function getRandomReward()
local rewardIndex = math.random(1, #rewards)
return rewards[rewardIndex]
end
local function giveRewardToPlayer(player, reward)
local itemId = reward.itemId
local itemCount = reward.count
player:addItem(itemId, itemCount)
end
local function createGiftBags()
for _, pos in pairs(GIFT_BAG_POSITIONS) do
local bagItem = doCreateItem(GIFT_BAG_ITEMID, 1, pos)
if bagItem then
bagItem:setActionId(GIFT_ITEM_AID)
end
end
end
local function useGift(player, item)
local currentTime = os.time()
local lastUsedTime = player:getStorageValue(COOLDOWN_STORAGE)
if lastUsedTime == 0 or currentTime - lastUsedTime >= 900 then
local reward = getRandomReward()
giveRewardToPlayer(player, reward)
player:sendTextMessage(MESSAGE_INFO_DESCR, "Você abriu o presente e recebeu uma recompensa!")
player:setStorageValue(COOLDOWN_STORAGE, currentTime)
item:remove(1)
return true
else
player:sendTextMessage(MESSAGE_INFO_DESCR, "Você deve esperar antes de abrir outro presente.")
return false
end
end
local giftAction = Action()
function giftAction.onUse(player, item, fromPos, target, toPos, isHotkey)
return useGift(player, item)
end
-- Chame a função para criar as bolsas no carregamento do servidor
createGiftBags()
giftAction:aid(GIFT_ITEM_AID)
giftAction:register()