• 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 Check values from/to?

sowber

New Member
Joined
Sep 30, 2016
Messages
35
Reaction score
0
Hello, i'm trying to create a script wich checks PlayerStorageValue from number to number. How to it properly with just 1 line? Here comes an example:

Lua:
if getPlayerStorageValue(cid,44601-44700) < 0 then
    doPlayerSetStorageValue(cid,44601,1-44609,1) -- Storages
    doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Your questlog has been updated.")
    end
 
Solution
This will probably lag your server for a second though.
Why do you need to update that many storages?
Lua:
for i = 1, 100 do
   if getPlayerStorageValue(cid, 44600 + i) < 0 then
       doPlayerSetStorageValue(cid, 44600 + i, 1) -- Storages
       doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Your questlog has been updated.")
   end
end
This will probably lag your server for a second though.
Why do you need to update that many storages?
Lua:
for i = 1, 100 do
   if getPlayerStorageValue(cid, 44600 + i) < 0 then
       doPlayerSetStorageValue(cid, 44600 + i, 1) -- Storages
       doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Your questlog has been updated.")
   end
end
 
Solution
Lua:
for i = from, to do
   if getPlayerStorageValue(cid, i) < 0 then
      doPlayerSetStorageValue(cid, i, 1) -- Storages
       doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Your questlog has been updated.")
   end
end
It Mostly would depend on your Range & the constants, mostly around 0.1 or 0.2 of a second for 100k storages
 
Got it, and what about my npcs? all of them uses this random text script:

