• 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 Trying to use getWorldTime tfs 1.1

Jfrye

Mapper, trying to learn scripting
Joined
Jan 8, 2009
Messages
366
Solutions
5
Reaction score
86
Location
Mexico Missouri
Code:
local light = 1479
local spawnpos = {x=477, y=557, z=7}


function onTime()
for i = 1, #t do
    if getWorldTime() < 1435 > 0600 then
    doTransformItem(light, 1480)
    end
return true
end  
end

Using this code, I am trying to transform a light from id 1479, to id 1480 at a certain tibia time. I tried using this https://otland.net/threads/light-up-the-streets-at-night.197060/ but that script isnt working on my server. So I thought I would try my own, and it seems like I have no idea what I am doing. Ive tried searching the right way to use getWorldTime(), and havent had much luck. Any help is appreciated, but it would also be nice if an explanation could be included.
 
What is this?
Code:
if getWorldTime() < 1435 > 0600 then
A for effort but the entire script is written incorrectly
 
Last edited:
Code:
local light = 1479
local spawnpos = {x=477, y=557, z=7}


function onTime()
for i = 1, #t do
    if getWorldTime() < 1435 > 0600 then
    doTransformItem(light, 1480)
    end
return true
end
end

Using this code, I am trying to transform a light from id 1479, to id 1480 at a certain tibia time. I tried using this https://otland.net/threads/light-up-the-streets-at-night.197060/ but that script isnt working on my server. So I thought I would try my own, and it seems like I have no idea what I am doing. Ive tried searching the right way to use getWorldTime(), and havent had much luck. Any help is appreciated, but it would also be nice if an explanation could be included.

As @Codex NG has mentioned, the entire syntax is wrong.

if getWorldTime() < 1435 > 0600 then
doTransformItem(light, 1480)
end

if getWorldTime() < 1435 then
if getWorldTime() > 0600 then
doTransformItem(light, 1480)
end
end

But even so, the entire script will not work as nothing will trigger it.
This script worked because it was a GLOBAL EVENT triggered to execute at a certain time:

globalevents.xml:

<globalevent name="script" time="06:00:00" script="script.lua"/>

When you tried using this script did you include the change to globalevents.xml?
 
@Leo32 I do have that line in globalevents.xml, but isn't that scripted to run at 6 am based off my system time? Also thank you for showing the right way to check time. I was pretty sure my way was completely wrong, but I'm trying to understand the way these things work.

Also shouldn't it be like this, doTransformItem (light, spawnpos, 1480)?
Or can I just transform 1479 based off an action id, then transform the new 1480 with another aid? So light 1479, aid 2500, transforms into light 1480, aid 2501? Then I wouldn't need the spawnpos?
 
Items are not transformed using doTransformItem from an action id, they are transformed from its uid using an item's item id to manipulate the item, this is why you need to get the actual item from the location rather than just passing it a value in the 1st parameter.

A simple way to think of this is.
1st parameter is the item to manipulate (item.uid)
2nd parameter is the modifier (item.itemid), it manipulates the 1st parameter
Code:
doTransformItem(item.uid, item.itemid)
 
@Codex NG thank you for explaining that. Explanations help alot. I will work on this script some more over the next few days and see what I can come up with. Thanks for the help so far guys. I appreciate it.
 
Something like this could work for you....

Code:
local positions = { --Each light that will be changed
[1] = {x = 1000, y = 1000, z = 7},
[2] = {x = 1000, y = 1000, z = 7},
[3] = {x = 1000, y = 1000, z = 7},
[4] = {x = 1000, y = 1000, z = 7},
[5] = {x = 1000, y = 1000, z = 7},
[6] = {x = 1000, y = 1000, z = 7}
}

local no_light_itemid = 1111
local light_itemid = 1111

