• 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 Script Issues [PLEASE HELP!]

aalqaq2

Trying to help, but I'm not too good at it.
Joined
Apr 10, 2017
Messages
112
Solutions
3
Reaction score
8
TFS Version 1.2: Client Version 10.98

I'm sure the problem is literally in front of my face, but I've been trying to figure this out for the last few days. Can someone please help me? The NPC is in Thais DP. (32352,32226,7)

I can:
  • Greet the NPC,
  • Put the money on table (Only in middle)
  • Place a bet
  • NPC will take money He'll even tell me if I win/lose.
  • When you win he WON'T GIVE YOU YOUR MONEY.
Also, HUGE issue. You can walk through NPC.

If someone can help me fix these issues:

  • Be able to put money on DP in front of NPC AND Middle section
  • Fix payout
  • Prevent people from walking on NPC position
I would really appreciate it. If it works perfectly I'll even add in a little reward if you give me your paypal :D
PICTURES ATTACHED. Please let me know if any other information is needed.

XML FILE:

<?xml version="1.0" encoding="UTF-8"?>
<npc name="The Gambling Man" script="Dicer.lua" walkinterval="0" floorchange="0">
<health now="100" max="100"/>
<look type="132" head="114" body="0" legs="0" feet="114" addons="3"/>
<parameters>
<parameter key="message_greet" value="Hello, |PLAYERNAME|. You can bet speaking {H} or {L}, the minimum wager is 1,000 and the maximum wager is 1,000,000." />
</parameters>
</npc>

Dicer1.png Dicer2.png
 
Solution
No, I don't have a doCreateItem function. Would you mind helping me insert that? I changed all of the "local functions" to "functions". Am I supposed to change the variables as well or just the functions?
Lua:
function doCreateItem(itemid, count, pos)
    local tile = Tile(pos)
    if not tile then
        return false
    end

    local item = Game.createItem(itemid, count, pos)
    if item then
        return item:getUniqueId()
    end
    return false
end
forgottenserver/compat.lua at 5ba1ef3926695237a8da3d969bd99e779a0ae7f1 · otland/forgottenserver · GitHub
XML:
 <?xml version="1.0" encoding="UTF-8"?>
<npc name="The Gambling Man" script="Dicer.lua" walkinterval="0" floorchange="0">
<health now="100" max="100"/>
<look type="132" head="114" body="0" legs="0" feet="114" addons="3"/>
 <parameters>
  <parameter key="message_greet" value="Hello, |PLAYERNAME|. You can bet speaking {H} or {L}, the minimum wager is 1,000 and the maximum wager is 1,000,000." />
 </parameters>
</npc>
PHP:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
 
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 function delayMoneyRemoval(item, pos)
        doRemoveItem(getTileItemById(pos, item).uid)
        return true
end
 
local function placeMoney(amount, table_middle_pos)
        local remain = amount
        local crystal_coins = 0
        local platinum_coins = 0
 
        if (math.floor(amount / 10000) >= 1) then
                crystal_coins = math.floor(amount / 10000)
                remain = remain - crystal_coins * 10000
        end
        if ((remain / 100) >= 1) then
                platinum_coins = remain / 100
        end
        addEvent(doCreateItem, 550, 2152, platinum_coins, table_middle_pos)
        addEvent(doCreateItem, 600, 2160, crystal_coins, table_middle_pos)
end
 
