• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua

Lucenzo

The Way You Move :)
Joined
Jan 29, 2011
Messages
363
Reaction score
14
Location
UK
Heya


So i decided that i will try to learn some LUA, but got no idea where to begin.

I was thinking i would start by editing some simple scripts and then writting my own.


Any other ideas, suggested webs or such? :P

Thanks x x
 
Instead of reading tutorials, look around and try to understand the syntax of this simple scripting language

However if you need a tutorial on something specific, you could always pay a visit to http://lua-users.org/wiki/TutorialDirectory
Don't expect to learn how OT-Lua functions work by reading those tutorials (they're not a part of standard Lua, obviously), though, but it'll help you make more efficient scripts overall.
 
was wondering if you could explain to me the whole end function and return true/false.

How does the
LUA:
end
return true
end

or
LUA:
        end 
   end 
end

Confused abit about these, does it work like each end is for a line of code or?
Sometimes i see scripts with 3 ends or 2 ends, which is what i'd like to know :P
 
Every function, if, for and while statement needs to be closed with an end. You can make the job easier for yourself if you tab your scripts properly.

LUA:
function dummy(param1, param2)

	for i = 1, 10 do
		print('Iteration: ' .. i)
	end
	
	local random = math.random(2, 6)
	if random >= 3 and random <= 5 then
		print('Lucky number was between 3 and 5.')
	elseif random == 2 then
		print('It was 2')
	else
		print('This happens when it\'s neither.')
	end

	local k = 5
	while k > 0 do
		print('Numbers left: ' .. k)
		k = k - 1
	end

	local day = os.date('%A')
	if day == 'Monday' then
		print('It\'s your lucky day!')
	else
		print('It\'s ' .. day .. ' today.')
	end

end

dummy() -- calling the function with no parameters
 
Cheers x

What about the locals, how do these work? would this work?
How would i put the locals under function? e.g

LUA:
local c = 20000
local l = 10

function onSay(cid, words, param)
  local ?

c is for cash and l is for level. ? :p
suppose to be aol script.
 
If you put them outside the function, they will always be loaded in RAM (this is generally preferred, but doesn't make much of a difference).
If they're inside it, they'll be declared again every time the function onSay is executed.

More about locals and scopes:
LUA:
if something ~= 1 then
	local variable = 'abc'
	print(variable) -- prints abc
end

print(variable) -- prints nil (null / undefined),
		-- since the variable was local, it was only available within that scope
Using global variables (without prefix 'local') is generally frowned upon, you should use locals whenever possible since they're faster to use, especially inside functions where you don't want the variables to remain declared for the next time your script is executed.
LUA:
local c = 20000
local l = 10

function onSay(cid, words, param)
  local ?

c is for cash and l is for level. ? :p
suppose to be aol script.
For such a simple script, there is really no need to declare any variables, especially if it's only going to get used internally, in a production environment:
LUA:
function onSay(cid, words, param, channel)
	if getPlayerLevel(cid) >= 10 then
		if doPlayerRemoveMoney(cid, 10000) then
			doPlayerAddItem(cid, 2173, 1)
			doCreatureSay(cid, 'You bought an Amulet of Loss.', TALKTYPE_ORANGE_1, false, cid)
			doSendMagicEffect(getThingPos(cid), CONST_ME_HOLYAREA)
		else
			doPlayerSendCancel(cid, 'You need 10000 gold to buy this..')
		end
	else
		doPlayerSendCancel(cid, 'You need Level 10 or higher to buy this.')
	end
	return true
end
 
Last edited:
would this work?
LUA:
local c = 20000
local v = 2173
local l = 10


function onSay(cid, words, param)
  if getPlayerLevel(cid) >= l then
	if doPlayerRemoveItem == c then
	doPlayerAddItem(cid, v, 1)
  doPlayerSendTextMessage(cid, 22, "You have purchased an Amulet of Loss.")
  else
  doPlayerSendCancel(cid, "You do not have enough money.")
  end
  else 
  doPlayerSendCancel(cid, "You need level 10 or higher to buy this.")
end
  return true
end
 
Last edited:
> if c == 20000 then
You have to use doPlayerRemoveItem here

You're also missing 1 end, right before that return true, to close if getPlayerLevel(cid) >= l then

LUA:
local c, v, l = 20000, 2173, 10

function onSay(cid, words, param, channel)
	if getPlayerLevel(cid) >= l then
		if doPlayerRemoveMoney(cid, c) then
			doPlayerAddItem(cid, v, 1)
			doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, 'You have purchased an Amulet of Loss.')
		else
			doPlayerSendCancel(cid, 'You do not have enough money, it costs ' .. c .. ' gold.')
		end
	else 
		doPlayerSendCancel(cid, 'You need level ' .. l .. ' or higher to buy this.')
	end
	return true
