• 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 First script please correct me.

Elite Questbane

Sylphera.com
Joined
Mar 11, 2008
Messages
185
Reaction score
0
Location
Sweden
I have during a long time wanted to learn lua and today I took the first step reading CAREFULLY through the tutorials. Now I finished my first script and I have NO idea if this is fucked up or if it's working. Please correct the script if I made any mistake

How's what I believe the script is working.

1. I pull a lever and the scripts check my level, and if I am lvl 50 or higher then a teleport comes up. At the same time a text message pops up "Escape has been made!"
2. If you are not lvl 50 or higher another message comes up, "You dont..blabla"
3. When the lever is pulled and you pull it back the teleports dissapear and a message comes up "Your escape failed!" if your not lvl 50 or higher a message will come "You dont.. blabla"

Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
	      if item.itemid == 1945 then
	      		    if getPlayerLevel(cid) >= 50 then
		                    doCreateTeleport(itemid, topos, createpos)
		                    doPlayerSendTextMessage(cid,21,"Escape has been made!")
		            else
		    	              doPlayerSendCancel(cid,"You don't have the required level.")
		    	      end
		    elseif item.itemid == 1946 then
		    		    if getPlayerLevel(cid) >= 50 then
		    		    			  doPlayerSendTextMessage(cid,21,"Your escape failed!")
		    		    else
		    		    			  doPlayerSendCancel(cid,"You don't have the required level.")
		    		    end
		    end
		    return TRUE
end
 
Last edited by a moderator:
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)

local toPos = {x=1234, y=1234, z=7} -- position that player will go on
local createPos = {x=4321, y=4321, z=7} -- where the teleport will be created

	      if item.itemid == 1945 then
	      		    if getPlayerLevel(cid) >= 50 then
		                    doCreateTeleport(1387, toPos, createPos)
		                    doPlayerSendTextMessage(cid,21,"Escape has been made!")
		            else
		    	              doPlayerSendCancel(cid,"You don't have the required level.")
		    	      end
		    elseif item.itemid == 1946 then
		    		    if getPlayerLevel(cid) >= 50 then
		    		    			  doPlayerSendTextMessage(cid,21,"Your escape failed!")
		    		    else
		    		    			  doPlayerSendCancel(cid,"You don't have the required level.")
		    		    end
		    end
		    return TRUE
end

Change this:
Code:
local toPos = [COLOR="Red"]{x=1234, y=1234, z=7}[/COLOR] -- position that player will be teleported
local createPos = [COLOR="Blue"]{x=4321, y=4321, z=7}[/COLOR] -- where the teleport will be created

Regards.
 
And...
After:
Lua:
doPlayerSendTextMessage(cid,21,"Your escape failed!")
You should put:
Lua:
doTransformItem(item.uid,item.itemid + 1)

And..
After:
Lua:
doPlayerSendTextMessage(cid,21,"Escape has been made!")
You should put:
Lua:
doTransformItem(item.uid,item.itemid - 1)
 
So it should be like..

Lua:
  function onUse(cid, item, fromPosition, itemEx, toPosition)

local toPos = {x=XXXX, y=YYYY, z=Z}
local createPos = {x=XXXX, y=YYYY, z=Z}

              if item.itemid == 1945 then
                            if getPlayerLevel(cid) >= 50 then
                                    doCreateTeleport(1387, toPos, createPos)
                                    doPlayerSendTextMessage(cid,21,"Escape has been made!")
                                    doTransformItem(item.uid,item.itemid + 1)
                            else
                                      doPlayerSendCancel(cid,"You don't have the required level.")
                              end
                    elseif item.itemid == 1946 then
                                    if getPlayerLevel(cid) >= 50 then
                                                          doPlayerSendTextMessage(cid,21,"Your escape failed!")
                                                          doTransformItem(item.uid,item.itemid - 1)
                                    else
                                                          doPlayerSendCancel(cid,"You don't have the required level.")
                                    end
                    end
                    return TRUE
end


???
 
Last edited:
Hm, I have optimised this script a bit :p so it looks now cleaner and shorter.
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
	local toPos = {x=XXXX, y=YYYY, z=Z}
	local createPos = {x=XXXX, y=YYYY, z=Z}
	if getPlayerLevel(cid) >=50 then
		if item.itemid == 1945 then
			doCreateTeleport(1387, toPos, createPos)
			doPlayerSendTextMessage(cid, 21, "Escape has been made!")
			doTransformItem(item.uid, item.itemid - 1)
		elseif item.itemid == 1946 then
			doPlayerSendTextMessage(cid, 21, "Your escape failed!")
			doTransformItem(item.uid, item.itemid + 1)
		end
	else
		doPlayerSendCancel(cid, "You don't have the required level.")
	end
    return TRUE
end
 
@up:
Nope.
The function doTransformItem... are changed. Because if you use the item 1945 it will transform into 1944;also, if you use the item 1946 it will transform into 1947, so there's no script to those itemIDs.
It must be like this:
Lua:
  function onUse(cid, item, fromPosition, itemEx, toPosition)
        local toPos = {x=XXXX, y=YYYY, z=Z}
        local createPos = {x=XXXX, y=YYYY, z=Z}
        if getPlayerLevel(cid) >=50 then
                if item.itemid == 1945 then
                        doCreateTeleport(1387, toPos, createPos)
                        doPlayerSendTextMessage(cid, 21, "Escape has been made!")
                        doTransformItem(item.uid, item.itemid + 1)
                elseif item.itemid == 1946 then
                        doPlayerSendTextMessage(cid, 21, "Your escape failed!")
                        doTransformItem(item.uid, item.itemid - 1)
                end
        else
                doPlayerSendCancel(cid, "You don't have the required level.")
        end
    return TRUE
