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

Solved Dungeon System Charges. TFS 1.5/ OTclient mehah

eardrums

Member
Joined
May 27, 2010
Messages
94
Solutions
1
Reaction score
10
Hey guys,

I have been trying to create my own dungeon script and I have gotten pretty far. my issue is with adding dungeon charges that regenerate over time. in the script ive said the charges to regenerate every 15 seconds just for the purposes of testing but nothing seems to make it work correctly the charges regeneration is all messy and it gives me a random amount of charges every time the 15 seconds end. here is the script. can anyone help me in fixing the charges system while also using storage values.

Lua:
-- Constants
local CHARGE_REGEN_INTERVAL = 15
local MAX_CHARGES = 5
local STORAGE_KEY = 30048 -- Replace with your desired storage key

-- Utility function to format time
function formatTime(seconds)
    local hours = math.floor(seconds / 3600)
    local minutes = math.floor((seconds % 3600) / 60)
    local remainingSeconds = seconds % 60

    local formattedTime = ""
    
    if hours > 0 then
        formattedTime = formattedTime .. hours .. " hour(s) "
    end

    if minutes > 0 then
        formattedTime = formattedTime .. minutes .. " minute(s) "
    end

    formattedTime = formattedTime .. remainingSeconds .. " second(s)"
    
    return formattedTime
end

-- Charge regeneration function
function regenerateCharges()
    for _, player in ipairs(Game.getPlayers()) do
        if player then
            local chargesValue = player:getStorageValue(STORAGE_KEY)
            if chargesValue < MAX_CHARGES then
                player:setStorageValue(STORAGE_KEY, chargesValue + 1)
            end
        end
    end

    addEvent(regenerateCharges, CHARGE_REGEN_INTERVAL * 1000) -- Schedule the next regeneration event
end

-- Start the initial charge regeneration
addEvent(regenerateCharges, CHARGE_REGEN_INTERVAL * 1000) -- Schedule the first regeneration event

-- Main function triggered when using the item
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.uid == 1103 then
        local chargesValue = player:getStorageValue(STORAGE_KEY)

        if chargesValue > 0 then
            local centerX, centerY, centerZ = 1064, 1582, 7 -- Center of the spawn area
            local radius = 2 -- Radius of the spawn area
            local numMonsters = 1
            local monstersSpawned = 0
            
            for _ = 1, numMonsters do
                local offsetX = math.random(-radius, radius)
                local offsetY = math.random(-radius, radius)
                local spawnPosition = Position(centerX + offsetX, centerY + offsetY, centerZ)
        
                local monster = Game.createMonster("troll", spawnPosition)
                if monster then
                    monstersSpawned = monstersSpawned + 1
                end
            end
            
            local teleportPosition = Position(1067, 1584, 7)
            player:teleportTo(teleportPosition)
            player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have summoned 5 monsters around the area.")
            
            player:setStorageValue(STORAGE_KEY, chargesValue - 1)
            
            local timeUntilNextRegen = CHARGE_REGEN_INTERVAL - (os.time() % CHARGE_REGEN_INTERVAL)
            local chargeNotification = "You have " .. math.max(chargesValue - 1, 0) .. " charge(s) left."
            
            if chargesValue == 1 then
                chargeNotification = "You have 0 charge(s) left. Next charge in: " .. formatTime(timeUntilNextRegen)
            else
                chargeNotification = chargeNotification .. " Next charge in: " .. formatTime(timeUntilNextRegen)
            end
            
            player:sendTextMessage(MESSAGE_INFO_DESCR, chargeNotification)
        else
            local timeUntilNextRegen = CHARGE_REGEN_INTERVAL - (os.time() % CHARGE_REGEN_INTERVAL)
            local chargeNotification = "You have 0 charge(s) left. Next charge in: " .. formatTime(timeUntilNextRegen)
            player:sendTextMessage(MESSAGE_INFO_DESCR, chargeNotification)
        end
    end
    return true
end
Post automatically merged:

well I managed to make the script work myself. feel free to use it if you want guys: enjoy :D

Lua:
-- Constants             
local CHARGE_REGEN_INTERVAL = 10800   -- time in seconds, and 10800 is 3 hours --
local MAX_CHARGES = 5
local STORAGE_KEY = 30048 -- Replace with your desired storage key

-- Utility function to format time
function formatTime(seconds)
    local hours = math.floor(seconds / 3600)
    local minutes = math.floor((seconds % 3600) / 60)
    local remainingSeconds = seconds % 60

    local formattedTime = ""
    
    if hours > 0 then
        formattedTime = formattedTime .. hours .. " hour(s) "
    end

    if minutes > 0 then
        formattedTime = formattedTime .. minutes .. " minute(s) "
    end

    if seconds > 0 or (hours == 0 and minutes == 0) then
        formattedTime = formattedTime .. remainingSeconds .. " second(s)"
    end
    
    return formattedTime
end

-- Charge regeneration function
function regenerateCharges()
    for _, player in ipairs(Game.getPlayers()) do
        if player then
            local chargesValue = player:getStorageValue(STORAGE_KEY)
            local lastRegenTime = player:getStorageValue("LastChargeRegenTime")
            local currentTime = os.time()
            local timeSinceLastRegen = currentTime - lastRegenTime

            if timeSinceLastRegen >= CHARGE_REGEN_INTERVAL and chargesValue < MAX_CHARGES then
                player:setStorageValue(STORAGE_KEY, chargesValue + 1)
                player:setStorageValue("LastChargeRegenTime", currentTime)
            end
        end
    end

    addEvent(regenerateCharges, 1000) -- Check for regen every second
end

-- Start the initial charge regeneration
addEvent(regenerateCharges, 1000) -- Schedule the first regeneration check

-- Main function triggered when using the item
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.uid == 1103 then
        local chargesValue = player:getStorageValue(STORAGE_KEY)
        local currentTime = os.time()
        local lastRegenTime = player:getStorageValue("LastChargeRegenTime")
        local timeUntilNextRegen = lastRegenTime + CHARGE_REGEN_INTERVAL - currentTime

        if chargesValue > 0 then
            local centerX, centerY, centerZ = 1064, 1582, 7 -- Center of the spawn area
            local radius = 2 -- Radius of the spawn area
            local numMonsters = 1
            local monstersSpawned = 0
            
            for _ = 1, numMonsters do
                local offsetX = math.random(-radius, radius)
                local offsetY = math.random(-radius, radius)
                local spawnPosition = Position(centerX + offsetX, centerY + offsetY, centerZ)
        
                local monster = Game.createMonster("troll", spawnPosition)
                if monster then
                    monstersSpawned = monstersSpawned + 1
                end
            end
            
            local teleportPosition = Position(1067, 1584, 7)
            player:teleportTo(teleportPosition)
            player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have summoned 5 monsters around the area.")
            
            player:setStorageValue(STORAGE_KEY, chargesValue - 1)
            
            local chargeNotification = "You have " .. math.max(chargesValue - 1, 0) .. " charge(s) left. Next charge in: " .. formatTime(timeUntilNextRegen)
            player:sendTextMessage(MESSAGE_INFO_DESCR, chargeNotification)
        else
            local chargeNotification = "You have 0 charge(s) left. Next charge in: " .. formatTime(timeUntilNextRegen)
            player:sendTextMessage(MESSAGE_INFO_DESCR, chargeNotification)
        end
    end
    return true
end
 
Last edited:
Back
Top