• 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 Function onAdvance TFS 1.0???

zetibia

Member
Joined
Jun 23, 2013
Messages
109
Reaction score
11
<event type="advance" name="LevelRecompense" event="script" value="recompenselvl20.lua"/>


function onAdvance(cid, skill, oldLevel, newLevel)
local config = {
[20] = {item = 2160, count = 3},
[50] = {item = 2160, count = 4},
[100] = {item = 2160, count = 8},
[200] = {item = 2160, count = 10},
[300] = {item = 2160, count = 20},
[400] = {item = 2160, count = 30},
}
if skill == 8 then
for level, info in pairs(config) do
if newLevel >= level and (getPlayerStorageValue(cid, 93010) == -1 or not (string.find(getPlayerStorageValue(cid, 93010), "'" .. level .. "'"))) then
doPlayerAddItem(cid, info.item, info.count)
doPlayerSendTextMessage(cid, 27, "Congratulation você atingiu o level "..newLevel.." e ganhou "..info.count.." "..getItemNameById(info.item)..".")
local sat = getPlayerStorageValue(cid, 93010) == -1 and "Values: '" .. level .. "'" or getPlayerStorageValue(cid, 93010) .. ",'" .. level .. "'"
setPlayerStorageValue(cid, 93010, sat)
end
end
end
return TRUE
end


What is new Function for script
 
TFS 1.0 uses script instead of event and value.
Code:
<event type="advance" name="LevelRecompense" script="recompenselvl20.lua"/>
Also register the name in login.lua.
 
Your ">=" test will test true every level after 19, and is probably why you're getting the reward too often.

Take those stupid text comparisons out of your if-statement conditions, clean up your test conditions, and try again. if you really need that Storage, put usable values into it.

Code:
local sat = getPlayerStorageValue(cid, 93010) == -1 and "Values: '" .. le
vel .. "'" or getPlayerStorageValue(cid, 93010) .. ",'" .. level .. "'"
setPlayerStorageValue(cid, 93010, sat)
"Values: '" .. level .. "'" is a constant - it's always going to return the same thing.

Using unnecessary quotes in a text value is asking for trouble with escape characters.
Remove them from the contents of the storage, - if you want them on output, add them on output.[/CODE]

BTW - I can't see what you're using that storage for at all.
You get "previous level" and "new level" from the onAdvance() function call, so if all you want to do it give the player something at the specific levels in that table, you have no need for Storage:

Code:
function onAdvance(cid, skill, oldLevel, newLevel)
local config = {
[20] = {item = 2160, count = 3},
[50] = {item = 2160, count = 4},
[100] = {item = 2160, count = 8},
[200] = {item = 2160, count = 10},
[300] = {item = 2160, count = 20},
[400] = {item = 2160, count = 30},
}
if skill == 8 then
       for level, info in pairs(config) do
               if (newLevel == level) then
                      doPlayerAddItem(cid, info.item, info.count)
                      doPlayerSendTextMessage(cid, 27, "Congratulation você atingiu o level "..newLevel.." e ganhou "..info.count.." "..getItemNameById(info.item)..".")
               end
        end
end

return TRUE
end
if skill level is exactly 8, this will give the correct reward at levels 20, 50, 100, 200, 300, and 400; nothing at other levels; nothing at other skill levels

If you want to use successive indexes from the table to delimit a range, they are data, not keys.

Put them into the array as data, traverse it using a "for 1, #table" loop, and remember the previous value in a variable (requires special-case handling for the first element of course, but that's easy with an integer-indexed array). Then your test is:
if (newLevel >= savedLevel) and (newLevel <= level) then
...
end


You could also put both the start and the end values for each range into the Table, because there are so few elements That would make the coding easier (no special case on element 1), but would be a pain if you had a lot of elements.
Code:
local config = {
{lowerBound=20, upperBound=49, item = 2160, count = 3},
{lowerBound=50, upperBound=99, item = 2160, count = 4},
{lowerBound=100, upperBound=199, item = 2160, count = 8},
{lowerBound=200, upperBound=299, item = 2160, count = 10},
{lowerBound=300, upperBound=399, item = 2160, count = 20},
{lowerBound=400, upperBound=math.huge, item = 2160, count = 30},
}

for index, info in ipairs(config) do
       if (newLevel >= info.lowerBound) and (newLevel <= info.upperBound) then
              doPlayerAddItem(cid, info.item, info.count)
              break
       end
end

BTW - don't underestimate the value of clean code.
Complicated conditions and unnecessarily repeated code lead to bugs when you change things later.
 
Last edited:
I found problem with ">=" but thank you for your helpfull answer.
About Storage it's for players who dead and get that level twice, it's protect from money makers.
I use your cleanup script.

Yours, CvB
 
I found problem with ">=" but thank you for your helpful answer.
About Storage it's for players who dead and get that level twice, it's protect from money makers.
Ok - the storage makes sense then.Uninitialized player storage holds "-1" (and hence supports a numerical comparison), so you can do this at the start:
Code:
maxLevelAchieved = getPlayerStorageValue(cid, 93010)

if (newLevel > maxLevelAchieved) then
        setPlayerStorageValue(cid, 93010, newLevel)
else return true
end
IMO this requirement is distinct from selecting the correct reward, so it should be done independently, using a separate "if" statement. Also if "new level isn't larger than maxLevel" is solid evidence of cheating, you can penalize the player in the "else" before returning.

Note: If your Storage is persistent between server restarts, and you want to go with this simpler value in the storage, you'll need some code to migrate the old values to the new ones.

In your situation I'd do that rather than stay with the old text . I'd use a single-use script that runs on startup (with built-in validation testing) for that rather than "polluting" this script with migration code, run it by recycling the server, then remove the script.
 
Last edited:
Back
Top