• 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 What i made worng? Open door if have item

warriorfrog

Active Member
Joined
Jul 29, 2015
Messages
334
Reaction score
35
I tried made it to
if door is closed

if u tried to open and have a scroll
ok, door will open and in 5 seconds it will close

else
u cant open the door

script (im not good in lua, i tried make it, cathing parts on web)
Code:
local open_door = 6258
local closed_door = 6257
local scroll_id = 1949

local function changeBack(Pos)
   doTransformItem(getTileItemById(Pos, open_door).uid, closed_door)
end

function onUse(cid, item, frompos, item2, toPosition)
   if(itemEx.itemid == closed_door) then
     if getPlayerItemCount(cid,scroll_id) > -1 then
       doTransformItem(itemEx.uid, open_door)
       addEvent(changeBack,5000,toPosition)
     else
       doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "You don't have the scroll password.")
     end
   end
end
 
Last edited:
And what happens if the door gets opened, some other person (other than the guy who used the door) stands in the doorway?

Yeah...I wasn't talking about the actual post itself. I was commenting on someones code. As shown in the quote in my previous comment. So, he made a code that will teleport (only the person standing in the correct position) Which obviously can be a problem. I simply put a code that doesn't work that way. Instead it does what he was trying to code.
 
Yeah...I wasn't talking about the actual post itself. I was commenting on someones code. As shown in the quote in my previous comment. So, he made a code that will teleport (only the person standing in the correct position) Which obviously can be a problem. I simply put a code that doesn't work that way. Instead it does what he was trying to code.

That's true, but this problem still remains, thus I recommended a way that would be a solution for both problems and you said it was bad code :p
 
I personally don't think posting a complete script is the proper way of helping someone.
Copy & paste only feeds to laziness and does not challenge the person in question to understand what is really going on.
This way of "helping" someone is actually not doing the person a favor at all since, if/when that person gets a bug later on, he/she must rely on someone else's help to fix it.

I guess I'm being a bit hyprocritical this time, but at least I've added comments that explains pretty much everything so you can learn something from it.

Code:
<action actionid="8047" script="password.lua"/>

