• 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 Stop function when player log out

marek12

Available for sprite works
Joined
Apr 8, 2020
Messages
398
Solutions
4
Reaction score
394
Hello guys, I am using talkaction script with function (on/off). The thing is when player turn it on and log out it is not turning off the script and I am getting error in console "Creature not found" and in order to turn it on again he needs to turn it off when he log back in. Any idea how to make a function stop on logout? Thanks!
 
Solution
Try this one
Lua:
local colors = {42, 50, 64, 65, 79, 81, 91, 94, 95, 114, 128}
local storage = 65535
local time = 100 --in miliseconds

function onSay(cid, words, param, channel)
        if(param == "on") then
                if getPlayerStorageValue(cid, storage) < 1 then
                        if doPlayerRemoveMoney(cid, 5000) == TRUE then
                                local event = addEvent(changeOutfit, time, cid)
                                setPlayerStorageValue(cid, storage, 1)
                                return TRUE
                        else
                                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You do not have enough money.")
                                return TRUE...
TFS version? Post the script which you need help with and as much details as possible so we can help you.
I will just assume you are using TFS 1.3 so you can add creaturescript onlogout
Lua:
function onLogout(player)
 
I am using TFS 0.4.
I am asking more to learn and not just to copy-paste and forget about it. So would be nice if you can explain how can I use such a thing.
Basically the script is adding a storage to player, in this case local storage = 65535 :

if(param == "on") then
if getPlayerStorageValue(cid, storage) < 1 then

so from what I understand, is when player use "!<command> on" it will add him a storage 65535,1 and continue to use script, when he says "!<command> off" it removes the storage value and the script stops.

But when player turn the script on and log out, the storage does not change, so TFS can't see the who to assign storage 65535,1 and I am getting error in console.
Tell me if I am right and possibly explain how to fix that.
If I didn't explain something right just correct me please.
Post automatically merged:

Now I think I got 2 options to fix that.
1. make a creaturescript onLogout and change storage value 65535 to 0.
2. make it inside talkaction (but I have no idea how or if it's even possible)
Post automatically merged:

lets use this as an example. can you explain how to do it on this script?
Lua:
local colors = {42, 50, 64, 65, 79, 81, 91, 94, 95, 114, 128}
local storage = 65535
local time = 100 --in miliseconds

function onSay(cid, words, param, channel)

        if(param == "on") then
                if getPlayerStorageValue(cid, storage) < 1 then
                        if doPlayerRemoveMoney(cid, 5000) == TRUE then
                                local event = addEvent(changeOutfit, time, cid)
                                setPlayerStorageValue(cid, storage, 1)
                                return TRUE
                        else
                                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You do not have enough money.")
                                return TRUE
                        end
                else
                        return TRUE
                end

        elseif(param == "off") then
                if getPlayerStorageValue(cid, storage) > 0 then
                        setPlayerStorageValue(cid, storage, 0)
                        return TRUE
                else
                        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You do not have rainbow outfit on.")
                        return TRUE
                end
        else
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong command param! Use !rainbow on/off or !r on/off")
                return TRUE
        end
        return TRUE
end

function changeOutfit(cid)

local randomHead = colors[math.random(1,#colors)]
local randomLegs = colors[math.random(1,#colors)]
local randomBody = colors[math.random(1,#colors)]
local randomFeet = colors[math.random(1,#colors)]
local tmp = {}

        if getPlayerStorageValue(cid, storage) > 0 then
        local outfit = getCreatureOutfit(cid)
                tmp = outfit
                tmp.lookType = outfit.lookType
                tmp.lookHead = randomHead
                tmp.lookLegs = randomLegs
                tmp.lookBody = randomBody
                tmp.lookFeet = randomFeet
                tmp.lookAddons = outfit.lookAddons

                doCreatureChangeOutfit(cid, tmp)
                local event = addEvent(repeatChangeOutfit, time, cid)
                return TRUE
        else
                stopEvent(event)
                return TRUE
        end
end

function repeatChangeOutfit(cid)

local randomHead = colors[math.random(#colors)]
local randomLegs = colors[math.random(#colors)]
local randomBody = colors[math.random(#colors)]
local randomFeet = colors[math.random(#colors)]
local tmp = {}

        if getPlayerStorageValue(cid, storage) > 0 then
        local outfit = getCreatureOutfit(cid)
                tmp = outfit
                tmp.lookType = outfit.lookType
                tmp.lookHead = randomHead
                tmp.lookLegs = randomLegs
                tmp.lookBody = randomBody
                tmp.lookFeet = randomFeet
                tmp.lookAddons = outfit.lookAddons

                doCreatureChangeOutfit(cid, tmp)
                local event = addEvent(changeOutfit, time, cid)
                return TRUE
        else
                stopEvent(event)
                return TRUE
        end
end
 
Last edited:
in your \data\creaturescripts\scripts does it exit logout.lua? If not then just create a script and name it whatever you want then add this inside it.
Lua:
function onLogout(cid)
                 if getPlayerStorageValue(cid, 65535) > 0 then
                        setPlayerStorageValue(cid, 65535, 0)
end
    return true
end
and add this to your creaturescripts.xml
XML:
<event type="logout" name="Yourscriptname" event="script" value="Yourscriptname.lua"/>
Also register it to your login.lua
Lua:
registerCreatureEvent(cid, "yourscriptname")
 
added everything as u said above and no errors about it, but I am still having error in console when I log out.

Code:
[9:11:06.368] Emporio Here has logged out.

[9:11:06.502] [Error - TalkAction Interface]
[9:11:06.510] In a timer event called from:
[9:11:06.516] data/talkactions/scripts/dancing.lua:onSay
[9:11:06.523] Description:
[9:11:06.525] (LuaInterface::luaGetCreatureStorage) Creature not found

[9:11:06.534] [Error - TalkAction Interface]
[9:11:06.540] In a timer event called from:
[9:11:06.543] data/talkactions/scripts/dancing.lua:onSay
[9:11:06.548] Description:
[9:11:06.551] data/talkactions/scripts/dancing.lua:44: attempt to compare number with boolean
[9:11:06.561] stack traceback:
[9:11:06.565]   data/talkactions/scripts/dancing.lua:44: in function <data/talkactions/scripts/dancing.lua:36>
 
Try this one
Lua:
local colors = {42, 50, 64, 65, 79, 81, 91, 94, 95, 114, 128}
local storage = 65535
local time = 100 --in miliseconds

function onSay(cid, words, param, channel)
        if(param == "on") then
                if getPlayerStorageValue(cid, storage) < 1 then
                        if doPlayerRemoveMoney(cid, 5000) == TRUE then
                                local event = addEvent(changeOutfit, time, cid)
                                setPlayerStorageValue(cid, storage, 1)
                                return TRUE
                        else
                                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You do not have enough money.")
                                return TRUE
                        end
                else
                        return TRUE
                end

        elseif(param == "off") then
                if getPlayerStorageValue(cid, storage) > 0 then
                        setPlayerStorageValue(cid, storage, 0)
                        return TRUE
                else
                        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You do not have rainbow outfit on.")
                        return TRUE
                end
        else
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong command param! Use !rainbow on/off or !r on/off")
                return TRUE
        end
        return TRUE
end

function changeOutfit(cid)
if not isPlayer(cid) then
     return true
end
local randomHead = colors[math.random(1,#colors)]
local randomLegs = colors[math.random(1,#colors)]
local randomBody = colors[math.random(1,#colors)]
local randomFeet = colors[math.random(1,#colors)]
local tmp = {}

        if getPlayerStorageValue(cid, storage) > 0 then
        local outfit = getCreatureOutfit(cid)
                tmp = outfit
                tmp.lookType = outfit.lookType
                tmp.lookHead = randomHead
                tmp.lookLegs = randomLegs
                tmp.lookBody = randomBody
                tmp.lookFeet = randomFeet
                tmp.lookAddons = outfit.lookAddons

                doCreatureChangeOutfit(cid, tmp)
                local event = addEvent(repeatChangeOutfit, time, cid)
                return TRUE
        else
                stopEvent(event)
                return TRUE
        end
end

function repeatChangeOutfit(cid)
if not isPlayer(cid) then
     return true
end
local randomHead = colors[math.random(#colors)]
local randomLegs = colors[math.random(#colors)]
local randomBody = colors[math.random(#colors)]
local randomFeet = colors[math.random(#colors)]
local tmp = {}

        if getPlayerStorageValue(cid, storage) > 0 then
        local outfit = getCreatureOutfit(cid)
                tmp = outfit
                tmp.lookType = outfit.lookType
                tmp.lookHead = randomHead
                tmp.lookLegs = randomLegs
                tmp.lookBody = randomBody
                tmp.lookFeet = randomFeet
                tmp.lookAddons = outfit.lookAddons

                doCreatureChangeOutfit(cid, tmp)
                local event = addEvent(changeOutfit, time, cid)
                return TRUE
        else
                stopEvent(event)
                return TRUE
        end
end
 
Solution
I added player check below your functions, so it stop spams if no player or if player is offline.
Lua:
if not isPlayer(cid) then
     return true
end
 
this is exactly what I was trying to do. I was just placing it in wrong place I guess.
Thanks anyways. How do rep you? :D
 
No there's no rep here on the forum, the only way to thank someone who helped is by marking his reply as Best Answer or by pressing like to his reply which you have already done, so its enough.
 
Back
Top