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

Lua what the problem of my "for"?

Lurk

Active Member
Joined
Dec 4, 2017
Messages
336
Reaction score
49
Hello, I'm trying to apply a for to my script but it aint working, The same script is working perfectly without the for and it does work for the first value of the toKnow/storage, but not for the storage 123463 for example. I'm using tfs 0.4
The script aims to kick a player if he has a certain storage IF the time of other storage is over
LUA:
local config = {
toKnow = 123456,
storage = 789456,
pos = {x = 160, y = 54, z = 7}, -- para onde o jogador será teleportado caso o tempo tenha acabado.
}

local count = 21

function onKill(cid, target, lastHit)
for i= 0, count-1 do
    if getPlayerStorageValue(cid, config.toKnow+i) == 1 then
        if getPlayerStorageValue(cid, config.storage+i) < os.time () then
            doSendMagicEffect(getThingPos(cid), CONST_ME_TELEPORT)
            doRemoveCreature(cid)
        end
    end
print("ToKnow OnKill: " ..config.toKnow+i.."")
print("Storage OnKill: "..config.storage+i.."")
end
return true
end


local count2 = 21

function onLogin(cid)
for i= 0, count2-1 do
    if getPlayerStorageValue(cid, config.toKnow+i) == 1 then
        if getPlayerStorageValue(cid, config.storage+i)  < os.time () then
            doTeleportThing(cid, config.pos)
            setPlayerStorageValue(cid, config.toKnow+i, 0)
        end
    end
print("ToKnow OnLogin: " ..config.toKnow+i.."")
print("Storage OnLogin: "..config.storage+i.."")
end
return true
end

the prints are showing me the currect values of the storages, the player should've been kiked

edit: when changing the prints to
LUA:
print("ToKnow OnKill: " ..getPlayerStorageValue(cid, config.toKnow+i).."")
print("Storage OnKill: " ..getPlayerStorageValue(cid, config.storage+i).."")
print("I: " ..i.."")

I get this in the console:
Code:
ToKnow OnKill: 0
Storage OnKill: 1566452503
I: 0
ToKnow OnKill: 0
Storage OnKill: 1566958361
I: 1
ToKnow OnKill: -1
Storage OnKill: 1566781969
I: 2
ToKnow OnKill: -1
Storage OnKill: -1
I: 3
ToKnow OnKill: -1
Storage OnKill: -1
I: 4
ToKnow OnKill: -1
Storage OnKill: -1
I: 5
ToKnow OnKill: -1
Storage OnKill: -1
I: 6
ToKnow OnKill: -1
Storage OnKill: 1
I: 7
ToKnow OnKill: -1
Storage OnKill: -1
I: 8
ToKnow OnKill: -1
Storage OnKill: -1
I: 9
ToKnow OnKill: -1
Storage OnKill: -1
I: 10
ToKnow OnKill: -1
Storage OnKill: -1
I: 11
ToKnow OnKill: -1
Storage OnKill: -1
I: 12
ToKnow OnKill: -1
Storage OnKill: -1
I: 13
ToKnow OnKill: 0
Storage OnKill: 1565435340
I: 14
ToKnow OnKill: -1
Storage OnKill: -1
I: 15
ToKnow OnKill: 0
Storage OnKill: -1
I: 16
ToKnow OnKill: 0
Storage OnKill: -1
I: 17
ToKnow OnKill: 0
Storage OnKill: -1
I: 18
ToKnow OnKill: 0
Storage OnKill: -1
I: 19
ToKnow OnKill: -1
Storage OnKill: -1
I: 20

I honestly have no idea what's happening here
edit: changed the prints to
LUA:
print("config.toKnow + i: " ..config.toKnow+i.."")
print("config.storage + i: " ..config.storage+i.."\n")

and got this in the console
Code:
config.toKnow + i: 123456
config.storage + i: 789456

config.toKnow + i: 123457
config.storage + i: 789457

config.toKnow + i: 123458
config.storage + i: 789458

config.toKnow + i: 123459
config.storage + i: 789459

config.toKnow + i: 123460
config.storage + i: 789460

config.toKnow + i: 123461
config.storage + i: 789461

config.toKnow + i: 123462
config.storage + i: 789462

config.toKnow + i: 123463
config.storage + i: 789463

config.toKnow + i: 123464
config.storage + i: 789464