end
 
edited, that's ok?

But i get the point if single thing then better with numbers, if few then locals.
so the aol script above is crap :P no point in so many locals for it.
 
In fact, I only define locals if I have to use its value multiple times;
or if it's long, and better off being put into a variable
or if it's something resource intensive like a function, so it's better to define it once than call it every time you need to use it.
 
Any way to shorten this, and i know theres something missing. Can't figure out what it is thought :P
LUA:
local b, a, s = 1988, 2173, 2554

function onSay(cid, words, param)
    if words == '!backpack' then
    if getPlayerMoney(cid, 50) then
   if doPlayerRemoveMoney(cid, 50) then
   doPlayerAddItem(cid, b, 1)
    doPlayerSendTextMessage(cid, 22, "You have purchased a Backpack.")
   else
  doPlayerSendCancel(cid, "You do not have enough money.")
  end
    if words == '!aol' then
    if getPlayerMoney(cid, 20000) then
   if doPlayerRemoveMoney(cid, 20000) then
   doPlayerAddItem(cid, a, 1)
    doPlayerSendTextMessage(cid, 22, "You have purchased an Amulet of Loss.")
   else
  doPlayerSendCancel(cid, "You do not have enough money.")
  end
    if words == '!shovel' then
    if getPlayerMoney(cid, 25) then
   if doPlayerRemoveMoney(cid, 25) then
   doPlayerAddItem(cid, s, 1)
    doPlayerSendTextMessage(cid, 22, "You have purchased a Shovel.")
	else
  doPlayerSendCancel(cid, "You do not have enough money.")
  end
  return TRUE
end
 
Last edited:
It's called tables
LUA:
local t = {
	['!backpack'] = {1988, 50},
	['!aol'] = {2173, 20000},
	['!shovel'] = {2554, 25}
}
function onSay(cid, words, param, channel)
	local v = t[param]
	if doPlayerRemoveMoney(cid, v[2]) then
		doPlayerAddItem(cid, v[1], 1)
		v = getItemInfo(v[1])
		doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, 'You have purchased ' .. v.article .. ' ' .. v.name .. '.')
	else
		doPlayerSendCancel(cid, 'You need ' .. v[2] .. ' gold to buy this item.')
	end
	return true
end
 
It's called tables
LUA:
local t = {
	['!backpack'] = {1988, 50},
	['!aol'] = {2173, 20000},
	['!shovel'] = {2554, 25}
}
function onSay(cid, words, param, channel)
	local v = t[param]
	if doPlayerRemoveMoney(cid, v[2]) then
		doPlayerAddItem(cid, v[1], 1)
		v = getItemInfo(v[1])
		doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, 'You have purchased ' .. v.article .. ' ' .. v.name .. '.')
	else
		doPlayerSendCancel(cid, 'You need ' .. v[2] .. ' gold to buy this item.')
	end
	return true
end


All good scripters know how to do that XD
Cykotitan is not crazy is good scripter =)
 
Back
Top