function onThink(interval)
if getWorldTime() < 1435 then
    if getWorldTime() > 0600 then
    ------Turn the lights on--------
        for i = 1, #positions do
            ITEM = item(no_light_itemid):getThingFromPos(positions[i])
            doRemoveItem(ITEM)
        end
      
        for i = 1, #positions do
            doCreateItem(light_itemid, positions[i])
        end
    else
    -------If the time is < 0600 turn the lights off
    for i = 1, #positions do
            ITEM = item(light_itemid):getThingFromPos(positions[i])
            doRemoveItem(ITEM)
        end
      
        for i = 1, #positions do
            doCreateItem(no_light_itemid, positions[i])
        end
    end
    -----if the time is > 1435 turn the lights off
for i = 1, #positions do
            ITEM = item(light_itemid):getThingFromPos(positions[i])
            doRemoveItem(ITEM)
        end
      
        for i = 1, #positions do
            doCreateItem(no_light_itemid, positions[i])
        end
end
return true
end
 
@Itutorial, looking at that script, it uses the onThink function. With that function, do I just change my globalevents.xml <globalevents name ="lights" interval ="xxxx" script ="lights.lua"> ?
 
Code:
local positions = { --Each light that will be changed
    [1] = {x = 1000, y = 1000, z = 7},
    [2] = {x = 1000, y = 1000, z = 7},
    [3] = {x = 1000, y = 1000, z = 7},
    [4] = {x = 1000, y = 1000, z = 7},
    [5] = {x = 1000, y = 1000, z = 7},
    [6] = {x = 1000, y = 1000, z = 7}
}

local no_light_itemid = 1111
local light_itemid = 1111

function isBetweenWorldTime(min, max)
    return getWorldTime() > min and getWorldTime() < max
end

function swapLights(pos, removeItem, makeItem)
    for i = 1, #pos do
        ITEM = item(removeItem):getThingFromPos(pos[i])
        if ITEM ~= nil then
            doRemoveItem(ITEM)
            doCreateItem(makeItem, pos[i])
        end
    end
end

function onThink(interval)
    if isBetweenWorldTime(0600, 1435) then
        swapLights(positions, no_light_itemid, light_itemid)
    else
        swapLights(positions, light_itemid, no_light_itemid)
    end 
    return true
end
 
Oh wow, I was a little off with my script. Looks like I still have a lot to learn with these. But, I'm still confused about globalevents.xml. How would the new line look? Will it still be based on time, or go to interval? If it goes to interval, what would the number value be? If it goes by time, what time should be put in there?

@Itutorial, the script you posted throws errors for this part
Code:
function onThink(interval)
if getWorldTime() < 1435 then
if getWorldTime() > 0600 then
------Turn the lights on--------
for i = 1, #positions do
ITEM = item(no_light_itemid):getThingFromPos(positions[i])
doRemoveItem(ITEM)
end

@Codex NG The script you posted throws errors for this part
Code:
function swapLights(pos, removeItem, makeItem)

So is there a function somewhere that I can add?

Edit: this is the line from globalevents.xml
Code:
<globalevent name="Night Lights" interval="7200" script="nightlights.lua"/>
I used this code as I am honestly not sure how it should be. So I used this for testing, and the console spams the errors I posted every few seconds.
 
Last edited by a moderator:
Code:
The Forgotten Server - Version 1.1
Compiled with Microsoft Visual C++ version 12.0
Compiled on May 14 2015 22:47:50 for platform x64

A server developed by Mark Samman
Visit our forum for updates, support, and resources: http://otland.net/.

>> Loading config
>> Establishing database connection... MySQL 6.1.6
>> Running database manager
>> Loading vocations
>> Loading items
>> Loading script systems
>> Loading monsters
>> Loading outfits
>> Checking world type... NO-PVP
>> Loading map
> Map size: 3072x3072.
> Map loading time: 1.574 seconds.
> Loaded house items in: 0 s
>> Initializing gamestate
>> Loaded all modules, server starting up...
>> Forgotten Server Online!


Lua Script Error: [GlobalEvent Interface]
data/globalevents/scripts/nightlights.lua:onThink
data/globalevents/scripts/nightlights.lua:82: attempt to call global 'item' (a n
il value)
stack traceback:
        [C]: in function 'item'
        data/globalevents/scripts/nightlights.lua:82: in function <data/globalev
