• 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 How to make a new currency

Tbol

Well-Known Member
Joined
Apr 7, 2019
Messages
529
Reaction score
56
Hello, how can i make new currency like event points after event you get +5 event points or more and those points are storaged in your player so to trade your points you would need to go to a npc and you could trade those points to a some kind of item. So what i need is
  1. These event points system it would be dope if it looked like this so it could be used everywhere ( player:addEventPoints (5) )
  2. Npc that could give rewards which is items for event points
  3. And talkaction to give event points or remove
USING TFS 1.2
 
Solution
I'm just gonna give you something quick I wrote, it doesn't include the npc or the talkaction, but will get you going on the right track.
Paste this on player.lua
Code:
function Player:getEventPoints(eventStorage)
    return self:getStorageValue(eventStorage)
end

function Player:addEventPoints(eventStorage, amount)
local points = self:getStorageValue(eventStorage)
    if points == -1 then
        self:setStorageValue(eventStorage, 0)
    end
    
    self:setStorageValue(eventStorage, points + amount)
end

You can have multiple events as they will have independent points from each other, let's say event A uses storage 1111 and event B uses 1112. You can store this points apart if you desire.
Example of use:
Code:
local myEvent = 1650...
I'm just gonna give you something quick I wrote, it doesn't include the npc or the talkaction, but will get you going on the right track.
Paste this on player.lua
Code:
function Player:getEventPoints(eventStorage)
    return self:getStorageValue(eventStorage)
end

function Player:addEventPoints(eventStorage, amount)
local points = self:getStorageValue(eventStorage)
    if points == -1 then
        self:setStorageValue(eventStorage, 0)
    end
    
    self:setStorageValue(eventStorage, points + amount)
end

You can have multiple events as they will have independent points from each other, let's say event A uses storage 1111 and event B uses 1112. You can store this points apart if you desire.
Example of use:
Code:
local myEvent = 1650 -- storage number
player:addEventPoints(myEvent, 10) -- 10 being the amount of points to add

-- You can also retrieve how many points a player has in certain event, for example
player:getEventPoints(myEvent)
 
Solution
I'm just gonna give you something quick I wrote, it doesn't include the npc or the talkaction, but will get you going on the right track.
Paste this on player.lua
Code:
function Player:getEventPoints(eventStorage)
    return self:getStorageValue(eventStorage)
end

function Player:addEventPoints(eventStorage, amount)
local points = self:getStorageValue(eventStorage)
    if points == -1 then
        self:setStorageValue(eventStorage, 0)
    end
   
    self:setStorageValue(eventStorage, points + amount)
end

You can have multiple events as they will have independent points from each other, let's say event A uses storage 1111 and event B uses 1112. You can store this points apart if you desire.
Example of use:
Code:
local myEvent = 1650 -- storage number
player:addEventPoints(myEvent, 10) -- 10 being the amount of points to add

-- You can also retrieve how many points a player has in certain event, for example
player:getEventPoints(myEvent)
It doesnt need database table?
 
It doesnt need database table?
This script use storage as "coin".

And you can adapt this script to work as a npc trader:
Ex: Changing if getPlayerItemCount(cid, 21400) >= 20 then, for something like: player: getEventPoints ()
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)
    npcHandler:onCreatureAppear(cid)
end
function onCreatureDisappear(cid)
    npcHandler:onCreatureDisappear(cid)
end
function onCreatureSay(cid, type, msg)
    npcHandler:onCreatureSay(cid, type, msg)
end
function onThink()
    npcHandler:onThink()
end