Lua:
local random_texts = {'Message1', 'Message2', 'Message3'}
local random_texts_chance = 50 -- percent
local random_texts_interval = 14 -- seconds
------------------------------
function onThink()
if(getCreatureStorage(getNpcId(), 1) < os.time()) then
doCreatureSetStorage(getNpcId(), 1, os.time() + random_texts_interval)
if(math.random(1, 100) < random_texts_chance) then
selfSay(random_texts[math.random(1, #random_texts)])
end
end
npcHandler:onThink()
end
 
Got it, and what about my npcs? all of them uses this random text script:

Lua:
local random_texts = {'Message1', 'Message2', 'Message3'}
local random_texts_chance = 50 -- percent
local random_texts_interval = 14 -- seconds
------------------------------
function onThink()
if(getCreatureStorage(getNpcId(), 1) < os.time()) then
doCreatureSetStorage(getNpcId(), 1, os.time() + random_texts_interval)
if(math.random(1, 100) < random_texts_chance) then
selfSay(random_texts[math.random(1, #random_texts)])
end
end
npcHandler:onThink()
end
Can Npc's even use storage values?
I'll be honest, I've never tried.

Can you be more specific with your question though?
I'm not sure what I'm looking for here.
 
not about storages at all, (sorry about that), just curious if i'll get lag using this script for all npcs. It's a script wich send random default messages like tibiarl
 
No, you won't lag.
I'm just amazed that you can give/update npc storages at all.
 
in older versions you can give any creature a storage value ;0
Poor TFS 1.0+ users. xD

But to be honest, I can't think of any reason to give npc/monsters a storage value. :/
There is so many more ways to get the same result without using a storage value.

Npc's think every 0.5 seconds.. I think.
You may want to adjust the interval to 14 * 2, to compensate if that's the case.
Lua:
local random_texts = {'Message1', 'Message2', 'Message3'}
local random_texts_interval = 14 -- seconds
local counter = 0
------------------------------
function onThink()
   if counter < random_texts_interval then
       counter = counter + 1
   else
       if math.random(1, 2) == 1 then
           selfSay(random_texts[math.random(#random_texts)])
       end
       counter = 0
   end
   npcHandler:onThink()
end
 
Last edited:
Poor TFS 1.0+ users. xD

But to be honest, I can't think of any reason to give npc/monsters a storage value. :/
There is so many more ways to get the same result without using a storage value.

Lua:
local random_texts = {'Message1', 'Message2', 'Message3'}
local random_texts_interval = 14 -- seconds
local counter = 0
------------------------------
function onThink()
   if counter < random_texts_interval then
       counter = counter + 1
   else
       if math.random(1, 2) == 1 then
           selfSay(random_texts[math.random(#random_texts)])
       end
       counter = 0
   end
   npcHandler:onThink()
end
This is a much better way as it wont go through the storage shit, and faster i suppose

Could Go For This(Just Shorter)
Lua:
local random_texts = {'Message1', 'Message2', 'Message3'}
local random_texts_interval = 14 -- seconds
local counter = 0
------------------------------
function onThink()
    if counter < os.time() then
        counter = os.time() + random_texts_interval
        if math.random(1, 2) == 1 then
            selfSay(random_texts[math.random(#random_texts)])
        end
    end
   npcHandler:onThink()
end
 
Last edited:
Poor TFS 1.0+ users. xD

But to be honest, I can't think of any reason to give npc/monsters a storage value. :/
There is so many more ways to get the same result without using a storage value.

Npc's think every 0.5 seconds.. I think.
You may want to adjust the interval to 14 * 2, to compensate if that's the case.
Lua:
local random_texts = {'Message1', 'Message2', 'Message3'}
local random_texts_interval = 14 -- seconds
local counter = 0
------------------------------
function onThink()
   if counter < random_texts_interval then
       counter = counter + 1
   else
       if math.random(1, 2) == 1 then
           selfSay(random_texts[math.random(#random_texts)])
       end
       counter = 0
   end
   npcHandler:onThink()
end
id say its a disadvantage of 1.x but you can do the same shit with a table and save keys/values the same way you could with storages
 
This is a much better way as it wont go through the storage shit, and faster i suppose

Could Go For This(Just Shorter)
Lua:
local random_texts = {'Message1', 'Message2', 'Message3'}
local random_texts_interval = 14 -- seconds
local counter = 0
------------------------------
function onThink()
    if counter < os.time then
        counter = os.time + random_texts_interval
        if math.random(1, 2) == 1 then
            selfSay(random_texts[math.random(#random_texts)])
        end
    end
   npcHandler:onThink()
end
haha, almost the same as mine. ^^
We had the same idea.
 
haha, almost the same as mine. ^^
We had the same idea.

Code:
[17:36:27.618] [Error - NpcScript Interface]
[17:36:27.622] data/npc/scripts/rook/Cipfried.lua:onThink
[17:36:27.625] Description:
[17:36:27.627] data/npc/scripts/rook/Cipfried.lua:13: attempt to compare number
with function
 
Code:
[17:36:27.618] [Error - NpcScript Interface]
[17:36:27.622] data/npc/scripts/rook/Cipfried.lua:onThink
[17:36:27.625] Description:
[17:36:27.627] data/npc/scripts/rook/Cipfried.lua:13: attempt to compare number
with function
script?
 
Srry, I misquoted, it's the one from @Xeraphus

Lua:
local random_texts = {'Message1', 'Message2', 'Message3'}
local random_texts_interval = 14 -- seconds
local counter = 0
------------------------------
function onThink()
    if counter < os.time() then
        counter = os.time() + random_texts_interval
        if math.random(1, 2) == 1 then
            selfSay(random_texts[math.random(#random_texts)])
        end
    end
   npcHandler:onThink()
end
 
This will probably lag your server for a second though.
Why do you need to update that many storages?
Lua:
for i = 1, 100 do
   if getPlayerStorageValue(cid, 44600 + i) < 0 then
       doPlayerSetStorageValue(cid, 44600 + i, 1) -- Storages
       doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Your questlog has been updated.")
   end
end

Won't that spam the player with up to 100: "Your quest log has been updated." messages?
Another question, doesn't TFS already send you that message when the quest is registered in data/xml/quests.xml? Or is that not the case for older revs?

-- updateQuestStorages(cid, startStorageKey, endStorageKey, storageValue)
updateQuestStorages(cid, 44601, 44700, 1)
Lua:
function updateQuestStorages(cid, startStorageKey, endStorageKey, storageValue)
    local questLogUpdated = false
    for i = startStorageKey, endStorageKey do
        if getPlayerStorageValue(cid, i) ~= storageValue then
            doPlayerSetStorageValue(cid, i, storageValue)
            questLogUpdated = true
        end
    end
  
    if questLogUpdated then
        doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Your questlog has been updated.")
    end
end
 
Last edited:
Hmm i don't think so, i tried with quests.xml and got no questlog message. (TFS 0.4 3777)

And yes, it will spam 100+ messages, and i don't want this. I want to register quests in questlog, so player will know wich accesses he has and stuff.
 
Back
Top