• 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!

Solved Trying to learn to script some

GOD Wille

Excellent OT User
Joined
Jan 11, 2010
Messages
2,826
Solutions
2
Reaction score
815
Location
Sweden
Hey, im trying to learn to script this;

if there is no bush on specific tile then add a bush
if there is a bush then remove the bush (if lever is 1946)
you understand!

please correct my script and tell me what i do wrong :)

LUA:
local config = {
    bushId = 2768,
    pos = {x=2424, y=946, z=7}
    }

function onUse(cid, item, fromPosition, itemEx, toPosition)
if item.id == 1945 then
    if getTileItemById(config.pos, config.bushId).uid > 0 then
        doTileAddItemEx(config.pos, config.bushId).uid
            doCreatureSay(cid, 'You summoned a bush!', TALKTYPE_ORANGE)
elseif item.id == 1946 then
    if getTileItemById(config.pos, config.bushId).uid < 0 then
        doCreatureSay(cid, 'You deleted the bush!', TALKTYPE_ORANGE)
            doRemoveItem(getTileItemById(config.pos, config.bushId).uid)
            else
            doPlayerSendCancel(cid, 'Sorry not possible')
            return false
            end
        end
    end
end

1d300702919c14c6e11c2b12bbe0bd1c.png
 
Last edited by a moderator:
Try this. @GOD Wille
LUA:
local config = {
bushId = 2768,
pos = {x=2424, y=946, z=7}
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
if item.id == 1945 then
if getTileItemById(config.pos, config.bushId).uid) > 0 then
doCreateItem(config.bushId, 1, config.pos)
doCreatureSay(cid, "You summoned a bush!", 19)
elseif item.id == 1946 then
if getTileItemById(config.pos, config.bushId).uid < 0 then
doCreatureSay(cid, "You deleted the bush!", 19)
doRemoveItem(getTileItemById(config.pos, config.bushId).uid)
else
doPlayerSendCancel(cid, 'Sorry not possible')
return false
end
end
end
end
 
Last edited by a moderator:
Okay, if you're gonna try to learn to script, recognizing errors is an absolute must.
Take a look at the error closely.

You'll see the first line of the error, which tells you there's an error when loading a file, then it tells you which file, followed by the line number.
'data/actions/scripts/bush.lua' is the file. The ':8' is the line number. So, in bush.lua at line 8, there is a 'then' that is expected.

That means, you have something wrong with 'then'.

Let's go back to the script:
LUA:
local config = {
bushId = 2768,
pos = {x=2424, y=946, z=7}
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if item.id == 1945 then
        if getTileItemById(config.pos, config.bushId).uid) > 0 then
            doCreateItem(config.bushId, 1, config.pos)
            doCreatureSay(cid, "You summoned a bush!", 19)
        elseif item.id == 1946 then
            if getTileItemById(config.pos, config.bushId).uid < 0 then
                doCreatureSay(cid, "You deleted the bush!", 19)
                doRemoveItem(getTileItemById(config.pos, config.bushId).uid)
            else
                doPlayerSendCancel(cid, 'Sorry not possible')
                return false
            end
        end
    end
end

Okay, we now see at line 8:
LUA:
        if getTileItemById(config.pos, config.bushId).uid) > 0 then

Oh! 'then' is there, but what could be the problem? Why is it not finding the 'then'?
Oh! you have a syntax error that occurs before 'then', look at the ')' after .uid. You have an unnecessary closing parenthesis.
 
Last edited by a moderator:
LUA:
local config = {
    bushId = 2768,
    pos = {x=2424, y=946, z=7}
    }

function onUse(cid, item, fromPosition, itemEx, toPosition)
if item.id == 1945 then
    if getTileItemById(config.pos, config.bushId).uid > 0 then
        doTileAddItemEx(config.pos, config.bushId).uid
            doCreatureSay(cid, 'You summoned a bush!', TALKTYPE_ORANGE)
