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

Lua anyy idea why this code aint working correcly ?

Lbtg

Advanced OT User
Joined
Nov 22, 2008
Messages
2,398
Reaction score
165
So i got this sript not working correcly , can someone check it out and fix it :)

globalevent script

onlinegift.lua
PHP:
local config = {
    [1] = {minLevel = 80, maxLevel = 350, item = {2160,5}},
    [2] = {minLevel = 100, maxLevel = 1500, item = {12601,5}},
    [3] = {minLevel = 150, maxLevel = 1600, item = {11225,1}}
}
function FindSuitable(cid)
    local to_return = {}
    for i = 1,#config do
        if getPlayerLevel(cid) >= config[i].minLevel and getPlayerLevel(cid) <= config[i].maxLevel then
            table.insert(to_return,config[i].item[1])
            table.insert(to_return,config[i].item[2])
            break
        end
    end
    return to_return
end
function onThink(interval)
    local players = getPlayersOnline()
    if #players >= 1 then
        for i = 1,#players do
            local reward = FindSuitable(players[i])
            if #reward > 0 then
                doPlayerSetStorageValue(players[i],6722, tonumber(getPlayerStorageValue(players[i],6722)) + 1)
                if getPlayerStorageValue(players[i],6722) >= 60 then
                    doPlayerAddItem(players[i],reward[1],reward[2])
                    doPlayerSetStorageValue(players[i],6722,0)
                    doPlayerSendTextMessage(players[i], 27, "You Have Been Rewarded With "..reward[2].."x Of "..getItemNameById(reward[1])..".")
                end
            end
        end
    end
    return true
end

xml part
PHP:
<globalevent name="onlinegift" interval="1000" event="script" value="onlinegift.lua"/>
 
script aint working , no errors from 3 different scripts familiar to this same just rewards and lvls different all 3 scripts aint working just third one and just 1 gift from 3 ^^
 
Code:
local storage = 6722

local c = {
    [1] = {min = 80, max = 350, i = {2160, 5} },
    [2] = {min = 100, max = 1500, i = {12601, 5} },
    [3] = {min = 150, max = 1600, i = {11225, 1} }
}

-- no need to pass cid, we are just concerned with populating a table
function getRewards(lvl)
    local t = {}
    for x = 1, #c do
        if lvl >= c[x].min and lvl <= c[x].max then
            t[#t + 1] = {}
            t[#t].item = c[x].i[1]
            t[#t].amount = c[x].i[2]
        end
    end
    return t
end
function onThink(interval)
    local players = getPlayersOnline()
    if players then
        for _, cid in pairs(players) do
            local reward = getRewards(getPlayerLevel(cid))
            if #reward >= 1 then
                local ps = getPlayerStorageValue(cid, storage)
                -- just noticed this below is pointless or in the wrong place at least
                doPlayerSetStorageValue(cid, storage, ps + 1)
                -- --------------------------------------
                if ps >= 60 then
                    for n = 1, #reward do
                        doPlayerAddItem(cid, reward[n].item, reward[n].amount)
                        doPlayerSetStorageValue(cid, storage, 0)
                        doPlayerSendTextMessage(cid, 27, "You Have Been Rewarded With "..reward[n].amount.."x Of "..getItemNameById(reward[n].item)..".")
                    end
                end
            end
        end
    end
    return true
end
 
Last edited:
what about xml should i leave like this
<globalevent name="onlinegift" interval="1000" event="script" value="onlinegift.lua"/>
? :)
its working better but gives gift on different timings i think since after reload coupe second it gaves rewards ^^
and again same reward after 1 min
 
Should make more sense now
Code:
local storage = 6722

local c = {
    [1] = {min = 80, max = 350, i = {2160, 5} },
    [2] = {min = 100, max = 1500, i = {12601, 5} },
    [3] = {min = 150, max = 1600, i = {11225, 1} }
}

-- no need to pass cid, we are just concerned with populating a table
function getRewards(lvl)
    local t = {}
    for x = 1, #c do
        if lvl >= c[x].min and lvl <= c[x].max then
            t[#t + 1] = {}
            t[#t].item = c[x].i[1]
            t[#t].amount = c[x].i[2]
        end
    end
    return t
end

-- i don't like to clutter up the interface too much
function storageValue(cid, s, v)
    local ps = getPlayerStorageValue(cid, s)
    doPlayerSetStorageValue(cid, s, ps + v)
    return getPlayerStorageValue(cid, s)
end

function onThink(interval)
    local players = getPlayersOnline()
    if players then
        for _, cid in pairs(players) do
            local reward = getRewards(getPlayerLevel(cid))
            if #reward >= 1 then
                local ps = storageValue(cid, storage, 1)
                if ps >= 60 then
                    for n = 1, #reward do
                        doPlayerAddItem(cid, reward[n].item, reward[n].amount)
                        doPlayerSetStorageValue(cid, storage, 0)
                        doPlayerSendTextMessage(cid, 27, "You Have Been Rewarded With "..reward[n].amount.."x Of "..getItemNameById(reward[n].item)..".")
                    end
                end
            end
        end
    end
    return true
end
 
what about xml should i leave like this
<globalevent name="onlinegift" interval="1000" event="script" value="onlinegift.lua"/>
? :)
its working better but gives gift on different timings i think since after reload coupe second it gaves rewards ^^
and again same reward after 1 min
Well you are basing the reward on levels and it is executing every second, so naturally if the player meets the level requirement, it will add 1 to storage value 60 * 1000 is 1 minute, I don't know much about the xml interval so i can't help you there.

Play around with the interval, see if anything changes, raise it lower it etc.. process of elimination.
 
after reload in same time i recieved gifts
15:58 Reloading globalevents...
15:58 Reloaded successfully.
15:58 You Have Been Rewarded With 5x Of .
15:58 You Have Been Rewarded With 1x Of .
 
well what i wanted is to give these rewards every 1 hour for players who stays online 1 hour

Edit:reward player for being online 1 hour
 
well what i wanted is to give these rewards every 1 hour for players who stays online 1 hour

Edit:reward player for being online 1 hour
isnt it getItemName(id)?
are the names defined in your .otb file?
try a test function that just has
Code:
print(getItemNameById(id))
and troubleshoot
in findrewards and another check for time logged in also in addition to levels
 
i use 0.4
and can't just add like on login.lua ?
PHP:
function addTimedCc(cid)
     if not isPlayer(cid) then
         return true
     end
     doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You recieved 2cc because you play ! In another 30 min you will recieve more! ENJOY WARFARE!!!")
     doPlayerAddItem(cid,2160,2)
     addEvent(addTimedCc, 30 * 60 * 1000, cid)
end
like this one ? :)
 
i use 0.4
and can't just add like on login.lua ?
PHP:
function addTimedCc(cid)
     if not isPlayer(cid) then
         return true
     end
     doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You recieved 2cc because you play ! In another 30 min you will recieve more! ENJOY WARFARE!!!")
     doPlayerAddItem(cid,2160,2)
     addEvent(addTimedCc, 30 * 60 * 1000, cid)
end
like this one ? :)
just do exactly like in your other thread and change the thing the script gives the player
..or just add to the existing script that rewards players every 30 min some more rewards and a changed string..
 
i just need add function on that script so if you are level 100-500 you recieve the gift if you are lover or higher you just skip gift
 
Back
Top