config.toKnow + i: 123465
config.storage + i: 789465

config.toKnow + i: 123466
config.storage + i: 789466

config.toKnow + i: 123467
config.storage + i: 789467

config.toKnow + i: 123468
config.storage + i: 789468

config.toKnow + i: 123469
config.storage + i: 789469

config.toKnow + i: 123470
config.storage + i: 789470

config.toKnow + i: 123471
config.storage + i: 789471

config.toKnow + i: 123472
config.storage + i: 789472

config.toKnow + i: 123473
config.storage + i: 789473

config.toKnow + i: 123474
config.storage + i: 789474

config.toKnow + i: 123475
config.storage + i: 789475

config.toKnow + i: 123476
config.storage + i: 789476

which is the expected, still when trying to getPlayerStorageValue(cid, config.toKnow+i) == whatever I get those weird, giant numbers
 
Last edited:
Hello, I'm trying to apply a for to my script but it aint working, The same script is working perfectly without the for and it does work for the first value of the toKnow/storage, but not for the storage 123463 for example. I'm using tfs 0.4
LUA:
local config = {
toKnow = 123456,
storage = 789456,
pos = {x = 160, y = 54, z = 7}, -- para onde o jogador será teleportado caso o tempo tenha acabado.
}

local count = 21

function onKill(cid, target, lastHit)
for i= 0, count-1 do
    if getPlayerStorageValue(cid, config.toKnow+i) == 1 then
        if getPlayerStorageValue(cid, config.storage+i) < os.time () then
            doSendMagicEffect(getThingPos(cid), CONST_ME_TELEPORT)
            doRemoveCreature(cid)
        end
    end
print("ToKnow OnKill: " ..config.toKnow+i.."")
print("Storage OnKill: "..config.storage+i.."")
end
return true
end


local count2 = 21

function onLogin(cid)
for i= 0, count2-1 do
    if getPlayerStorageValue(cid, config.toKnow+i) == 1 then
        if getPlayerStorageValue(cid, config.storage+i)  < os.time () then
            doTeleportThing(cid, config.pos)
            setPlayerStorageValue(cid, config.toKnow+i, 0)
        end
    end
print("ToKnow OnLogin: " ..config.toKnow+i.."")
print("Storage OnLogin: "..config.storage+i.."")
end
return true
end

the prints are showing me the currect values of the storages, the player should've been kiked

edit: when changing the prints to
LUA:
print("ToKnow OnKill: " ..getPlayerStorageValue(cid, config.toKnow+i).."")
print("Storage OnKill: " ..getPlayerStorageValue(cid, config.storage+i).."")
print("I: " ..i.."")

I get this in the console:
Code:
ToKnow OnKill: 0
Storage OnKill: 1566452503
I: 0
ToKnow OnKill: 0
Storage OnKill: 1566958361
I: 1
ToKnow OnKill: -1
Storage OnKill: 1566781969
I: 2
ToKnow OnKill: -1
Storage OnKill: -1
I: 3
ToKnow OnKill: -1
Storage OnKill: -1
I: 4
ToKnow OnKill: -1
Storage OnKill: -1
I: 5
ToKnow OnKill: -1
Storage OnKill: -1
I: 6
ToKnow OnKill: -1
Storage OnKill: 1
I: 7
ToKnow OnKill: -1
Storage OnKill: -1
I: 8
ToKnow OnKill: -1
Storage OnKill: -1
I: 9
ToKnow OnKill: -1
Storage OnKill: -1
I: 10
ToKnow OnKill: -1
Storage OnKill: -1
I: 11
ToKnow OnKill: -1
Storage OnKill: -1
I: 12
ToKnow OnKill: -1
Storage OnKill: -1
I: 13
ToKnow OnKill: 0
Storage OnKill: 1565435340
I: 14
ToKnow OnKill: -1
Storage OnKill: -1
I: 15
ToKnow OnKill: 0
Storage OnKill: -1
I: 16
ToKnow OnKill: 0
Storage OnKill: -1
I: 17
ToKnow OnKill: 0
Storage OnKill: -1
I: 18
ToKnow OnKill: 0
Storage OnKill: -1
I: 19
ToKnow OnKill: -1
Storage OnKill: -1
I: 20

