fix a onKill function for Lucky coin system please..

oshrigames

Intermediate OT User
Joined
Nov 9, 2012
Messages
206
Best answers
0
Reaction score
37
Location
israel
Original post:

the Issue: lucky coins don't drop.

TFS 1.2, format 10.98.

i think the issue is in here?

Code:
--: Configs  :--
local storage = 32143 --: Script Storage
local msg = "You have found a lucky coin, say !luckycoins for more info." --: Message when a player find a lucky coin
local showqtd = 1 --: Show coin quantity on above message? 1-Yes, 0-No
local qtdtext = "You have" --: Text can be changed here for translation

--: VISUAL :--
local enableeffect = 1 --: Show effects when user get a coin? 1-Yes, 0-No
local effect = CONST_ME_FERUMBRAS --: Effect to show, if enableeffect = 1

--: RATES :--
local boost = 5 --: Boots lucky coins rate by this value, don't works with ignorecfg/anycrate yet. 1 = 1x, 2 = 2x etc...
local anycreature = 1 --: All creatures can drop lucky coins. 0 - No, 1 - Yes.
local anycrate = 20 --: Lucky Coins drop rate on every creature 200 = 0,005%
local ignorecfg = 1 --: Ignore rates of below table? 1-Yes, 0-No

--: Note: with ignorecfg = 1 all rates will be from anycrate value

--: Change the monster, the name and the drop rate  :--
local config = {
  -- Higher value = Low drop rate. Drop Rate = 1 / chance
  ["Demon"] = {chance = 600}, -- 600 = 1/600
  ["Ferumbras"] = {chance = 30}, -- 30 = 1/30 etc
  ["Rat"] = {chance = 1}, -- 100% drop rate
  ["Rotworm"] = {chance = 2}, -- 50% drop rate
  ["Dog"] = {chance = 10} -- 10% drop rate
}

function onKill(cid, target, lastHit)
  if (isPlayer(target)) then return true end
  local monster = getCreatureName(target)
  local rand

  if (ignorecfg ~= 1) then
    for index, arraymonster in ipairs(config) do
      if ((arraymonster ~= monster) and (anycreature == 0)) then return true end
    end
    if (((config[monster])) == nil) then rand = anycrate else rand = ((config[monster].chance) / boost) end
  else
    rand = anycrate
  end

  local storageatual = getPlayerStorageValue(cid, storage)
  local plural = "s"
  local qtd = ""
  if (rand < 1) then rand = 1 end
  if (storageatual == 1) then    plural = ""    end
  if (showqtd == 1) then qtd = " " .. qtdtext .. " " .. storageatual + 1 .. " lucky coin" .. plural .. "." end

  if  (math.random(rand) == 1) then
    setPlayerStorageValue(cid, storage, storageatual + 1)
    doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, msg .. qtd)
    if (enableeffect == 1) then doSendMagicEffect(getPlayerPosition(cid), effect) end
  end

  return true
end
 

Dries390

Excellent OT User
Joined
Sep 8, 2007
Messages
19
Best answers
0
Reaction score
2
If it's anything like older distributions it seems a registration in login.lua (creaturescripts/scripts/login.lua) might be missing. You should probably add

Lua:
registerCreatureEvent(cid, "Luckypoints")
to the file.

Edit: fixed name.
 
OP
oshrigames

oshrigames

Intermediate OT User
Joined
Nov 9, 2012
Messages
206
Best answers
0
Reaction score
37
Location
israel
ive just tested with that in login.lua.
still no drop.

ill leave it in there for now since i don't get error, and thats the issue no error so i have no clue how to find the problem. ;c
 

Awesomedudei

Revolutionot.com
Joined
Jan 20, 2010
Messages
294
Best answers
0
Reaction score
46
Location
Sweden
I would also greatly appreciate if someone could fix this, i've tried multiple times for my server but yeah something is either missing or broken. ❤
 

Xikini

I whore myself out for likes
Premium User
Joined
Nov 17, 2010
Messages
4,172
Best answers
174
Reaction score
2,233
ive just tested with that in login.lua.
still no drop.

ill leave it in there for now since i don't get error, and thats the issue no error so i have no clue how to find the problem. ;c
I would also greatly appreciate if someone could fix this, i've tried multiple times for my server but yeah something is either missing or broken. ❤
Okay.. well I don't see anything wrong with the script right away....
So let's put some basic prints into the script and find out where it's breaking?

