• 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][lua][actions] script building problem

Belciu

New Member
Joined
Apr 14, 2009
Messages
9
Reaction score
0
Hello dear scripters :)

Im beginner in lua, i started writting in this language from yesterday xD

I made my second script and it's not working. Could someone explain me whats wrong and correct it?

PHP:
local level = 1
function onUse(cid, item, fromPosition, itemEx, toPosition)
    if itemEx.itemid == 2283 then 
    level = getPlayerLevel(cid) 
    local cc = (level / 10) 
    math.ceil(cc)
    doPlayerAddItem(cid,2160,cc)
	doSendMagicEffect(getPlayerPosition(cid), CONST_ME_MAGIC_RED)
	doPlayerSendTextMessage(cid,MESSAGE_INFO_DESCR, "You gain "..cc.." as a reward!")
	doRemoveItem(item.uid)
	else
	doPlayerSendTextMessage(cid,MESSAGE_INFO_DESCR, "You cant use it.")
    end
end

Script is checking character (cid) level.
participate level by 10 ( level / 10 )
Nextly getting resault and integer it up ex.(if 1.5 get to 2.0 [math.ceil("..cc..")])
And giving player as much cc as is in the resault (cc)
 
Last edited:
ItemEx means you use a item on another item.
example: shovel(item) used on ground(itemEx)

Or even easier, when you get crosshair in-game and click on a item, that item you just clicked is the itemEx.

I think you ment that you use a item and you get money + item is removed.

I will show you 2 examples.
The first one is yours, but I fixed it, added a config. (I explain how it works in the bottom of this post)

------------------------------------------------------------
You use an item with crosshair on the rune.
<action itemid="item" event="script" value="script.lua"/>

Code:
Local config = {
    level = getPlayerLevel(cid),
    cc = level / 10
}
function onUse(cid, item, fromPosition, itemEx, toPosition)
  if [COLOR="Red"]itemEx[/COLOR].itemid == 2283 then
    doPlayerAddItem(cid, 2160 ,config.cc)
    doSendMagicEffect(getPlayerPosition(cid), CONST_ME_MAGIC_RED)
    doPlayerSendTextMessage(cid,MESSAGE_INFO_DESCR, "You gain " .. config.cc .. " crystal coins as a reward!")
    doRemoveItem(item.uid)
    else
    doPlayerSendTextMessage(cid,MESSAGE_INFO_DESCR, "You cant use it.")
  end
  return TRUE
end
------------------------------------------------------------
You use the rune. (no need of item.itemid in script, you put id in actions.xml)
<action itemid="2283" event="script" value="script.lua"/>

Code:
Local config = {
    level = getPlayerLevel(cid),
    cc = level / 10
}
function onUse(cid, item, fromPosition, itemEx, toPosition)
  doPlayerAddItem(cid, 2160 ,config.cc)
  doSendMagicEffect(getPlayerPosition(cid), CONST_ME_MAGIC_RED)
  doPlayerSendTextMessage(cid,MESSAGE_INFO_DESCR, "You gain " .. config.cc .. " crystal coins as a reward!")
  doRemoveItem(item.uid)
  return TRUE
end
PS. You better change to a item that uses no crosshair, easier.
PS. You better change to a item that uses no crosshair, easier.
PS. You better change to a item that uses no crosshair, easier.
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------


Code:
local [COLOR="Red"]config[/COLOR] = {    --[The "{" opens the config]--
  [COLOR="Blue"]storage[/COLOR] = 12345,  --[Don't forget to put a "," after the numbers/monster name etc.]--
  [COLOR="SeaGreen"]reward[/COLOR] = 2160, --[crystal coin]--
  [COLOR="Blue"]count[/COLOR] = 10  --[since it's the last row in the config: [COLOR="Red"][B]DONT[/B][/COLOR] use "," here]--
}  --[The "}" closes the config]--

Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)
  if getPlayerStorageValue(cid, [COLOR="Red"][B]config.[/COLOR][COLOR="Blue"]storage[/B][/COLOR]) < 0 then  --[to get the storage from our config write "[COLOR="Red"]config.[/COLOR]storage" or what line u want]--
    doPlayerAddItem(cid, [COLOR="Red"][B]config.[/COLOR][COLOR="SeaGreen"]reward[/B][/COLOR], [COLOR="Red"][B]config.[/COLOR][COLOR="Blue"]count[/B][/COLOR])
    doRemoveItem(item.uid)
  else
    doPlayerSendTextMessage(cid,MESSAGE_INFO_DESCR, "You have already gotten your reward.")
  end
  return TRUE
end
 
There was something worng with the array (local config) so i changed it to this, and now it works:)

PHP:
function onUse(cid, item, fromPosition, toPosition)
  level = getPlayerLevel(cid)
  cc = math.ceil(level / 10)
  doPlayerAddItem(cid, 2160 ,cc)
  doSendMagicEffect(getPlayerPosition(cid), CONST_ME_MAGIC_RED)
  doPlayerSendTextMessage(cid,MESSAGE_INFO_DESCR, "You gain " .. cc .. " crystal coins as a reward!")
  doRemoveItem(item.uid)
  return TRUE
end

Thanks for help!
btw. Do you know whats wrong with this?
PHP:
Local config = {
   (level = getPlayerLevel(cid)),
   (cc = level / 10)
}
If im using this table array with normal locals like for example:
PHP:
reward = 2160,
  count = 10
Is working fine, but when i try to put in array function like,
PHP:
(level = getPlayerLevel(cid)) and/or level = getPlayerLevel(cid)
i got error in console,
PHP:
[Warning - Event::loadScript] Cannot load script (data/actions/scripts/presentrune.lua)
data/actions/scripts/presentrune.lua:1: '=' expected near 'config'


Engine: tfs 0.3.5pl1,
platform: debian,
edited sources for high server security
 
Problem solved.

this structure:
PHP:
Local config = {
    level = getPlayerLevel(cid),
    cc = level / 10
}

Should be under function :)

PS sory for double post.
 
Code:
-- Cykestyled
function onUse(cid, item, fromPosition, itemEx, toPosition)
	if (itemEx.itemid == 2283) then
		return doPlayerAddItem(cid, 2160, math.ceil(getPlayerLevel(cid) / 10) < 100 or 100), doSendMagicEffect(getCreaturePosition(cid), CONST_ME_MAGIC_RED), doRemoveItem(item.uid)
	end
	return true
end
 
Code:
-- Cykestyled
function onUse(cid, item, fromPosition, itemEx, toPosition)
	return itemEx.itemid == 2283 and true and doPlayerAddItem(cid, 2160, math.ceil(getPlayerLevel(cid) / 10) < 100 or 100) and doSendMagicEffect(getCreaturePosition(cid), CONST_ME_MAGIC_RED) and doRemoveItem(item.uid) or false
end
true might not be necessary, but just in case nothing else returns true.
 
Hehe, short coder, but still to "advanced" that i want to learn it atm xD
Atleast he can learn some more basics. The "local config {}". :)
 
Back
Top