local function rollDice(roll, cc_count, pc_count, table_left_pos, table_middle_pos, npc)
        local dice_ids = {5792, 5793, 5794, 5795, 5796, 5797}
        local random_rollval = math.random(1,6)
        local total_g = (10000 * cc_count) + (100 * pc_count)
        local prize_percent = 0.8 -- 80%
  local total_new = total_g * prize_percent
 
        if ((total_g) <= 1000000 and (total_g) >= 1000) then
                doSendMagicEffect(table_left_pos, CONST_ME_CRAPS)
 
                for _, itemId in pairs(dice_ids) do
                                if(getTileItemById(table_left_pos, itemId).uid > 0) then
                                doTransformItem(getTileItemById(table_left_pos, itemId).uid, dice_ids[random_rollval])
                        end
                end
 
                if (roll == 1 and random_rollval <= 3) then
                        placeMoney(total_g + total_new, table_middle_pos)
                        addEvent(doSendMagicEffect, 400, table_left_pos, CONST_ME_SOUND_GREEN)
                        addEvent(doSendMagicEffect, 700, table_left_pos, CONST_ME_SOUND_GREEN)
                        addEvent(doCreatureSay, 500, npc, "You win!", TALKTYPE_SAY, false, 0)
                elseif (roll == 2 and random_rollval >= 4) then
                        placeMoney(total_g + total_new, table_middle_pos)
                        addEvent(doSendMagicEffect, 400, table_left_pos, CONST_ME_SOUND_GREEN)
                        addEvent(doSendMagicEffect, 700, table_left_pos, CONST_ME_SOUND_GREEN)
                        addEvent(doCreatureSay, 500, npc, "You win!", TALKTYPE_SAY, false, 0)
                else
                        addEvent(doSendMagicEffect, 400, table_left_pos, CONST_ME_BLOCKHIT)
                        addEvent(doSendMagicEffect, 700, table_left_pos, CONST_ME_BLOCKHIT)
                        addEvent(doCreatureSay, 500, npc, "Better luck next time.", TALKTYPE_SAY, false, 0)
                end
                doCreatureSay(npc, string.format("%s rolled a %d.", getCreatureName(npc), random_rollval), TALKTYPE_ORANGE_1, false, 0, table_left_pos)
        else
                addEvent(doCreateItem, 100, 2160, cc_count, table_middle_pos)
                addEvent(doCreateItem, 150, 2152, pc_count, table_middle_pos)
                doCreatureSay(npc, "The minimum wager is 1,000 and the maximum wager is 1,000,000.", TALKTYPE_SAY, false, 0)
        end
        return true
end
 
function creatureSayCallback(cid, type, msg)
        -- NPC userdata instance
        local npc = getNpcCid()
 
        -- Game table position userdata instances
        local table_left_pos = {x = 32352, y = 32225, z = 7} -- Table Position in front of NPC
        local table_middle_pos = {x = 32353, y = 32225, z = 7} -- Table Position between NPC and Player
 
        -- Search for coins on the left and middle tables and create item userdata instances
        local table_middle_cc = getTileItemById(table_middle_pos, 2160)
        local table_middle_pc = getTileItemById(table_middle_pos, 2152)
 
        -- Other variables
        local cc_count = 0
        local pc_count = 0
        local ROLL, LOW, HIGH = 0, 1, 2
        posplayer = {x=32354, y=32226, z=7} -- Players Position
                local ppos = getPlayerPosition(cid)
        if ppos.x == posplayer.x and ppos.y == posplayer.y then
        if isInArray({"H", "HIGH", "high", "h"}, msg) then
                        ROLL = HIGH
                elseif  isInArray({"L", "LOW", "l", "low"}, msg) then
                        ROLL = LOW             
                else
                        return false
                end
                if (table_middle_cc.uid ~= 0) then
                        cc_count = table_middle_cc.type
                        doTeleportThing(table_middle_cc.uid, table_left_pos)
                        addEvent(delayMoneyRemoval, 300, 2160, table_left_pos)
                end
                if (table_middle_pc.uid ~= 0) then
                        pc_count = table_middle_pc.type
                        doTeleportThing(table_middle_pc.uid, table_left_pos)
                        addEvent(delayMoneyRemoval, 300, 2152, table_left_pos)
                end
                addEvent(rollDice, 500, ROLL, cc_count, pc_count, table_left_pos, table_middle_pos, npc)
        else
                return false
        end
        return true
end
 
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Simple solution? Remove the word local in front of all the functions.(there is only 3 local functions)
This is irrelevant to the problem.
 