I honestly have no idea what's happening here
edit: I believe the script is executing too fast and the values area adding up? sometimes the storages appear as a really big number and other times as -1, how could I set up an exhaust of like 1 sec between each execution of this for?
What is your script supposed to be doing?
Atm it seems to be trying to log the player out after killing an enemy, then when the player logs back in, teleport them somewhere?
Just looks strange. Without knowing what your intention is, I can't really help solve your problem.

Your last inquiry though, I can help with that.
how could I set up an exhaust of like 1 sec between each execution of this for?
You can use an addEvent that loops to itself.
LUA:
local config = {
    toKnow = 123456,
    storage = 789456,
    pos = {x = 160, y = 54, z = 7}, -- para onde o jogador será teleportado caso o tempo tenha acabado.
}

local function delayed_loop(cid, iteration)
    if isPlayer(cid) then
        print("Loop: " .. iteration)
        print("config.toKnow+i: " .. config.toKnow + iteration .. " = " .. getPlayerStorageValue(cid, config.toKnow + iteration))
        print("config.storage+i: " .. config.storage + iteration .. " = " .. getPlayerStorageValue(cid, config.storage + iteration))
        if getPlayerStorageValue(cid, config.toKnow + iteration) == 1 then
            if getPlayerStorageValue(cid, config.storage + iteration) < os.time() then
                doSendMagicEffect(getThingPos(cid), CONST_ME_TELEPORT)
                doRemoveCreature(cid)
            end
        end
        if iteration ~= 0 then
            addEvent(delayed_loop, 1000, cid, iteration - 1)
        else
            print("------------------")
        end
    end
end

local count = 21

function onKill(cid, target, lastHit)
    print("------------------")
    addEvent(delayed_loop, 0, cid, count)
    return true
end
 
Yes that's the intent of the script, check if he has a storage and, if he does, check if other storage still has time left, if not then it will kick the player to the temple and set the first storage to 0. it is to remove him from a private cave that's supposed to last for 4 hours

edit: got the same problems as before
Code:
Loop: 21
config.toKnow+i: 123477 = -1
config.storage+i: 789477 = -1
Loop: 20
config.toKnow+i: 123476 = -1
config.storage+i: 789476 = -1
Loop: 19
config.toKnow+i: 123475 = 0
config.storage+i: 789475 = -1
Loop: 18
config.toKnow+i: 123474 = 0
config.storage+i: 789474 = -1
Loop: 17
config.toKnow+i: 123473 = 0
config.storage+i: 789473 = -1
Loop: 16
config.toKnow+i: 123472 = 0
config.storage+i: 789472 = -1
Loop: 15
config.toKnow+i: 123471 = -1
config.storage+i: 789471 = -1
Loop: 14
config.toKnow+i: 123470 = 0
config.storage+i: 789470 = 1565435340
Loop: 13
config.toKnow+i: 123469 = -1
config.storage+i: 789469 = -1
Loop: 12
config.toKnow+i: 123468 = -1
config.storage+i: 789468 = -1
Loop: 11
config.toKnow+i: 123467 = -1
config.storage+i: 789467 = -1
Loop: 10
config.toKnow+i: 123466 = -1
config.storage+i: 789466 = -1
Loop: 9
config.toKnow+i: 123465 = -1
config.storage+i: 789465 = -1
Loop: 8
config.toKnow+i: 123464 = -1
config.storage+i: 789464 = -1
Loop: 7
config.toKnow+i: 123463 = -1
config.storage+i: 789463 = 1
Loop: 6
config.toKnow+i: 123462 = -1
config.storage+i: 789462 = -1
Loop: 5
config.toKnow+i: 123461 = -1
config.storage+i: 789461 = -1
Loop: 4
config.toKnow+i: 123460 = -1
config.storage+i: 789460 = -1
Loop: 3
config.toKnow+i: 123459 = -1
config.storage+i: 789459 = -1
Loop: 2
config.toKnow+i: 123458 = -1
config.storage+i: 789458 = 1566781969
Loop: 1
config.toKnow+i: 123457 = 0
config.storage+i: 789457 = 1566958361
Loop: 0
config.toKnow+i: 123456 = 0
config.storage+i: 789456 = 1566452503
 
Last edited:
my bad, I was just checking it wrong.. the first script I commented was already working. sorry for this, been working at this script for a few hours already
 
Back
Top