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

else option dont work in my script TFS 1.X

gritness

New Member
Joined
May 26, 2021
Messages
24
Reaction score
3
Hey Guys,
I have some problem with my script, else option does not work. "If condition" work well, but when player have not specific item in arrows slot (9742) its dont work
Bellow is my script :

local storage_id = 10006
local unique_id = 35056

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
if player:getStorageValue(storage_id) == 2 and item:getUniqueId() == unique_id and player:getSlotItem(CONST_SLOT_AMMO):getId() == 9742 then

player:teleportTo(fromPosition,true)
item:transform(1259 + 1)

else

player:sendTextMessage(MESSAGE_INFO_DESCR,"You must have a specific item in your arrows slot.")

end
end


AND HERE IS ERROR IN TFS CONSOLE :

Lua Script Error: [Action Interface]
data/actions/scripts/quests/nowe/drzwidoyala.lua:eek:nUse
data/actions/scripts/quests/nowe/drzwidoyala.lua:5: attempt to index a nil value
stack traceback:
[C]: in metamethod '__index'
data/actions/scripts/quests/nowe/drzwidoyala.lua:5: in function <data/actions/scripts/quests/nowe/drzwidoyala.lua:4>
Post automatically merged:

Problem occurs when i have not item in arrow slot, when i take another item than 9742 message work.

So, need any validate of nil value, someone can help ?
 
Last edited:

Silba

is stephany, the josh wife
Premium User
Joined
Aug 22, 2013
Messages
357
Solutions
4
Reaction score
266
Format your post properly man :(

The problem is that you are using player:getSlotItem(CONST_SLOT_AMMO):getId() == 9742 which assumes there is always an item to compare.

So you could grab the slot earlier with this so it's easier to look at
local ammoItem = player:getSlotItem(CONST_SLOT_AMMO)
then you can check if there's an item there with
if ammoItem then

I believe this code should work

Lua:
local storage_id = 10006
local unique_id = 35056
local ammoItem = player:getSlotItem(CONST_SLOT_AMMO)

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if ammoItem and ammoItem.itemid == 9742 and player:getStorageValue(storage_id) == 2 then
        player:teleportTo(fromPosition, true)
        item:transform(1259 + 1)
    else
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You must have a specific item in your arrows slot.")
    end
end
 
Last edited:
Top