end
 
My help-tutorial.

Code:
local config = {
toPos = {x=XXXX, y=YYYY, z=Z},  [COLOR="Red"]--[Where should the tp take you?][/COLOR]
createPos = {x=XXXX, y=YYYY, z=Z},  [COLOR="Red"]--[Where should the tp be created?][/COLOR]
tpid = 1387,  [COLOR="Red"]--[Id of the teleport][/COLOR]
globalid = XXXX,  [COLOR="Red"]--[globalID the script use to see if someone else is making the {quest?}, prevents the tp to be removed because of someone else][/COLOR]
timestorage = XXXX,  [COLOR="Red"]--[storageID the timer checks if you have entered the tp][/COLOR]
createmsg = "Escape way has been made!",
failmsg = "Your escape succeded!",
failmsg = "Your escape failed!",
busymsg = "Someone else is trying to escape, try again later!",
lowlvlmsg = "You don't have the required level! (Level 50)",  [COLOR="Red"]--[Maybe they want to know what level they need?][/COLOR]
time = 300  [COLOR="Red"]--[Time in seconds the player have to get in the tp][/COLOR]
}    [COLOR="Red"]--[local config = {} is a way to make it more simple for other ppl to edit the script, maybe for yourself too. But i the beginning it might be harder to make scripts with this way. you maybe forget to add [COLOR="Blue"]config.***[/COLOR]][/COLOR]


function onUse(cid, item, fromPosition, itemEx, toPosition)
  if getPlayerLevel(cid) >=50 then  [COLOR="Red"]--[check level][/COLOR]
    if getGlobalStorageValue(config.globalid) <= 0 then  [COLOR="Red"]--[if another player is here = no then]
[/COLOR]
      if item.itemid == 1945 then  [COLOR="Red"]--[if itemid you use = 1945 (lever point left) then][/COLOR]
        doCreateTeleport(tpid, config.toPos, config.createPos)  [COLOR="Red"]--[create teleport(id, where you get, where tp is)][/COLOR]
        doPlayerSendTextMessage(cid, 21, config.createmsg)  [COLOR="Red"]--[send createmsg][/COLOR]
        setPlayerStorageValue(cid, config.timestorage, 1)  [COLOR="Red"]--[set storagevalue that shows [COLOR="Blue"]YOU[/COLOR] pulled the lever][/COLOR]
        setGlobalStorageValue(config.globalid, 1)  [COLOR="Red"]--[set globalStorageId that make the [COLOR="Blue"]{quest?}[/COLOR] "busy"][/COLOR]
        doTransformItem(item.uid, item.itemid + 1)  [COLOR="Red"]--[transform item.uid(thing you use) to item.itemid + 1 (item you use-id + 1)][/COLOR]
        addEvent(doPlayerFail, time * 1000)  [COLOR="Red"]--[addEvent = start another script(script to start, when it should start)   when it should start: 1000 = 1 second, i made a timer, time in config * 1000][/COLOR]

      elseif item.itemid == 1946 then  [COLOR="Red"]--[same as before but lever points right][/COLOR]
        doCreateTeleport(tpid, config.toPos, config.createPos)
        doPlayerSendTextMessage(cid, 21, config.createmsg)
        setPlayerStorageValue(cid, config.timestorage, 1)
        setGlobalStorageValue(config.globalid, 1)
        doTransformItem(item.uid, item.itemid - 1)
        addEvent(doPlayerFail, time * 1000)
      end

    else  [COLOR="Red"]--[if global storage is over 0 then][/COLOR]
      doPlayerSendTextMessage(cid, 21, config.busymsg)  [COLOR="Red"]--[send player message, busymsg][/COLOR]
    end

  else  [COLOR="Red"]--[if player level is under 50 then][/COLOR]
    doPlayerSendTextMessage(cid, 21, config.lowlvlmsg)  [COLOR="Red"]--[send player message, lowlvlmsg][/COLOR]
  end
  return TRUE
end


function doPlayerFail(cid)  [COLOR="Red"]--[You can say this is another "script", it checks if player entered the tp][/COLOR]
  gettp = getThingfromPos(config.createpos)  [COLOR="Red"]--[check what is on "createpos"][/COLOR]
  if gettp.itemid == tpid then  [COLOR="Red"]--[if item on createpos == tpid then][/COLOR]
    doRemoveItem(gettp.uid, 1)  [COLOR="Red"]--[remove the tp][/COLOR]
    if getPlayerStorageValue(cid, config.timestorage) == > 0 then  [COLOR="Red"]--[if player have storageid over 0 then][/COLOR]
      doPlayerSendTextMessage(cid, 21, config.failmsg)  [COLOR="Red"]--[send player message, failmsg][/COLOR]
      setPlayerStorageValue(cid, config.timestorage, 0)  [COLOR="Red"]--[set storageid to 0][/COLOR]
        setGlobalStorageValue(config.globalid, 0)  [COLOR="Red"]--[set globalid to 0 (makes the quest possible for other players or you again][/COLOR]
    else
      doPlayerSendTextMessage(cid, 21, config.successmsg)  [COLOR="Red"]--[if you entered tp, you will get message, successmsg][/COLOR]
      setGlobalStorageValue(config.globalid, 0)  [COLOR="Red"]--[set globalid to 0 (makes the quest possible for other players][/COLOR]
    end
  end
  return TRUE
end

I know this looks advanced, but if you read the comments, look how the script is build, and make more scripts (mabye not like mine, but slowly more advanced) you can become a really great scripter one day.

My biggest changes in your script is: I added a timer that removes the TP, also only one person can make the quest(I guess it's for a quest) at the time. Mabye you learn the addEvent() faster then me. :)
 
Last edited:
Back
Top