Make sure to add that line into login.lua from this post.

Lua:
--: Configs  :--
local storage = 32143 --: Script Storage
local msg = "You have found a lucky coin, say !luckycoins for more info." --: Message when a player find a lucky coin
local showqtd = 1 --: Show coin quantity on above message? 1-Yes, 0-No
local qtdtext = "You have" --: Text can be changed here for translation

--: VISUAL :--
local enableeffect = 1 --: Show effects when user get a coin? 1-Yes, 0-No
local effect = CONST_ME_FERUMBRAS --: Effect to show, if enableeffect = 1

--: RATES :--
local boost = 1 --: Boots lucky coins rate by this value, don't works with ignorecfg/anycrate yet. 1 = 1x, 2 = 2x etc...
local anycreature = 1 --: All creatures can drop lucky coins. 0 - No, 1 - Yes.
local anycrate = 20 --: Lucky Coins drop rate on every creature 200 = 0,005%
local ignorecfg = 1 --: Ignore rates of below table? 1-Yes, 0-No

--: Note: with ignorecfg = 1 all rates will be from anycrate value

--: Change the monster, the name and the drop rate  :--
local config = {
    -- Higher value = Low drop rate. Drop Rate = 1 / chance
    ["Demon"] = {chance = 600}, -- 600 = 1/600
    ["Ferumbras"] = {chance = 30}, -- 30 = 1/30 etc
    ["Rat"] = {chance = 1}, -- 100% drop rate
    ["Rotworm"] = {chance = 2}, -- 50% drop rate
    ["Dog"] = {chance = 10} -- 10% drop rate
}

function onKill(cid, target, lastHit)
    print("script starting..")
    if (isPlayer(target)) then
        return true
    end
    local monster = getCreatureName(target)
    local rand
 
    if (ignorecfg ~= 1) then
        print("not ignoring rates of table..")
        for index, arraymonster in ipairs(config) do
            print("looping through table.. index = " .. index .. " | arraymonster = " .. arraymonster .. "")
            if ((arraymonster ~= monster) and (anycreature == 0)) then -- if array does not equal monster_name.. and not all creatures can drop coins.. then end script? this is stupid.
                print("Monster not found. Script ends prematurely..? Why is this return true even here? Wtf is this entire loop doing?")
                return true
            end
        end
        if (((config[monster])) == nil) then
            print("monster not in config table.. defaulting to anycrate value")
            rand = anycrate
        else
            print("monster found in config table.. setting chance value")
            rand = ((config[monster].chance) / boost)
        end
    else
        print("ignoring config table.. defaulting to anycrate value")
        rand = anycrate
    end
 
    local storageatual = getPlayerStorageValue(cid, storage)
    local plural = "s"
    local qtd = ""
    if (rand < 1) then
        print("minimum value is 1. rand is less then 1, so defaulting. seems to be a safeguard, probably won't see this text.")
        rand = 1
    end
    if (storageatual == 0) then -- edited this line old text -> if (storageatual == 1) then <-- if it's set to 1, then the plural text would be wrong later on.. because anything higher then 1 would be plural.. and it's adding + 1 to value later on.
        print("only one coin, getting rid of plural text.")
        plural = ""
    end
    if (showqtd == 1) then
        print("text with coin value getting sent to player gets appended to end of text")
        qtd = " " .. qtdtext .. " " .. storageatual + 1 .. " lucky coin" .. plural .. "."
    end
 
    if (math.random(rand) == 1) then
        print("coin obtained!")
        setPlayerStorageValue(cid, storage, storageatual + 1)
        doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, msg .. qtd)
        if (enableeffect == 1) then
            doSendMagicEffect(getPlayerPosition(cid), effect)
        end
    else -- added this for testing.
        print("coin NOT obtained.") -- added this for testing.
    end
   
    print("script finished successfully.")
    return true
end
 
Last edited:
OP
oshrigames

oshrigames

Intermediate OT User
Joined
Nov 9, 2012
Messages
206
Best answers
0
Reaction score
37
Location
israel
i just finish run all the test. seems like the OP from original post forgot to tell us to add

to login.lua
Lua:
    -- lucky coins
    player:registerEvent("LuckyPoints")

NOTE: i tried with this
registerCreatureEvent(cid, "Luckypoints")
but its seems like tfs 1.2 not recenzise it or something.

@Awesomedudei
i tag you so you know the fix.. good luck
 
Last edited:
Top