• 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 setStorageValue after 10 seconds

namco

Alienbutserious
Joined
Sep 5, 2010
Messages
148
Solutions
2
Reaction score
38
TFS 1.3

I've tried this (and EVERY LITTLE VARIATION):
Lua:
local function decara(player)
    local player = Player(playerId)
    player:setStorageValue(420, 0) -- line 43 which gets the error
end

With:
Lua:
addEvent(decara, 10000, playerId)

But I've got this error:
Code:
Lua Script Error: [Main Interface]
in a timer event called from:
(Unknown scriptfile)
D:\forgottenserver\data\scripts\actions\marijuanna.lua:43: attempt to index local 'player' (a nil value)
stack traceback:
        [C]: in function '__index'
        D:\forgottenserver\data\scripts\actions\marijuanna.lua:43: in function <D:\forgottenserver\data\scripts\actions\marijuanna.lua:41>

Here's the entire script:
Lua:
local fumar = Action() -- usar a tocha no baseado fuma ele

local function decara(player)
    local player = Player(playerId)
    player:setStorageValue(420, 0)
end

function fumar.onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if player:getStorageValue(420) ~= 0    then -- checa se ta chapado
        player:sendTextMessage(MESSAGE_STATUS_SMALL, "You must wait 10 seconds between smokes.")
        return true
    end
   
    if target.itemid == 7499 then -- joint
   
        target:remove(1)
       
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        player:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
       
        local outfit = player:getOutfit()
        outfit.lookHead = math.random(0, 132)
        outfit.lookBody = math.random(0, 132)
        outfit.lookLegs = math.random(0, 132)
        outfit.lookFeet = math.random(0, 132)
        player:setOutfit(outfit)
       
        player:say("Uuhf.", TALKTYPE_MONSTER_SAY)
       
        player:setStorageValue(420, 1) -- fica chapado
        addEvent(decara, 10000, playerId) -- fica de cara depois de 10 segundos
       
        return true
    end

end

fumar:id(8304, 9956, 2055)
fumar:register()
 
Last edited:
Solution
TFS 1.3

I've tried this (and EVERY LITTLE VARIATION):
Lua:
local function decara(player)
    local player = Player(playerId)
    player:setStorageValue(420, 0) -- line 43 which gets the error
end

With:
Lua:
addEvent(decara, 10000, playerId)

But I've got this error:
Code:
Lua Script Error: [Main Interface]
in a timer event called from:
(Unknown scriptfile)
D:\forgottenserver\data\scripts\actions\marijuanna.lua:43: attempt to index local 'player' (a nil value)
stack traceback:
        [C]: in function '__index'
        D:\forgottenserver\data\scripts\actions\marijuanna.lua:43: in function <D:\forgottenserver\data\scripts\actions\marijuanna.lua:41>

Here's the entire script:
Lua:
local fumar = Action() -- usar a tocha no baseado...
TFS 1.3

I've tried this (and EVERY LITTLE VARIATION):
Lua:
local function decara(player)
    local player = Player(playerId)
    player:setStorageValue(420, 0) -- line 43 which gets the error
end

With:
Lua:
addEvent(decara, 10000, playerId)

But I've got this error:
Code:
Lua Script Error: [Main Interface]
in a timer event called from:
(Unknown scriptfile)
D:\forgottenserver\data\scripts\actions\marijuanna.lua:43: attempt to index local 'player' (a nil value)
stack traceback:
        [C]: in function '__index'
        D:\forgottenserver\data\scripts\actions\marijuanna.lua:43: in function <D:\forgottenserver\data\scripts\actions\marijuanna.lua:41>

Here's the entire script:
Lua:
local fumar = Action() -- usar a tocha no baseado fuma ele

local function decara(player)
    local player = Player(playerId)
    player:setStorageValue(420, 0)
end

function fumar.onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if player:getStorageValue(420) ~= 0    then -- checa se ta chapado
        player:sendTextMessage(MESSAGE_STATUS_SMALL, "You must wait 10 seconds between smokes.")
        return true
    end
 
    if target.itemid == 7499 then -- joint
 
        target:remove(1)
     
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        player:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
     
        local outfit = player:getOutfit()
        outfit.lookHead = math.random(0, 132)
        outfit.lookBody = math.random(0, 132)
        outfit.lookLegs = math.random(0, 132)
        outfit.lookFeet = math.random(0, 132)
        player:setOutfit(outfit)
     
        player:say("Uuhf.", TALKTYPE_MONSTER_SAY)
     
        player:setStorageValue(420, 1) -- fica chapado
        addEvent(decara, 10000, playerId) -- fica de cara depois de 10 segundos
     
        return true
    end

end

fumar:id(8304, 9956, 2055)
fumar:register()
Try this:

Lua:
local fumar = Action() -- usar a tocha no baseado fuma ele

local function decara(playerId)
    local player = Player(playerId)
    if not player then
        return false
    end
    player:setStorageValue(420, 0)
    return true
end

function fumar.onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if player:getStorageValue(420) ~= 0    then -- checa se ta chapado
        player:sendTextMessage(MESSAGE_STATUS_SMALL, "You must wait 10 seconds between smokes.")
        return true
    end
 
    if target.itemid == 7499 then -- joint
 
        target:remove(1)
     
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        player:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
     
        local outfit = player:getOutfit()
        outfit.lookHead = math.random(0, 132)
        outfit.lookBody = math.random(0, 132)
        outfit.lookLegs = math.random(0, 132)
        outfit.lookFeet = math.random(0, 132)
        player:setOutfit(outfit)
     
        player:say("Uuhf.", TALKTYPE_MONSTER_SAY)
     
        player:setStorageValue(420, 1) -- fica chapado
        addEvent(decara, 10000, player:getId()) -- fica de cara depois de 10 segundos
     
        return true
    end

end

fumar:id(8304, 9956, 2055)
fumar:register()
In your line 32, you pass the variable playerId that don't exist in the scope. Then it is nil. When addEvent call the function decara the code have one declared variable player but you try to find Player(playerId) the variable playerId don't exist in this scope too. Even if its was right, you need to check if the player exist, to avoid the error that showed in the console, where the code try to set a storage value to a player that don't exist.

I suggest you use better names to your functions, it's a good practice.
 
Solution
Back
Top