Code:
local scroll_id = 1949
local p_1 = {x = 950, y = 1086, z = 6} -- 1 side of the door(think of p_1 as position_1)
local p_2 = {x = 950, y = 1088, z = 6} -- the opposite side of the door

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local pp = getPlayerPosition(cid) --[[Since the script will be using "getPlayerPosition(cid) a lot we might as well shorten it"
    (we put this inside the function, otherwise the script won't know who "cid" is referring to and it will cause errors)]]

    if getPlayerItemCount(cid, scroll_id) < 1 then -- if the player doesn't have a scroll then
        return doPlayerSendCancel(cid, "You need a password scroll to gain access to this door.") --"return" will stop the script from going further if the "if" statement is true.
    else -- if the "if" statement was not true then
        if pp.x == p_1.x and pp.y == p_1.y then -- if player position(pp) == position_1(p_1) then (we don't include "z" since you can't use a door from another floor)
            return doTeleportThing(cid, p_2) -- teleports the player to the other side of the door
        elseif pp.x == p_2.x and pp.y == p_2.y then
            return doTeleportThing(cid, p_1)
        else -- if the player is not standing in any of the positions(he/she is not standing infront of the door) then
            return doPlayerSendCancel(cid, "Stand in front of the door to use it")
        end
    end
end
 
I personally don't think posting a complete script is the proper way of helping someone.
Copy & paste only feeds to laziness and does not challenge the person in question to understand what is really going on.
This way of "helping" someone is actually not doing the person a favor at all since, if/when that person gets a bug later on, he/she must rely on someone else's help to fix it.

I guess I'm being a bit hyprocritical this time, but at least I've added comments that explains pretty much everything so you can learn something from it.

Code:
<action actionid="8047" script="password.lua"/>

Code:
local scroll_id = 1949
local p_1 = {x = 950, y = 1086, z = 6} -- 1 side of the door(think of p_1 as position_1)
local p_2 = {x = 950, y = 1088, z = 6} -- the opposite side of the door

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local pp = getPlayerPosition(cid) --[[Since the script will be using "getPlayerPosition(cid) a lot we might as well shorten it"
    (we put this inside the function, otherwise the script won't know who "cid" is referring to and it will cause errors)]]

    if getPlayerItemCount(cid, scroll_id) < 1 then -- if the player doesn't have a scroll then
        return doPlayerSendCancel(cid, "You need a password scroll to gain access to this door.") --"return" will stop the script from going further if the "if" statement is true.
    else -- if the "if" statement was not true then
        if pp.x == p_1.x and pp.y == p_1.y then -- if player position(pp) == position_1(p_1) then (we don't include "z" since you can't use a door from another floor)
            return doTeleportThing(cid, p_2) -- teleports the player to the other side of the door
        elseif pp.x == p_2.x and pp.y == p_2.y then
            return doTeleportThing(cid, p_1)
        else -- if the player is not standing in any of the positions(he/she is not standing infront of the door) then
            return doPlayerSendCancel(cid, "Stand in front of the door to use it")
        end
    end
end

This is a great way to provide scripts to someone if you're gonna do a full script indeed.
The way I understood OP, the door should open when you have the scroll and use it. This would allow the user and other players accompanying him to pass through the door, but the door closes automatically after a delay.

I think this should do the trick:

Code:
local open_door = 6258
local closed_door = 6257
local scroll_id = 1949

local function changeBack(pos)
   local pp = getThingFromPos(pos).uid
  
   if isPlayer(pp) then
     local destination = {x = 1000, y = 1000, z = 7}
     doTeleportThing(pp,destination)
   end

   doTransformItem(getTileItemById(pos, open_door).uid, closed_door)
end

function onUse(cid, item, frompos, item2, toPosition)
   if item.actionid == 7420 and item.itemid == closed_door and getPlayerItemCount(cid,scroll_id) >= 1 then
        doTransformItem(item.uid, open_door)
        addEvent(changeBack,5000,toPosition)
   else
     doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE,"You don't have the scroll password.")
   end
   return true
end

Code explained:

Code:
-- Configurations of local variables:
local open_door = 6258
local closed_door = 6257
local scroll_id = 1949
---------------------



local function changeBack(pos) -- Local function changeBack that uses 1 parameter named 'pos'. We will pass the door's position as the 'pos' parameter.
   local pp = getThingFromPos(pos).uid -- We will store whatever is on the door position into a variable "pp". If this is a player, that means he's standing inside the door. We need to kick him out somewhere so he doesn't get stuck in the "closed door" sprite when it closes.
  
   if isPlayer(pp) then -- If what we stored in "pp" is indeed a Player then
     local destination = {x = 1000, y = 1000, z = 7} -- We define a position where to kick the player
     doTeleportThing(pp,destination) -- We kick this player to a desired position
   end

   doTransformItem(getTileItemById(pos, open_door).uid, closed_door) -- We transform the door from an open door to a closed door.
end

function onUse(cid, item, frompos, item2, toPosition)
   if item.actionid == 7420 and item.itemid == closed_door and getPlayerItemCount(cid,scroll_id) >= 1 then -- If we are using a closed door, and the door has the action id 7420 and the player owns a password scroll then
        doTransformItem(item.uid, open_door) -- We open the door
        addEvent(changeBack,5000,toPosition) -- We add an event that after a delay of 5000 ms (5 seconds), the function called changeBack is called. We pass the "toPosition" value to that function. Since that function uses only 1 parameter called "pos", this means that toPosition from the main function will now be sent to changeBack as "pos" and used that way in there.
   else -- If the above circumstances are not met, then
     doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE,"You don't have the scroll password.") -- Send the player a message he doesn't have a scroll password.
   end
  
   return true
end
 
local open_door = 6258 local closed_door = 6257 local scroll_id = 1949 local function changeBack(pos) local pp = getThingFromPos(pos).uid if isPlayer(pp) then local destination = {x = 1000, y = 1000, z = 7} doTeleportThing(pp,destination) end doTransformItem(getTileItemById(pos, open_door).uid, closed_door) end function onUse(cid, item, frompos, item2, toPosition) if item.actionid == 7420 and item.itemid == closed_door and getPlayerItemCount(cid,scroll_id) >= 1 then doTransformItem(item.uid, open_door) addEvent(changeBack,5000,toPosition) else doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE,"You don't have the scroll password.") end return true end
Can anyone tell me why this might cause a problem?
 
Can anyone tell me why this might cause a problem?
I haven't tested it, but pretty sure it should work. However, if OP has player stacking (several people able to stand on the same tile at once) enabled, that might be a problem since the script will only tp 1 of them.
 
Back
Top