• 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 Different Values

Sun

Knowledge is power - France is bacon
Joined
Jan 26, 2015
Messages
333
Solutions
22
Reaction score
244
---for tfs 0.4---

I was trying to make a "complex system" inside my promotion book script, but ended up making a simplified version that works for me.
It kind of ticks me that I can't figure out how to make it the proper way, so I'm asking for assistance.

The gegg and pegg value system has a flaw

Code:
local cashAmount = 1000000

local gegg = 6544 -- green egg
local pegg = 6545 -- purple egg

function onUse(cid, item, fromPosition, itemEx, toPosition)
   local money = getPlayerMoney(cid)
   local getItem = getPlayerItemCount(cid, 12406)

   local x = getPlayerItemCount(cid, gegg) -- base value (kinda like gp)
   local y = getPlayerItemCount(cid, pegg) -- second value (like platinum coins)
   local a = y * 100 -- means that 1 pegg has the value of 100 gegg
   local z = a + x -- total amount of pegg and gegg together. This was only used in the test fase, but I beleive it will be an important part to the solution.
  
   local b = x/100 -- returns each 100 gegg as 1
   if b > 5 then -- if there's more than 500 gegg then
     b = 5 -- gegg is only equals to 500
   end
  
   local c = 0 -- just a base value so it can be edited without returning errors
  
   if math.floor(b) == 4 then -- if gegg is rounded down to 400 then
     c = 1 -- will be the amount of pegg that we'll need to compensate for the gegg
   elseif math.floor(b) == 3 then
     c = 2
   elseif math.floor(b) == 2 then
     c = 3
   elseif math.floor(b) == 1 then
     c = 4
   elseif math.floor(b) == 0 then
     c = 5
   else
     c = 0
   end -- this c part annoys me, it'll be a pain if one would like to change the amount of gegg needed
  
   if(getPlayerPromotionLevel(cid) < 1) then --if the one using the item is a player and isn't promoted then
     if z >= 500 and money >= cashAmount and getItem >= 300 then
       if doPlayerRemoveMoney(cid, cashAmount) and doPlayerRemoveItem(cid, 12406, 300) and doPlayerRemoveItem(cid, gegg, b*100) and doPlayerRemoveItem(cid, pegg, c) then -- if we removed all the required items from the player then
         doPlayerSetPromotionLevel(cid, 1)
         doCreatureSay(cid, "you\'ve been promoted", TALKTYPE_ORANGE_1)
       end
     else
       return doPlayerSendCancel(cid, "you need 100 crystal coins, 300 death notes and 500 eggs to be promoted.")
     end
   else
     return doPlayerSendCancel(cid, "you\'re already promoted")
   end  
   return true
end

This one has been quite a challenge even up to just this point.
Ty in advance :p
 
Last edited:
I don't think you need to check if the cid is a player in an action script since creatures can't execute an onUse action :p

The last thing you want to do is start removing items from a player especially if the player doesn't have all the items required, you want to count the items 1st then removed them, this way you aren't stealing from the player.

As for the rest of the code I am not exactly sure what the point of the eggs are, or the reason to use math.floor.
 
Last edited:
What's with x, a, y, z, c???
Variables should have proper names that indicate what they are!

I'm not sure what you are trying out, but if you are trying to remove a quantity and it can either be one item or the other you are better of making a kind of exchange rate or something.
 
I don't think you need to check if the cid is a player in an action script since creatures can't execute an onUse action :p
Wasn't sure so I added it, but ty :p

The last thing you want to do is start removing items from a player especially if the player doesn't have all the items required, you want to count the items 1st then removed them, this way you aren't stealing from the player.
I accidently pasted a copy of the script before it was finished, updated the main post

As for the rest of the code I am not exactly sure what the point of the eggs are, or the reason to use math.floor.
The player needs 500 green eggs in order to be promoted & 100 green eggs = 1 purple egg.
math.floor is used to make sure the amount of green eggs are rounded downwards so it doesn't remove more green eggs than needed from the player.
 
I just happened to make a function to handle this
Code:
    function countMinimumItems(cid, item)
        if type(item) == 'table' then
            local count = {}
            for _, items in pairs(item) do
                count[#count + 1] = math.floor(getPlayerItemCount(cid, items))
            end
            return unpack(count)
        end
        return math.floor(getPlayerItemCount(cid, item))
    end

Can be used like this
Code:
local eggs = {
    ['Green'] = 6544,
    ['Purple'] = 6545
}
local green, purple = countMinimumItems(eggs)
 
I just happened to make a function to handle this
Code:
function countMinimumItems(cid, item)
if type(item) == 'table' then
local count = {}
for _, items in pairs(item) do
count[#count + 1] = math.floor(getPlayerItemCount(cid, items))
end
return unpack(count)
end
return math.floor(getPlayerItemCount(cid, item))
end
Can be used like this
Code:
local eggs = {
['Green'] = 6544,
['Purple'] = 6545
}
local green, purple = countMinimumItems(eggs)

Awesome gonna try it out.
Btw, how does it know the conversion rate between the eggs?
 
all it does is return the minimum amount of items, so if you have 499 of a item type, it returns 400, i could of did the math by dividing 100 but a function should be generic, allowing it to handle multiple value types.
 
Ah, I see. So it's basically what "local b" should've been.
ty

local c = 0

if math.floor(b) == 4 then
c = 1
elseif math.floor(b) == 3 then
c = 2
elseif math.floor(b) == 2 then
c = 3
elseif math.floor(b) == 1 then
c = 4
elseif math.floor(b) == 0 then
c = 5
else
c = 0
end
this part still pains me
 
Code:
function randomValue(value)
    local t = {[0] = 5, 4, 3, 2, 1}
    return t[math.floor(value)] ~= nil and t[math.floor(value)] or 0
end
 
Last edited:

Similar threads

Replies
5
Views
692
Back
Top