Last edited:
No, I don't have a doCreateItem function. Would you mind helping me insert that? I changed all of the "local functions" to "functions". Am I supposed to change the variables as well or just the functions?
 
No, I don't have a doCreateItem function. Would you mind helping me insert that? I changed all of the "local functions" to "functions". Am I supposed to change the variables as well or just the functions?
Lua:
function doCreateItem(itemid, count, pos)
    local tile = Tile(pos)
    if not tile then
        return false
    end

    local item = Game.createItem(itemid, count, pos)
    if item then
        return item:getUniqueId()
    end
    return false
end
forgottenserver/compat.lua at 5ba1ef3926695237a8da3d969bd99e779a0ae7f1 · otland/forgottenserver · GitHub
 
Solution
I inserted the function and got this:

[Warning - NpcScript::NpcScript] Can not load script: Dicer.lua
data/npc/scripts/Dicer.lua:17: '=' expected near ' '

I don't even know what ' ' is...
 
[Warning - NpcScript::NpcScript] Can not load script: Dicer.lua
data/npc/scripts/Dicer.lua:17: '=' expected near ' '
Show the script again this way we know what you edited, if you are told to make a change, always show the changes you made it makes it easier to figure why something isn't working.
 
PHP:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
 
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
 
function delayMoneyRemoval(item, pos)
        doRemoveItem(getTileItemById(pos, item).uid)
        return true
end
function doCreateItem(itemid, count, pos)
    local tile = Tile(pos)
    if not tile then
        return false
    end
 
    local item = Game.createItem(itemid, count, pos)
    if item then
        return item:getUniqueId()
    end
    return false
end
 
function placeMoney(amount, table_middle_pos)
        local remain = amount
        local crystal_coins = 0
        local platinum_coins = 0
 
        if (math.floor(amount / 10000) >= 1) then
                crystal_coins = math.floor(amount / 10000)
                remain = remain - crystal_coins * 10000
        end
        if ((remain / 100) >= 1) then
                platinum_coins = remain / 100
        end
        addEvent(doCreateItem, 550, 2152, platinum_coins, table_middle_pos)
        addEvent(doCreateItem, 600, 2160, crystal_coins, table_middle_pos)
end
 
function rollDice(roll, cc_count, pc_count, table_left_pos, table_middle_pos, npc)
        local dice_ids = {5792, 5793, 5794, 5795, 5796, 5797}
        local random_rollval = math.random(1,6)
        local total_g = (10000 * cc_count) + (100 * pc_count)
        local prize_percent = 0.8 -- 80%
  local total_new = total_g * prize_percent
 
        if ((total_g) <= 1000000 and (total_g) >= 1000) then
                doSendMagicEffect(table_left_pos, CONST_ME_CRAPS)
 
                for _, itemId in pairs(dice_ids) do
                                if(getTileItemById(table_left_pos, itemId).uid > 0) then
                                doTransformItem(getTileItemById(table_left_pos, itemId).uid, dice_ids[random_rollval])
                        end
                end
 
                if (roll == 1 and random_rollval <= 3) then
                        placeMoney(total_g + total_new, table_middle_pos)
                        addEvent(doSendMagicEffect, 400, table_left_pos, CONST_ME_SOUND_GREEN)
                        addEvent(doSendMagicEffect, 700, table_left_pos, CONST_ME_SOUND_GREEN)
                        addEvent(doCreatureSay, 500, npc, "You win!", TALKTYPE_SAY, false, 0)
                elseif (roll == 2 and random_rollval >= 4) then
                        placeMoney(total_g + total_new, table_middle_pos)
                        addEvent(doSendMagicEffect, 400, table_left_pos, CONST_ME_SOUND_GREEN)
                        addEvent(doSendMagicEffect, 700, table_left_pos, CONST_ME_SOUND_GREEN)
                        addEvent(doCreatureSay, 500, npc, "You win!", TALKTYPE_SAY, false, 0)
                else
                        addEvent(doSendMagicEffect, 400, table_left_pos, CONST_ME_BLOCKHIT)
                        addEvent(doSendMagicEffect, 700, table_left_pos, CONST_ME_BLOCKHIT)
                        addEvent(doCreatureSay, 500, npc, "Better luck next time.", TALKTYPE_SAY, false, 0)
                end
                doCreatureSay(npc, string.format("%s rolled a %d.", getCreatureName(npc), random_rollval), TALKTYPE_ORANGE_1, false, 0, table_left_pos)
        else
                addEvent(doCreateItem, 100, 2160, cc_count, table_middle_pos)
                addEvent(doCreateItem, 150, 2152, pc_count, table_middle_pos)
                doCreatureSay(npc, "The minimum wager is 1,000 and the maximum wager is 1,000,000.", TALKTYPE_SAY, false, 0)
        end
        return true
