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

Solved how to get value from database in LUA? TFS 1x

whitevo

Feeling good, thats what I do.
Joined
Jan 2, 2015
Messages
3,454
Solutions
1
Reaction score
627
Location
Estonia
How can i make local variable with value from database?
i want to get the numbers (or string, don't care) from players - onlinetime
and set it as variable, how can i do that?
local onlineTime = getOnlineTime()
 
What you can do:
  • Create a new Player method, e.g Player.getOnlineTime(self)
  • Use:
    • db.storeQuery("SELECT `onlinetime` FROM `players` WHERE `id` = " .. self:getGuid())
    • result.getNumber

Feel free to check the spoiler if you get stuck. Good luck :)

 
Instead of making new thread, i will just open up this and ask new question about querys

Now i want to get highest time value from table player_deaths, How can i get this?
Currently i see NIL value.

Code:
local playerID = player:getGuid()
    local lastDeath = db.storeQuery("SELECT MAX(time) FROM player_deaths WHERE player_id = "..playerID)
    print(result.getNumber)
    if player:getStorageValue(10003) < 1 and lastDeath < lastDeath+6*60*60 then -- Hardcore lives
        player:setStorageValue(10003, 1)
    end
 
Order by ?
like this??
db.storeQuery("SELECT Order_by(time) FROM player_deaths WHERE player_id = "..playerID)

my latest test was with:
db.storeQuery("SELECT MAX(time) AS time FROM player_deaths WHERE player_id = "..playerID)

but still gave me NIL
 
SELECT colum FROM table ORDER BY column
Code:
    local playerID = player:getGuid()
    local lastDeath = db.storeQuery("SELECT time FROM player_deaths WHERE player_id = "..playerID.." ORDER BY time")
    print(lastDeath)
    print(result.getNumber)
    if player:getStorageValue(10003) < 1 and lastDeath < lastDeath+6*60*60 then -- Hardcore lives
        player:setStorageValue(10003, 1)
    end
first print is 1
second print still is NIL

EDIT: tried like this too, still same prints
db.storeQuery("SELECT time FROM player_deaths WHERE player_id = "..playerID.." ORDER BY time DESC")

EDIT2: still same prints
db.storeQuery("SELECT time FROM player_deaths WHERE player_id = "..playerID.." ORDER BY player_deaths.time DESC")

jpy35s.png

EDIT3: Just for the record.
all the suggestions and my test querys WORK in database itself.
I always get the time values in order or the higest one if i use MAX(time)

Maybe there is something wrong with my
print(result.getNumber)
Perhaps it should be something else?
 
Last edited:
i did this:
for k, v in pairs(result) do
i got lot of functions as result.
i tested them all and i got was False.

I also tried
result.time (seemed logical, still a nil value)

I have no clue how to continue. Nothing seems to work.
 
:rolleyes:
Code:
local function something(playerGUID)
    local lastDeath = db.storeQuery("SELECT time FROM player_deaths WHERE player_id = " .. playerGUID .. " ORDER BY time")
    if lastDeath ~= false then
        local timeStamp = result.getNumber(lastDeath, "time")
        result.free(lastDeath)
        return timeStamp
    end
    return 0
end

something(player:getGuid())
 
:rolleyes:
Code:
local function something(playerGUID)
    local lastDeath = db.storeQuery("SELECT time FROM player_deaths WHERE player_id = " .. playerGUID .. " ORDER BY time")
    if lastDeath ~= false then
        local timeStamp = result.getNumber(lastDeath, "time")
        result.free(lastDeath)
        return timeStamp
    end
    return 0
end

something(player:getGuid())
i get ERRORS: getNumber is nil
this line: local timeStamp = result.getNumber(lastDeath, "time")

I tried several other possibilites too, all gave nil error.
local timeStamp = result.getNumber(lastDeath, time)
local timeStamp = result.getNumber(lastDeath)
local timeStamp = result.getNumber("time")
local timeStamp = result.getNumber(time)
local timeStamp = result.getSring(lastDeath, "time")
local timeStamp = result.getSring(lastDeath, time)
local timeStamp = result.getSring(lastDeath)
local timeStamp = result.getSring("time")
local timeStamp = result.getSring(time)

EDIT: ITS ALIVE!!!!
local timeStamp = result.getDataInt(lastDeath, "time")
was the function what made it work


Full Working script:
Code:
local function NinjaOPFunction(playerGUID)
local lastDeath = db.storeQuery("SELECT time FROM player_deaths WHERE player_id = " .. playerGUID .. " ORDER BY time")
   
    if lastDeath ~= false then
        local timeStamp = result.getDataInt(lastDeath, "time")
        result.free(lastDeath)
        return timeStamp
    end
return 0
end

local lastDeath = NinjaOPFunction(player:getGuid())
 
Last edited:
Back
Top