local items = {
     [1] = {name = "Abacus", id = 21467},
     [2] = {name = "Assassin Doll", id = 32594},
     [3] = {name = "Bag of Oriental Spices", id = 26338},
     [4] = {name = "Bookworm Doll", id = 32592},
     [5] = {name = "Cateroides Doll", id = 29217},
     [6] = {name = "Doll of Durin the Almighty", id = 26335},
     [7] = {name = "Dragon Eye", id = 24683},
     [8] = {name = "Dragon Goblet", id = 36100},
     [9] = {name = "Draken Doll", id = 29215},
     [10] = {name = "Encyclopedia", id = 26334},
     [11] = {name = "Friendship Amulet", id = 21469},
     [12] = {name = "Frozen Heart", id = 21472},
     [13] = {name = "Golden Falcon", id = 32593},
     [14] = {name = "Golden Newspaper", id = 26337},
     [15] = {name = "Hand Puppets", id = 26332},
     [16] = {name = "Imortus", id = 26339},
     [17] = {name = "Jade Amulet", id = 36103},
     [18] = {name = "Key of Numerous Locks", id = 21468},
     [19] = {name = "Loremaster Doll", id = 36102},
     [20] = {name = "Mathmaster Shield", id = 29218},
     [21] = {name = "Medusa Skull", id = 26336},
     [22] = {name = "Music Box", id = 26333},
     [23] = {name = "Noble Sword", id = 18551},
     [24] = {name = "Norsemal Doll", id = 21466},
     [25] = {name = "Old Radio", id = 32591},
     [26] = {name = "Orcs Jaw Shredder", id = 21471},
     [27] = {name = "Pigeon Trophy", id = 36101},
     [28] = {name = "Phoenix Statue", id = 24682},
     [29] = {name = "The Mexcalibur", id = 21470},
     [30] = {name = "TibiaHispano Emblem", id = 29216},
     [31] = {name = "Goromaphone", id = 39045}
}

local function greetCallback(cid)
    return true
end

local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end

    local player = Player(cid)

    if msg then
        for i = 1, #items do
          if msgcontains(msg, items[i].name) then
                if getPlayerItemCount(cid, 21400) >= 20 then
                    doPlayerRemoveItem(cid, 21400, 20)
                    doPlayerAddItem(cid, items[i].id, 1)
                    selfSay('You just swapped 20 silver raid tokens for 1 '.. getItemName(items[i].name) ..'.', cid)
                else
                    selfSay('You need 20 silver raid tokens.', cid)
                end
            end
        end
    end
    return true
end

local function onAddFocus(cid)
end

local function onReleaseFocus(cid)
end

npcHandler:setCallback(CALLBACK_ONADDFOCUS, onAddFocus)
npcHandler:setCallback(CALLBACK_ONRELEASEFOCUS, onReleaseFocus)

npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
This script use storage as "coin".

And you can adapt this script to work as a npc trader:
Ex: Changing if getPlayerItemCount(cid, 21400) >= 20 then, for something like: player: getEventPoints ()
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)
    npcHandler:onCreatureAppear(cid)
end
function onCreatureDisappear(cid)
    npcHandler:onCreatureDisappear(cid)
end
function onCreatureSay(cid, type, msg)
    npcHandler:onCreatureSay(cid, type, msg)
end
function onThink()
    npcHandler:onThink()
end

local items = {
     [1] = {name = "Abacus", id = 21467},
     [2] = {name = "Assassin Doll", id = 32594},
     [3] = {name = "Bag of Oriental Spices", id = 26338},
     [4] = {name = "Bookworm Doll", id = 32592},
     [5] = {name = "Cateroides Doll", id = 29217},
     [6] = {name = "Doll of Durin the Almighty", id = 26335},
     [7] = {name = "Dragon Eye", id = 24683},
     [8] = {name = "Dragon Goblet", id = 36100},
     [9] = {name = "Draken Doll", id = 29215},
     [10] = {name = "Encyclopedia", id = 26334},
     [11] = {name = "Friendship Amulet", id = 21469},
     [12] = {name = "Frozen Heart", id = 21472},
     [13] = {name = "Golden Falcon", id = 32593},
     [14] = {name = "Golden Newspaper", id = 26337},
     [15] = {name = "Hand Puppets", id = 26332},
     [16] = {name = "Imortus", id = 26339},
     [17] = {name = "Jade Amulet", id = 36103},
     [18] = {name = "Key of Numerous Locks", id = 21468},
     [19] = {name = "Loremaster Doll", id = 36102},
     [20] = {name = "Mathmaster Shield", id = 29218},
     [21] = {name = "Medusa Skull", id = 26336},
     [22] = {name = "Music Box", id = 26333},
     [23] = {name = "Noble Sword", id = 18551},
     [24] = {name = "Norsemal Doll", id = 21466},
     [25] = {name = "Old Radio", id = 32591},
     [26] = {name = "Orcs Jaw Shredder", id = 21471},
     [27] = {name = "Pigeon Trophy", id = 36101},
     [28] = {name = "Phoenix Statue", id = 24682},
     [29] = {name = "The Mexcalibur", id = 21470},
     [30] = {name = "TibiaHispano Emblem", id = 29216},
     [31] = {name = "Goromaphone", id = 39045}
}