ents/scripts/nightlights.lua:77>
[Error - GlobalEvents::think] Failed to execute event: Night Lights

Lua Script Error: [GlobalEvent Interface]
data/globalevents/scripts/nightlights.lua:onThink
data/globalevents/scripts/nightlights.lua:82: attempt to call global 'item' (a n
il value)
stack traceback:
        [C]: in function 'item'
        data/globalevents/scripts/nightlights.lua:82: in function <data/globalev
ents/scripts/nightlights.lua:77>
[Error - GlobalEvents::think] Failed to execute event: Night Lights

Lua Script Error: [GlobalEvent Interface]
data/globalevents/scripts/nightlights.lua:onThink
data/globalevents/scripts/nightlights.lua:82: attempt to call global 'item' (a n
il value)
stack traceback:
        [C]: in function 'item'
        data/globalevents/scripts/nightlights.lua:82: in function <data/globalev
ents/scripts/nightlights.lua:77>
[Error - GlobalEvents::think] Failed to execute event: Night Lights

Lua Script Error: [GlobalEvent Interface]
data/globalevents/scripts/nightlights.lua:onThink
data/globalevents/scripts/nightlights.lua:82: attempt to call global 'item' (a n
il value)
stack traceback:
        [C]: in function 'item'
        data/globalevents/scripts/nightlights.lua:82: in function <data/globalev
ents/scripts/nightlights.lua:77>
[Error - GlobalEvents::think] Failed to execute event: Night Lights

Lua Script Error: [GlobalEvent Interface]
data/globalevents/scripts/nightlights.lua:onThink
data/globalevents/scripts/nightlights.lua:82: attempt to call global 'item' (a n
il value)
stack traceback:
        [C]: in function 'item'
        data/globalevents/scripts/nightlights.lua:82: in function <data/globalev
ents/scripts/nightlights.lua:77>
[Error - GlobalEvents::think] Failed to execute event: Night Lights

Lua Script Error: [GlobalEvent Interface]
data/globalevents/scripts/nightlights.lua:onThink
data/globalevents/scripts/nightlights.lua:82: attempt to call global 'item' (a n
il value)
stack traceback:
        [C]: in function 'item'
        data/globalevents/scripts/nightlights.lua:82: in function <data/globalev
ents/scripts/nightlights.lua:77>
[Error - GlobalEvents::think] Failed to execute event: Night Lights

This is using @Itutorial script

Code:
The Forgotten Server - Version 1.1
Compiled with Microsoft Visual C++ version 12.0
Compiled on May 14 2015 22:47:50 for platform x64

A server developed by Mark Samman
Visit our forum for updates, support, and resources: http://otland.net/.

>> Loading config
>> Establishing database connection... MySQL 6.1.6
>> Running database manager
>> Loading vocations
>> Loading items
>> Loading script systems
>> Loading monsters
>> Loading outfits
>> Checking world type... NO-PVP
>> Loading map
> Map size: 3072x3072.
> Map loading time: 1.516 seconds.
> Loaded house items in: 0.001 s
>> Initializing gamestate
>> Loaded all modules, server starting up...
>> Forgotten Server Online!


Lua Script Error: [GlobalEvent Interface]
data/globalevents/scripts/nightlights.lua:onThink
data/globalevents/scripts/nightlights.lua:83: attempt to call global 'item' (a n
il value)
stack traceback:
        [C]: in function 'item'
        data/globalevents/scripts/nightlights.lua:83: in function 'swapLights'
        data/globalevents/scripts/nightlights.lua:93: in function <data/globalev
ents/scripts/nightlights.lua:91>
[Error - GlobalEvents::think] Failed to execute event: Night Lights

This is using @Codex NG script.

Again, both of these were attempted using
Code:
<globalevent name="Night Lights" interval="7200" script="nightlights.lua"/>
 
Last edited by a moderator:
Code:
local positions = { --Each light that will be changed
    [1] = {x = 1000, y = 1000, z = 7},
    [2] = {x = 1000, y = 1000, z = 7},
    [3] = {x = 1000, y = 1000, z = 7},
    [4] = {x = 1000, y = 1000, z = 7},
    [5] = {x = 1000, y = 1000, z = 7},
    [6] = {x = 1000, y = 1000, z = 7}
}