end
 
function creatureSayCallback(cid, type, msg)
        -- NPC userdata instance
        local npc = getNpcCid()
 
        -- Game table position userdata instances
        local table_left_pos = {x = 32352, y = 32225, z = 7} -- Table Position in front of NPC
        local table_middle_pos = {x = 32353, y = 32225, z = 7} -- Table Position between NPC and Player
 
        -- Search for coins on the left and middle tables and create item userdata instances
        local table_middle_cc = getTileItemById(table_middle_pos, 2160)
        local table_middle_pc = getTileItemById(table_middle_pos, 2152)
 
        -- Other variables
        local cc_count = 0
        local pc_count = 0
        local ROLL, LOW, HIGH = 0, 1, 2
        posplayer = {x=32354, y=32226, z=7} -- Players Position
                local ppos = getPlayerPosition(cid)
        if ppos.x == posplayer.x and ppos.y == posplayer.y then
        if isInArray({"H", "HIGH", "high", "h"}, msg) then
                        ROLL = HIGH
                elseif  isInArray({"L", "LOW", "l", "low"}, msg) then
                        ROLL = LOW             
                else
                        return false
                end
                if (table_middle_cc.uid ~= 0) then
                        cc_count = table_middle_cc.type
                        doTeleportThing(table_middle_cc.uid, table_left_pos)
                        addEvent(delayMoneyRemoval, 300, 2160, table_left_pos)
                end
                if (table_middle_pc.uid ~= 0) then
                        pc_count = table_middle_pc.type
                        doTeleportThing(table_middle_pc.uid, table_left_pos)
                        addEvent(delayMoneyRemoval, 300, 2152, table_left_pos)
                end
                addEvent(rollDice, 500, ROLL, cc_count, pc_count, table_left_pos, table_middle_pos, npc)
        else
                return false
        end
        return true
end
 
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Is my "placeMoney" function supposed to have a return statement?
No return statement is needed

Lets try it again I didn't change anything I just formatted the script so its readable (for me at least), sometimes when you reformat a script it will remove hidden characters.
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
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

function delayMoneyRemoval(item, pos)
  doRemoveItem(getTileItemById(pos, item).uid)
  return true
end

function doCreateItem(itemid, count, pos)
  local tile = Tile(pos)
  if not tile then
      return false
  end

  local item = Game.createItem(itemid, count, pos)
  if item then
      return item:getUniqueId()
  end
  return false
end

function placeMoney(amount, table_middle_pos)
  local remain = amount
  local crystal_coins = 0
  local platinum_coins = 0

  if (math.floor(amount / 10000) >= 1) then
    crystal_coins = math.floor(amount / 10000)
    remain = remain - crystal_coins * 10000
  end
  if ((remain / 100) >= 1) then
    platinum_coins = remain / 100
  end
  addEvent(doCreateItem, 550, 2152, platinum_coins, table_middle_pos)
  addEvent(doCreateItem, 600, 2160, crystal_coins, table_middle_pos)
end

