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

TFS1.2. os.date doesnt work onThink function?

Lopaskurwa

Active Member
Joined
Oct 6, 2017
Messages
873
Solutions
2
Reaction score
49
Hi im using os.date function so particular global storage would activate on described date, but noticed that it ignores the days i defined and it activated all the time and global interval i set up is 1 hour

Lua:
local days = {"Saturday", "Sunday"}
function onThink(interval)
    local state = Game.getGameState()
    if state == GAME_STATE_CLOSED or state == GAME_STATE_CLOSING then
        return true
    end

    local storage = getGlobalStorageValue(Storage.Weekend)
    if storage >= 1 and not table.contains(days, os.date("%A")) then
        setGlobalStorageValue(Storage.Weekend, 0)
        broadcastMessage("disabled!", MESSAGE_STATUS_WARNING)
        return true
    end

    if storage < 1 then
        setGlobalStorageValue(Storage.Weekend, 1)
        broadcastMessage("activated!", MESSAGE_STATUS_WARNING)
    end

    return true
end
 
Solution
B
Lua:
local days = {"Saturday", "Sunday"}
function onThink(interval)
    local state = Game.getGameState()
    if state == GAME_STATE_CLOSED or state == GAME_STATE_CLOSING then
        return true
    end

    local day = os.date("%A")
    local storage = getGlobalStorageValue(Storage.Weekend)
    if storage < 1 and table.contains(days, day) then
        setGlobalStorageValue(Storage.Weekend, 1)
        broadcastMessage("activated!", MESSAGE_STATUS_WARNING)
        return true
    end

    if storage == 1 and not table.contains(days, day) then
        setGlobalStorageValue(Storage.Weekend, 0)
        broadcastMessage("deactivated!", MESSAGE_STATUS_WARNING)
    end

    return true
end

This will activate every Saturday at 00:00 and...
if storage >= 1 and not table.contains(days, os.date("%A")) then

This checks if it's any day other then Saturday or Sunday.

So assuming you used it today.. and the storage was currently 1...

First hour
-> It's not Saturday or Sunday, and storage >= 1
-> broadcasts "disabled!"
-> storage set to 0

Second hour
-> Storage is < 1
-> broadcasts "activated!"
-> storage set to 0

Third hour
-> It's not Saturday or Sunday, and storage >= 1
-> broadcasts "disabled!"
-> storage set to 0

-- repeat.


-------------
So, what is it you want to happen?
Because it's not clear at the moment..
 
Lua:
local days = {"Saturday", "Sunday"}
function onThink(interval)
    local state = Game.getGameState()
    if state == GAME_STATE_CLOSED or state == GAME_STATE_CLOSING then
        return true
    end

    local day = os.date("%A")
    local storage = getGlobalStorageValue(Storage.Weekend)
    if storage < 1 and table.contains(days, day) then
        setGlobalStorageValue(Storage.Weekend, 1)
        broadcastMessage("activated!", MESSAGE_STATUS_WARNING)
        return true
    end

    if storage == 1 and not table.contains(days, day) then
        setGlobalStorageValue(Storage.Weekend, 0)
        broadcastMessage("deactivated!", MESSAGE_STATUS_WARNING)
    end

    return true
end

This will activate every Saturday at 00:00 and deactivate every Monday at 00:00, presuming the script runs at xx:00
 
Solution
if storage >= 1 and not table.contains(days, os.date("%A")) then

This checks if it's any day other then Saturday or Sunday.

So assuming you used it today.. and the storage was currently 1...

First hour
-> It's not Saturday or Sunday, and storage >= 1
-> broadcasts "disabled!"
-> storage set to 0

Second hour
-> Storage is < 1
-> broadcasts "activated!"
-> storage set to 0

Third hour
-> It's not Saturday or Sunday, and storage >= 1
-> broadcasts "disabled!"
-> storage set to 0

-- repeat.


-------------
So, what is it you want to happen?
Because it's not clear at the moment..
This checks if it's any day other then Saturday or Sunday. yea because if its other day then Saturday or Sunday it should set storage to 0, which is fine. Second hour you said shouldnt happen because its not Saturday or Sunday so it shouldnt activate. So basically what it should do is if its Saturday or Sunday storage is set to 1 which is activated, but if its not weekend (Saturday or Sunday) the storage should be set to 0 which is disabled, so its like bonus experience on weekends only. The reason why there is internval each hour is to set global storage to 0 if its not a weekend anymore so basically it becomes disabled automaticly and thats it, but this code for some reason activates every single day when it should activate only on Saturday and Sunday
Post automatically merged:

Lua:
local days = {"Saturday", "Sunday"}
function onThink(interval)
    local state = Game.getGameState()
    if state == GAME_STATE_CLOSED or state == GAME_STATE_CLOSING then
        return true
    end

    local day = os.date("%A")
    local storage = getGlobalStorageValue(Storage.Weekend)
    if storage < 1 and table.contains(days, day) then
        setGlobalStorageValue(Storage.Weekend, 1)
        broadcastMessage("activated!", MESSAGE_STATUS_WARNING)
        return true
    end

    if storage == 1 and not table.contains(days, day) then
        setGlobalStorageValue(Storage.Weekend, 0)
        broadcastMessage("deactivated!", MESSAGE_STATUS_WARNING)
    end

    return true
end

This will activate every Saturday at 00:00 and deactivate every Monday at 00:00, presuming the script runs at xx:00
0:00 midnight so it means 12:00 at night so basically entire Saturday is wasted isnt it?
 
Last edited:
Back
Top