local function greetCallback(cid)
    return true
end

local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end

    local player = Player(cid)

    if msg then
        for i = 1, #items do
          if msgcontains(msg, items[i].name) then
                if getPlayerItemCount(cid, 21400) >= 20 then
                    doPlayerRemoveItem(cid, 21400, 20)
                    doPlayerAddItem(cid, items[i].id, 1)
                    selfSay('You just swapped 20 silver raid tokens for 1 '.. getItemName(items[i].name) ..'.', cid)
                else
                    selfSay('You need 20 silver raid tokens.', cid)
                end
            end
        end
    end
    return true
end

local function onAddFocus(cid)
end

local function onReleaseFocus(cid)
end

npcHandler:setCallback(CALLBACK_ONADDFOCUS, onAddFocus)
npcHandler:setCallback(CALLBACK_ONRELEASEFOCUS, onReleaseFocus)

npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
What about getPlayerEventPointsBalance function?
 
What about getPlayerEventPointsBalance function?
Balance? You mean so that the npc will tell the player how many points they have right?

You can do something like this I guess:
Code:
local player = Player(cid)
local myEvent = 1650

if msgcontains(msg, 'balance') then
        npcHandler:say('You have ' .. player:getEventPoints(myEvent) .. ' event points.', cid)
end
It's been a while since I even opened up a local server so I cant test it out but it should work
 
I just used the post above as base.
You'll need to add the "coin storage" and function in lib to work this function.
Yea trying to make some code now.
Balance? You mean so that the npc will tell the player how many points they have right?

You can do something like this I guess:
Code:
local player = Player(cid)
local myEvent = 1650

if msgcontains(msg, 'balance') then
        npcHandler:say('You have ' .. player:getEventPoints(myEvent) .. ' event points.', cid)
end
It's been a while since I even opened up a local server so I cant test it out but it should work
Doing a talkaction for now npc is only for trading points to items. So im getting this error. Managed to make talkaction to check point balance

Lua:
function onSay(player, words, param)
    local split = param:split(",")
    local target = split[1] and Player(split[1])
    if not target then
        return player:sendCancelMessage("This player does not exist.")
    end
    local amount = 1
    if split[3] then
        amount = tonumber(split[3]) or 1
    end
    local result = target:addEventPoints(amount)
    if result then
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You gave %u event points to player %s.", amount, split[1]))
    target:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You received %s.", amount))
end
end
return true
Using
 
It says the var "amount" has a nil value, it's because the function is actually "addEventPoints(storage, amount)". You need to pass an storage to the function before the amount parameter

Like this:
Code:
function onSay(player, words, param)
    local split = param:split(",")
    local target = split[1] and Player(split[1])
    if not target then
        return player:sendCancelMessage("This player does not exist.")
    end
    local amount = 1
    if split[3] then
        amount = tonumber(split[3]) or 1
    end
    local result = target:addEventPoints(1111, amount)
    if result then
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You gave %u event points to player %s.", amount, split[1]))
    target:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You received %s.", amount))
end
end
return true
Used 1111 just as an example
Remember that points will be stored on an storage value, so you NEED to choose an storage number where you will be keeping them
 
Look this script, i think it could you for you.

Lua:
local test = TalkAction("/test")

function test.onSay(player, words, param)
    local split = param:split(",")
    local target = split[1] and Player(split[1])
    if not target then
        return player:sendCancelMessage("This player does not exist.")
    end

    local coins = tonumber(split[2])
    if type(coins) ~= 'number' or coins > 25000 then
        player:sendCancelMessage("Total Coins (Max: 25000).")
        return false
    end

    local result = target:setStorageValue(52400, target:getStorageValue(52400) + coins)
    if result then
    player:sendTextMessage(MESSAGE_INFO_DESCR, "You gave " .. coins .. " event points.")
    target:sendTextMessage(MESSAGE_INFO_DESCR, "You received " .. coins .. " event points.")
end
return true
end

test:separator(" ")
test:register()

The only changes you should make are to change the target:getStorageValue (52400) (and SET) to the one registered in your lib. And if your server doesn't support rev scripts, just remove the test. from onSay function, staying: function onSay(player, words, param)
Post automatically merged:

It says the var "amount" has a nil value, it's because the function is actually "addEventPoints(storage, amount)". You need to pass an storage to the function before the amount parameter