function rollDice(roll, cc_count, pc_count, table_left_pos, table_middle_pos, npc)
  local dice_ids = {5792, 5793, 5794, 5795, 5796, 5797}
  local random_rollval = math.random(1,6)
  local total_g = (10000 * cc_count) + (100 * pc_count)
  local prize_percent = 0.8 -- 80%
  local total_new = total_g * prize_percent
  if ((total_g) <= 1000000 and (total_g) >= 1000) then
    doSendMagicEffect(table_left_pos, CONST_ME_CRAPS)
    for _, itemId in pairs(dice_ids) do
      if(getTileItemById(table_left_pos, itemId).uid > 0) then
        doTransformItem(getTileItemById(table_left_pos, itemId).uid, dice_ids[random_rollval])
      end
    end
    if (roll == 1 and random_rollval <= 3) then
      placeMoney(total_g + total_new, table_middle_pos)
      addEvent(doSendMagicEffect, 400, table_left_pos, CONST_ME_SOUND_GREEN)
      addEvent(doSendMagicEffect, 700, table_left_pos, CONST_ME_SOUND_GREEN)
      addEvent(doCreatureSay, 500, npc, "You win!", TALKTYPE_SAY, false, 0)
    elseif (roll == 2 and random_rollval >= 4) then
      placeMoney(total_g + total_new, table_middle_pos)
      addEvent(doSendMagicEffect, 400, table_left_pos, CONST_ME_SOUND_GREEN)
      addEvent(doSendMagicEffect, 700, table_left_pos, CONST_ME_SOUND_GREEN)
      addEvent(doCreatureSay, 500, npc, "You win!", TALKTYPE_SAY, false, 0)
    else
      addEvent(doSendMagicEffect, 400, table_left_pos, CONST_ME_BLOCKHIT)
      addEvent(doSendMagicEffect, 700, table_left_pos, CONST_ME_BLOCKHIT)
      addEvent(doCreatureSay, 500, npc, "Better luck next time.", TALKTYPE_SAY, false, 0)
    end
    doCreatureSay(npc, string.format("%s rolled a %d.", getCreatureName(npc), random_rollval), TALKTYPE_ORANGE_1, false, 0, table_left_pos)
  else
    addEvent(doCreateItem, 100, 2160, cc_count, table_middle_pos)
    addEvent(doCreateItem, 150, 2152, pc_count, table_middle_pos)
    doCreatureSay(npc, "The minimum wager is 1,000 and the maximum wager is 1,000,000.", TALKTYPE_SAY, false, 0)
  end
  return true
end

function creatureSayCallback(cid, type, msg)
  -- NPC userdata instance
  local npc = getNpcCid()

  -- Game table position userdata instances
  local table_left_pos = {x = 32352, y = 32225, z = 7} -- Table Position in front of NPC
  local table_middle_pos = {x = 32353, y = 32225, z = 7} -- Table Position between NPC and Player

  -- Search for coins on the left and middle tables and create item userdata instances
  local table_middle_cc = getTileItemById(table_middle_pos, 2160)
  local table_middle_pc = getTileItemById(table_middle_pos, 2152)

  -- Other variables
  local cc_count = 0
  local pc_count = 0
  local ROLL, LOW, HIGH = 0, 1, 2
  posplayer = {x=32354, y=32226, z=7} -- Players Position
          local ppos = getPlayerPosition(cid)
  if ppos.x == posplayer.x and ppos.y == posplayer.y then
  if isInArray({"H", "HIGH", "high", "h"}, msg) then
    ROLL = HIGH
  elseif  isInArray({"L", "LOW", "l", "low"}, msg) then
    ROLL = LOW         
  else
    return false
  end
  if (table_middle_cc.uid ~= 0) then
    cc_count = table_middle_cc.type
    doTeleportThing(table_middle_cc.uid, table_left_pos)
    addEvent(delayMoneyRemoval, 300, 2160, table_left_pos)
  end
  if (table_middle_pc.uid ~= 0) then
    pc_count = table_middle_pc.type
    doTeleportThing(table_middle_pc.uid, table_left_pos)
    addEvent(delayMoneyRemoval, 300, 2152, table_left_pos)
  end
  addEvent(rollDice, 500, ROLL, cc_count, pc_count, table_left_pos, table_middle_pos, npc)
  else
    return false
  end
  return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