local no_light_itemid = 1111
local light_itemid = 1111

function minMaxWorldTime(min, max)
    return getWorldTime() > min and getWorldTime() < max
end

function swapLights(pos, removeItem, makeItem)
    for i = 1, #pos do
        local item = getThingFromPos(pos[i])
        if item ~= nil then
            if item.itemid == removeItem then
                doTransformItem(item.uid, makeItem)
            end
        end
    end
end

function onThink(interval)
    if isBetweenWorldTime(1435, 0600) then
        swapLights(positions, no_light_itemid, light_itemid)
    else
        swapLights(positions, light_itemid, no_light_itemid)
    end 
    return true
end
 
@Codex NG Now the error is this

Code:
The Forgotten Server - Version 1.1
Compiled with Microsoft Visual C++ version 12.0
Compiled on May 14 2015 22:47:50 for platform x64

A server developed by Mark Samman
Visit our forum for updates, support, and resources: http://otland.net/.

>> Loading config
>> Establishing database connection... MySQL 6.1.6
>> Running database manager
>> Loading vocations
>> Loading items
>> Loading script systems
>> Loading monsters
>> Loading outfits
>> Checking world type... NO-PVP
>> Loading map
> Map size: 3072x3072.
> Map loading time: 1.517 seconds.
> Loaded house items in: 0.001 s
>> Initializing gamestate
>> Loaded all modules, server starting up...
>> Forgotten Server Online!


Lua Script Error: [GlobalEvent Interface]
data/globalevents/scripts/nightlights.lua:onThink
data/globalevents/scripts/nightlights.lua:93: attempt to call global 'isBetweenW
orldTime' (a nil value)
stack traceback:
        [C]: in function 'isBetweenWorldTime'
        data/globalevents/scripts/nightlights.lua:93: in function <data/globalev
ents/scripts/nightlights.lua:92>
[Error - GlobalEvents::think] Failed to execute event: Night Lights


Do I need to add a function to global.lua or something?
 
Code:
local positions = { --Each light that will be changed
    [1] = {x = 1000, y = 1000, z = 7},
    [2] = {x = 1000, y = 1000, z = 7},
    [3] = {x = 1000, y = 1000, z = 7},
    [4] = {x = 1000, y = 1000, z = 7},
    [5] = {x = 1000, y = 1000, z = 7},
    [6] = {x = 1000, y = 1000, z = 7}
}

local no_light_itemid = 1111
local light_itemid = 1111

function swapLights(pos, removeItem, makeItem)
    for i = 1, #pos do
        local item = getThingFromPos(pos[i])
        if item ~= nil then
            if item.itemid == removeItem then
                doTransformItem(item.uid, makeItem)
            end
        end
    end
end

function onThink(interval)
    if getWorldTime() < 1435 and getWorldTime() > 0600 then
        swapLights(positions, no_light_itemid, light_itemid)
    else
        swapLights(positions, light_itemid, no_light_itemid)
    end 
    return true
end

Are you using TFS 1.0+ because I dont think getThingFromPos(pos) is supported in 1.0+

nvm i seen the error in his code give me a sec.

Code:
local positions = { --Each light that will be changed
    [1] = {x = 1000, y = 1000, z = 7},
    [2] = {x = 1000, y = 1000, z = 7},
    [3] = {x = 1000, y = 1000, z = 7},
    [4] = {x = 1000, y = 1000, z = 7},
    [5] = {x = 1000, y = 1000, z = 7},
    [6] = {x = 1000, y = 1000, z = 7}
}

local no_light_itemid = 1111
local light_itemid = 1111

function swapLights(pos, removeItem, makeItem)
        local item = getThingFromPos(pos)
        if item ~= nil then
            if item.itemid == removeItem then
                doTransformItem(item.uid, makeItem)
            end
        end
end