Like this:
Code:
function onSay(player, words, param)
    local split = param:split(",")
    local target = split[1] and Player(split[1])
    if not target then
        return player:sendCancelMessage("This player does not exist.")
    end
    local amount = 1
    if split[3] then
        amount = tonumber(split[3]) or 1
    end
    local result = target:addEventPoints(1111, amount)
    if result then
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You gave %u event points to player %s.", amount, split[1]))
    target:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You received %s.", amount))
end
end
return true
Used 1111 just as an example
Remember that points will be stored on an storage value, so you NEED to choose an storage number where you will be keeping them
Sorry i didn't see your post
 
It says the var "amount" has a nil value, it's because the function is actually "addEventPoints(storage, amount)". You need to pass an storage to the function before the amount parameter

Like this:
Code:
function onSay(player, words, param)
    local split = param:split(",")
    local target = split[1] and Player(split[1])
    if not target then
        return player:sendCancelMessage("This player does not exist.")
    end
    local amount = 1
    if split[3] then
        amount = tonumber(split[3]) or 1
    end
    local result = target:addEventPoints(1111, amount)
    if result then
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You gave %u event points to player %s.", amount, split[1]))
    target:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You received %s.", amount))
end
end
return true
Used 1111 just as an example
Remember that points will be stored on an storage value, so you NEED to choose an storage number where you will be keeping them
doesnt work
Look this script, i think it could you for you.

Lua:
local test = TalkAction("/test")

function test.onSay(player, words, param)
    local split = param:split(",")
    local target = split[1] and Player(split[1])
    if not target then
        return player:sendCancelMessage("This player does not exist.")
    end

    local coins = tonumber(split[2])
    if type(coins) ~= 'number' or coins > 25000 then
        player:sendCancelMessage("Total Coins (Max: 25000).")
        return false
    end

    local result = target:setStorageValue(52400, target:getStorageValue(52400) + coins)
    if result then
    player:sendTextMessage(MESSAGE_INFO_DESCR, "You gave " .. coins .. " event points.")
    target:sendTextMessage(MESSAGE_INFO_DESCR, "You received " .. coins .. " event points.")
end
return true
end

test:separator(" ")
test:register()

The only changes you should make are to change the target:getStorageValue (52400) (and SET) to the one registered in your lib. And if your server doesn't support rev scripts, just remove the test. from onSay function, staying: function onSay(player, words, param)
Post automatically merged:


Sorry i didn't see your post
dont you need to change that coins too? I dont need to register in my lib because i dont have any event yet? If i added the code in player.lua so doesnt make sense to be honest or im just really stupid
 
doesnt work

dont you need to change that coins too? I dont need to register in my lib because i dont have any event yet? If i added the code in player.lua so doesnt make sense to be honest or im just really stupid

No you don't need to change the coins. To make it easier, just change the number of the storage and that will work.
52400 → (YOUR STORAGE VALUE)

Lua:
function onSay(player, words, param)
    local split = param:split(",")
    local target = split[1] and Player(split[1])
    if not target then
        return player:sendCancelMessage("This player does not exist.")
    end

    local coins = tonumber(split[2])
    if type(coins) ~= 'number' or coins > 250000 then
        player:sendCancelMessage("Total Coins (Max: 250000).")
        return false
    end

    local result = target:setStorageValue(52400, target:getStorageValue(52400) + coins)
    if result then
    player:sendTextMessage(MESSAGE_INFO_DESCR, "You gave " .. coins .. " event points.")
    target:sendTextMessage(MESSAGE_INFO_DESCR, "You received " .. coins .. " event points.")
end
return true
end
 
No you don't need to change the coins. To make it easier, just change the number of the storage and that will work.
52400 → (YOUR STORAGE VALUE)

Lua:
function onSay(player, words, param)
    local split = param:split(",")
    local target = split[1] and Player(split[1])
    if not target then
        return player:sendCancelMessage("This player does not exist.")
    end

    local coins = tonumber(split[2])
    if type(coins) ~= 'number' or coins > 250000 then
        player:sendCancelMessage("Total Coins (Max: 250000).")
        return false
    end

    local result = target:setStorageValue(52400, target:getStorageValue(52400) + coins)
    if result then
    player:sendTextMessage(MESSAGE_INFO_DESCR, "You gave " .. coins .. " event points.")
    target:sendTextMessage(MESSAGE_INFO_DESCR, "You received " .. coins .. " event points.")
end
return true
end
Thanks everything works. Appreciate your support
 
Back
Top