I have a error with ruler 15, but why?
I use tfs 1.3 client 12.30


Lua Script Error: [Main Interface]
in a timer event called from:
(Unknown scriptfile)
data/npc/scripts/Dicer.lua:23: attempt to call method 'getUniqueId' (a nil value)
stack traceback:
[C]: in function 'getUniqueId'
data/npc/scripts/Dicer.lua:23: in function <data/npc/scripts/Dicer.lua:15>

But when I talk to npc, everything works. I win, get money and when I lose, he takes o_O

In Dicer.lua i have the same like you
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
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

function delayMoneyRemoval(item, pos)
  doRemoveItem(getTileItemById(pos, item).uid)
  return true
end

function doCreateItem(itemid, count, pos)
  local tile = Tile(pos)
  if not tile then
      return false
  end

  local item = Game.createItem(itemid, count, pos)
  if item then
      return item:getUniqueId()
  end
  return false
end

function placeMoney(amount, table_middle_pos)
  local remain = amount
  local crystal_coins = 0
  local platinum_coins = 0

  if (math.floor(amount / 10000) >= 1) then
    crystal_coins = math.floor(amount / 10000)
    remain = remain - crystal_coins * 10000
  end
  if ((remain / 100) >= 1) then
    platinum_coins = remain / 100
  end
  addEvent(doCreateItem, 550, 2152, platinum_coins, table_middle_pos)
  addEvent(doCreateItem, 600, 2160, crystal_coins, table_middle_pos)
end

function rollDice(roll, cc_count, pc_count, table_left_pos, table_middle_pos, npc)
  local dice_ids = {5792, 5793, 5794, 5795, 5796, 5797}
  local random_rollval = math.random(1,6)
  local total_g = (10000 * cc_count) + (100 * pc_count)
  local prize_percent = 0.8 -- 80%
  local total_new = total_g * prize_percent
  if ((total_g) <= 1000000 and (total_g) >= 1000) then
    doSendMagicEffect(table_left_pos, CONST_ME_CRAPS)
    for _, itemId in pairs(dice_ids) do
      if(getTileItemById(table_left_pos, itemId).uid > 0) then
        doTransformItem(getTileItemById(table_left_pos, itemId).uid, dice_ids[random_rollval])
      end
    end
    if (roll == 1 and random_rollval <= 3) then
      placeMoney(total_g + total_new, table_middle_pos)
      addEvent(doSendMagicEffect, 400, table_left_pos, CONST_ME_SOUND_GREEN)
      addEvent(doSendMagicEffect, 700, table_left_pos, CONST_ME_SOUND_GREEN)
      addEvent(doCreatureSay, 500, npc, "You win!", TALKTYPE_SAY, false, 0)
    elseif (roll == 2 and random_rollval >= 4) then
      placeMoney(total_g + total_new, table_middle_pos)
      addEvent(doSendMagicEffect, 400, table_left_pos, CONST_ME_SOUND_GREEN)
      addEvent(doSendMagicEffect, 700, table_left_pos, CONST_ME_SOUND_GREEN)
      addEvent(doCreatureSay, 500, npc, "You win!", TALKTYPE_SAY, false, 0)
    else
      addEvent(doSendMagicEffect, 400, table_left_pos, CONST_ME_BLOCKHIT)
      addEvent(doSendMagicEffect, 700, table_left_pos, CONST_ME_BLOCKHIT)
      addEvent(doCreatureSay, 500, npc, "Better luck next time.", TALKTYPE_SAY, false, 0)
    end
    doCreatureSay(npc, string.format("%s rolled a %d.", getCreatureName(npc), random_rollval), TALKTYPE_ORANGE_1, false, 0, table_left_pos)
  else
    addEvent(doCreateItem, 100, 2160, cc_count, table_middle_pos)
    addEvent(doCreateItem, 150, 2152, pc_count, table_middle_pos)
    doCreatureSay(npc, "The minimum wager is 1,000 and the maximum wager is 1,000,000.", TALKTYPE_SAY, false, 0)
  end
  return true