function onThink(interval)
    if getWorldTime() < 1435 and getWorldTime() > 0600 then
        for i = 1, #positions do
        swapLights(positions[i], no_light_itemid, light_itemid)
        end
    else
        for i = 1, #positions do
        swapLights(positions[i], light_itemid, no_light_itemid)
        end
    end 
    return true
end
 
Last edited by a moderator:
@Itutorial This is the error I get with that script.

Code:
The Forgotten Server - Version 1.1


Lua Script Error: [GlobalEvent Interface]
data/globalevents/scripts/nightlights.lua:onThink
data/globalevents/scripts/nightlights.lua:78: attempt to call global 'getThingFr
omPos' (a nil value)
stack traceback:
        [C]: in function 'getThingFromPos'
        data/globalevents/scripts/nightlights.lua:78: in function 'swapLights'
        data/globalevents/scripts/nightlights.lua:89: in function <data/globalev
ents/scripts/nightlights.lua:86>
[Error - GlobalEvents::think] Failed to execute event: Night Lights
 
Go ahead and try this

Code:
local positions = { --Each light that will be changed
    [1] = {x = 1000, y = 1000, z = 7},
    [2] = {x = 1000, y = 1000, z = 7},
    [3] = {x = 1000, y = 1000, z = 7},
    [4] = {x = 1000, y = 1000, z = 7},
    [5] = {x = 1000, y = 1000, z = 7},
    [6] = {x = 1000, y = 1000, z = 7}
}

local no_light_itemid = 1111
local light_itemid = 1111

function swapLights(pos, removeItem, makeItem)
        local item = Item(pos):getItemById(removeItem)
        if item ~= nil then
            if item.itemid == removeItem then
                doTransformItem(item.uid, makeItem)
            end
        end
end

function onThink(interval)
    if getWorldTime() < 1435 and getWorldTime() > 0600 then
        for i = 1, #positions do
        swapLights(positions[i], no_light_itemid, light_itemid)
        end
    else
        for i = 1, #positions do
        swapLights(positions[i], light_itemid, no_light_itemid)
        end
    end 
    return true
end
 
@Itutorial I keep getting this code

Code:
The Forgotten Server - Version 1.1
Compiled with Microsoft Visual C++ version 12.0
Compiled on May 14 2015 22:47:50 for platform x64


Lua Script Error: [GlobalEvent Interface]
data/globalevents/scripts/nightlights.lua:onThink
data/globalevents/scripts/nightlights.lua:14: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/globalevents/scripts/nightlights.lua:14: in function 'swapLights'
        data/globalevents/scripts/nightlights.lua:25: in function <data/globalev
ents/scripts/nightlights.lua:22>
[Error - GlobalEvents::think] Failed to execute event: nightlights

Lua Script Error: [GlobalEvent Interface]
data/globalevents/scripts/nightlights.lua:onThink
data/globalevents/scripts/nightlights.lua:14: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/globalevents/scripts/nightlights.lua:14: in function 'swapLights'
        data/globalevents/scripts/nightlights.lua:25: in function <data/globalev
ents/scripts/nightlights.lua:22>
[Error - GlobalEvents::think] Failed to execute event: nightlights

Lua Script Error: [GlobalEvent Interface]
data/globalevents/scripts/nightlights.lua:onThink
data/globalevents/scripts/nightlights.lua:14: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/globalevents/scripts/nightlights.lua:14: in function 'swapLights'
        data/globalevents/scripts/nightlights.lua:25: in function <data/globalev
ents/scripts/nightlights.lua:22>
[Error - GlobalEvents::think] Failed to execute event: nightlights

Lua Script Error: [GlobalEvent Interface]
data/globalevents/scripts/nightlights.lua:onThink
data/globalevents/scripts/nightlights.lua:14: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/globalevents/scripts/nightlights.lua:14: in function 'swapLights'
        data/globalevents/scripts/nightlights.lua:25: in function <data/globalev
ents/scripts/nightlights.lua:22>
[Error - GlobalEvents::think] Failed to execute event: nightlights

Same error that it throws for @Codex NG script.
 
Back
Top