• 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 Comparing numbers problem

Denziz

New Member
Joined
May 2, 2013
Messages
81
Reaction score
4
I am trying to compare the three skills (axe, sword and club) to see which skill is the highest. It prints out correctly the numbers from the mysql database but I keep getting the knight axe...

Code:
Code:
local skillAxeQuery = db.storeQuery("SELECT skill_axe FROM players WHERE id = " .. getPlayerGUID(player))
                    local skillSwordQuery = db.storeQuery("SELECT skill_sword FROM players WHERE id = " .. getPlayerGUID(player))
                    local skillClubQuery = db.storeQuery("SELECT skill_club FROM players WHERE id = " .. getPlayerGUID(player))
                
                    local skillAxeResult = result.getDataInt(skillAxeQuery, "skill_axe")
                    local skillSwordResult = result.getDataInt(skillSwordQuery, "skill_sword")
                    local skillClubResult = result.getDataInt(skillClubQuery, "skill_club")
                
                    print(skillAxeResult)
                    print(skillSwordResult)
                    print(skillClubResult)
                
                    if skillAxeResult > skillSwordResult or skillClubResult then
                        doPlayerAddItem(player, 2430, 1) -- Knight axe
                        return true
                    elseif skillSwordResult > skillAxeResult or skillClubResult then
                        doPlayerAddItem(player, 7408, 1) -- Wyvern fang
                        return true
                    elseif skillClubResult > skillAxeResult or skillSwordResult then
                        doPlayerAddItem(player, 2434, 1) -- Dragon hammer
                        return true
                    end
 
Code:
local skillAxe = getPlayerSkillLevel(player, SKILL_AXE)
local skillSword = getPlayerSkillLevel(player, SKILL_SWORD)
local skillClub = getPlayerSkillLevel(player, SKILL_CLUB)

local higherSkill = math.max(skillAxe, skillSword, skillClub)
if higherSkill == skillAxe then
    doPlayerAddItem(player, 2430, 1) -- Knight axe
elseif higherSkill == skillSword then
    doPlayerAddItem(player, 7408, 1) -- Wyvern fang
elseif higherSkill == skillClub then
    doPlayerAddItem(player, 2434, 1) -- Dragon hammer
end
return true

If you care to know why you were always receiving the knight axe: when you evaluate (skillAxeResult > skillSwordResult or skillClubResult) you're not evaluating whether the player's axe skill is higher than his sword and club skills, that expression will be truthy if either the player's axe skill is higher than his sword skill OR if skillClubResult is truthy (that means, any value other than nil or false), since your query works and returns the player's club result, which is a number, that expression will always be truthy and the player will always receive a Knight Axe.

And lastly, you should only retrieve a player value from the database if the player is offline, otherwise the value will be outdated.
 
Back
Top