end

function creatureSayCallback(cid, type, msg)
  -- NPC userdata instance
  local npc = getNpcCid()

  -- Game table position userdata instances
  local table_left_pos = {x = 32352, y = 32225, z = 7} -- Table Position in front of NPC
  local table_middle_pos = {x = 32353, y = 32225, z = 7} -- Table Position between NPC and Player

  -- Search for coins on the left and middle tables and create item userdata instances
  local table_middle_cc = getTileItemById(table_middle_pos, 2160)
  local table_middle_pc = getTileItemById(table_middle_pos, 2152)

  -- Other variables
  local cc_count = 0
  local pc_count = 0
  local ROLL, LOW, HIGH = 0, 1, 2
  posplayer = {x=32354, y=32226, z=7} -- Players Position
          local ppos = getPlayerPosition(cid)
  if ppos.x == posplayer.x and ppos.y == posplayer.y then
  if isInArray({"H", "HIGH", "high", "h"}, msg) then
    ROLL = HIGH
  elseif  isInArray({"L", "LOW", "l", "low"}, msg) then
    ROLL = LOW      
  else
    return false
  end
  if (table_middle_cc.uid ~= 0) then
    cc_count = table_middle_cc.type
    doTeleportThing(table_middle_cc.uid, table_left_pos)
    addEvent(delayMoneyRemoval, 300, 2160, table_left_pos)
  end
  if (table_middle_pc.uid ~= 0) then
    pc_count = table_middle_pc.type
    doTeleportThing(table_middle_pc.uid, table_left_pos)
    addEvent(delayMoneyRemoval, 300, 2152, table_left_pos)
  end
  addEvent(rollDice, 500, ROLL, cc_count, pc_count, table_left_pos, table_middle_pos, npc)
  else
    return false
  end
  return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Last edited:
I have a error with ruler 15, but why?
I use tfs 1.3 client 12.30


Lua Script Error: [Main Interface]
in a timer event called from:
(Unknown scriptfile)
data/npc/scripts/Dicer.lua:23: attempt to call method 'getUniqueId' (a nil value)
stack traceback:
[C]: in function 'getUniqueId'
data/npc/scripts/Dicer.lua:23: in function <data/npc/scripts/Dicer.lua:15>

But when I talk to npc, everything works. I win, get money and when I lose, he takes o_O

In Dicer.lua i have the same like you
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
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

function delayMoneyRemoval(item, pos)
  doRemoveItem(getTileItemById(pos, item).uid)
  return true
end

function doCreateItem(itemid, count, pos)
  local tile = Tile(pos)
  if not tile then
      return false
  end

  local item = Game.createItem(itemid, count, pos)
  if item then
      return item:getUniqueId()
  end
  return false
end

function placeMoney(amount, table_middle_pos)
  local remain = amount
  local crystal_coins = 0
  local platinum_coins = 0

  if (math.floor(amount / 10000) >= 1) then
    crystal_coins = math.floor(amount / 10000)
    remain = remain - crystal_coins * 10000
  end
  if ((remain / 100) >= 1) then
    platinum_coins = remain / 100
  end
  addEvent(doCreateItem, 550, 2152, platinum_coins, table_middle_pos)
  addEvent(doCreateItem, 600, 2160, crystal_coins, table_middle_pos)
end