elseif item.id == 1946 then
    if getTileItemById(config.pos, config.bushId).uid < 0 then
        doCreatureSay(cid, 'You deleted the bush!', TALKTYPE_ORANGE)
            doRemoveItem(getTileItemById(config.pos, config.bushId).uid
            else
            doPlayerSendCancel(cid, 'Sorry not possible')
            return false
            end
        end
    end
end

^updated script

thanks for reply!
alright so i fixed these errors but i do not understand this
4248dec71b5d86eaea17774ec00f814b.png
 
Last edited by a moderator:
Every block starting with an if must be enclosed with an end. The if statement on line 8 needs to be enclosed. There are four ends at the end but you only need three.

Also it should be item.itemid instead of item.id.

I don't think it's necessary to check whether the item you're using is item id 1945 or 1946 (it starts on id: 1945 anyway)
LUA:
local config = {
    bushId = 2768,
    pos = {x=2424, y=946, z=7}
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if getTileItemById(config.pos, config.bushId).uid < 0 then
        doCreateItem(config.bushId, 1, config.pos)
        doCreatureSay(cid, "You summoned a bush!", TALKTYPE_ORANGE_1)
    else
        doRemoveItem(getTileItemById(config.pos, config.bushId).uid)
        doCreatureSay(cid, "You deleted the bush!", TALKTYPE_ORANGE_1)
    end

    doTransformItem(item.uid, item.itemid == 1945 and 1946 or 1945)
    return true
end
 
Last edited by a moderator:
Code:
local config = {
    bushId = 2768,
    pos = {x=2424, y=946, z=7}
    }

function onUse(cid, item, fromPosition, itemEx, toPosition)
if item.id == 1945 then
    if getTileItemById(config.pos, config.bushId).uid > 0 then
        doTileAddItemEx(config.pos, config.bushId).uid
            doCreatureSay(cid, 'You summoned a bush!', TALKTYPE_ORANGE)
elseif item.id == 1946 then
    if getTileItemById(config.pos, config.bushId).uid < 0 then
        doCreatureSay(cid, 'You deleted the bush!', TALKTYPE_ORANGE)
            doRemoveItem(getTileItemById(config.pos, config.bushId).uid
            else
            doPlayerSendCancel(cid, 'Sorry not possible')
            return false
            end
        end
    end
end

^updated script

thanks for reply!
alright so i fixed these errors but i do not understand this
4248dec71b5d86eaea17774ec00f814b.png

Another thing you should do is change this part:
LUA:
if getTileItemById(config.pos, config.bushId).uid < 0 then
    doCreatureSay(cid, 'You deleted the bush!', TALKTYPE_ORANGE)
    doRemoveItem(getTileItemById(config.pos, config.bushId).uid

To this:
LUA:
local bush = getTileItemById(config.pos, config.bushId).uid
if bush < 0 then
    doCreatureSay(cid, 'You deleted the bush!', TALKTYPE_ORANGE)
    doRemoveItem(bush)

Because there's no need to call getTileItemById twice on the same item.
 
Last edited by a moderator:
LUA:
local config = {
    bushId = 2768,
    pos = {x=2424, y=946, z=7}
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
 local bush = getTileItemById(config.pos, config.bushId).uid
    if bush < 0 then
    doCreatureSay(cid, 'You deleted the bush!', TALKTYPE_ORANGE)
    doRemoveItem(bush)
    else
        doRemoveItem(getTileItemById(config.pos, config.bushId).uid)
        doCreatureSay(cid, "You deleted the bush!", TALKTYPE_ORANGE_1)
    end

    doTransformItem(item.uid, item.itemid == 1945 and 1946 or 1945)
    return true
end

still the same error though, "item not found"
 
Last edited by a moderator:
That change was meant for your previous script.
Try this:
LUA:
local config = {
    bushId = 2768,
    pos = {x=2424, y=946, z=7}
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local bush = getTileItemById(config.pos, config.bushId).uid
    if bush <= 0 then
        doCreateItem(config.bushId, 1, config.pos)
        doCreatureSay(cid, "You summoned a bush!", TALKTYPE_ORANGE_1)
    else
        doRemoveItem(bush)
        doCreatureSay(cid, "You deleted the bush!", TALKTYPE_ORANGE_1)
    end

    doTransformItem(item.uid, item.itemid == 1945 and 1946 or 1945)
    return true
end
 
Last edited by a moderator:
thanks, i will use this script now; although i have some questions so i will understand better


local config = {
bushId = 2768,
pos = {x=2424, y=946, z=7}
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
if getTileItemById(config.pos, config.bushId).uid <= 0 then
doCreateItem(config.bushId, 1, config.pos)
doCreatureSay(cid, "You summoned a bush!", TALKTYPE_ORANGE_1)
else
doRemoveItem(getTileItemById(config.pos, config.bushId).uid)
doCreatureSay(cid, "You deleted the bush!", TALKTYPE_ORANGE_1)
end

doTransformItem(item.uid, item.itemid == 1945 and 1946 or 1945)
return true
end

1. what does .uid mean? isn't uid = unique id? and why does uid has to be there in order for script to work? what serves the uid's purpose in this script etc
 
uid is indeed uniqueid, it gets/removes the item based on the uniqueid of the item (all items have uniqueids btw, if you set an uniqueid in the map editor it will just have a specific one that doesn't change).
If the item it not found it will return 0 and if the item is there it will return the uniqueid of the item which is more than 0.
 
Back
Top