function rollDice(roll, cc_count, pc_count, table_left_pos, table_middle_pos, npc)
  local dice_ids = {5792, 5793, 5794, 5795, 5796, 5797}
  local random_rollval = math.random(1,6)
  local total_g = (10000 * cc_count) + (100 * pc_count)
  local prize_percent = 0.8 -- 80%
  local total_new = total_g * prize_percent
  if ((total_g) <= 1000000 and (total_g) >= 1000) then
    doSendMagicEffect(table_left_pos, CONST_ME_CRAPS)
    for _, itemId in pairs(dice_ids) do
      if(getTileItemById(table_left_pos, itemId).uid > 0) then
        doTransformItem(getTileItemById(table_left_pos, itemId).uid, dice_ids[random_rollval])
      end
    end
    if (roll == 1 and random_rollval <= 3) then
      placeMoney(total_g + total_new, table_middle_pos)
      addEvent(doSendMagicEffect, 400, table_left_pos, CONST_ME_SOUND_GREEN)
      addEvent(doSendMagicEffect, 700, table_left_pos, CONST_ME_SOUND_GREEN)
      addEvent(doCreatureSay, 500, npc, "You win!", TALKTYPE_SAY, false, 0)
    elseif (roll == 2 and random_rollval >= 4) then
      placeMoney(total_g + total_new, table_middle_pos)
      addEvent(doSendMagicEffect, 400, table_left_pos, CONST_ME_SOUND_GREEN)
      addEvent(doSendMagicEffect, 700, table_left_pos, CONST_ME_SOUND_GREEN)
      addEvent(doCreatureSay, 500, npc, "You win!", TALKTYPE_SAY, false, 0)
    else
      addEvent(doSendMagicEffect, 400, table_left_pos, CONST_ME_BLOCKHIT)
      addEvent(doSendMagicEffect, 700, table_left_pos, CONST_ME_BLOCKHIT)
      addEvent(doCreatureSay, 500, npc, "Better luck next time.", TALKTYPE_SAY, false, 0)
    end
    doCreatureSay(npc, string.format("%s rolled a %d.", getCreatureName(npc), random_rollval), TALKTYPE_ORANGE_1, false, 0, table_left_pos)
  else
    addEvent(doCreateItem, 100, 2160, cc_count, table_middle_pos)
    addEvent(doCreateItem, 150, 2152, pc_count, table_middle_pos)
    doCreatureSay(npc, "The minimum wager is 1,000 and the maximum wager is 1,000,000.", TALKTYPE_SAY, false, 0)
  end
  return true
end

function creatureSayCallback(cid, type, msg)
  -- NPC userdata instance
  local npc = getNpcCid()

  -- Game table position userdata instances
  local table_left_pos = {x = 32352, y = 32225, z = 7} -- Table Position in front of NPC
  local table_middle_pos = {x = 32353, y = 32225, z = 7} -- Table Position between NPC and Player

  -- Search for coins on the left and middle tables and create item userdata instances
  local table_middle_cc = getTileItemById(table_middle_pos, 2160)
  local table_middle_pc = getTileItemById(table_middle_pos, 2152)

  -- Other variables
  local cc_count = 0
  local pc_count = 0
  local ROLL, LOW, HIGH = 0, 1, 2
  posplayer = {x=32354, y=32226, z=7} -- Players Position
          local ppos = getPlayerPosition(cid)
  if ppos.x == posplayer.x and ppos.y == posplayer.y then
  if isInArray({"H", "HIGH", "high", "h"}, msg) then
    ROLL = HIGH
  elseif  isInArray({"L", "LOW", "l", "low"}, msg) then
    ROLL = LOW     
  else
    return false
  end
  if (table_middle_cc.uid ~= 0) then
    cc_count = table_middle_cc.type
    doTeleportThing(table_middle_cc.uid, table_left_pos)
    addEvent(delayMoneyRemoval, 300, 2160, table_left_pos)
  end
  if (table_middle_pc.uid ~= 0) then
    pc_count = table_middle_pc.type
    doTeleportThing(table_middle_pc.uid, table_left_pos)
    addEvent(delayMoneyRemoval, 300, 2152, table_left_pos)
  end
  addEvent(rollDice, 500, ROLL, cc_count, pc_count, table_left_pos, table_middle_pos, npc)
  else
    return false
  end
  return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

Check the item class. It may have been changed to getUID instead of getUniqueID. Not sure.
 
Back
Top