• 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 [TFS 1.3] Get script to fetch player.id (from database)

croix

Member
Joined
Aug 26, 2011
Messages
123
Reaction score
13
As title says, I want to update players query when an item is used, but how do I get the player ID of the person using the item?

(Hopefully this also works if I add something in the script that kicks the player before I run the query)

Cheers!
 
player:getId() return its ID I suppose, or if you want to use a query:

Lua:
    local resultId = db.storeQuery("SELECT `id` FROM `players` WHERE `name` = " .. player:getName())
    if resultId ~= false then
        local guid = result.getNumber(resultId, "id")
        result.free(resultId)
        return guid
    end

you can find a lot of examples in the compat.lua file
 
Lua:
function getPlayerGuidByName(playerName)
    local resultId = db.storeQuery('SELECT id FROM players WHERE name = ' .. db.escapeString(playerName))
    if not resultId then
        return false
    end
    local playerGuid = result.getNumber(resultId, 'id')
    result.free(resultId)
    return playerGuid
end
 
Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local playerID = player:getId() -- <- Player ID
    local playerGuID = player:getGuid() -- <- Player GuID
    return true
end
 
I don't know if I'm just tired or slow atm but I guess I don't even really need the ID, name should be OK? -
shouldn't players.name return the row? (Test is the charname)

Lua:
db.storeQuery("UPDATE `players` SET `skulltime` = '0' WHERE `players`.`name` = " .. player:getName())

1609199377202.png
 
Lua:
db.query("UPDATE `players` SET `skulltime` = '0' WHERE `name` = '" .. player:getName() .. "';")

Use db.query instead.

You shouldn't get any errors, I tried this on my own server. Make sure the script is loaded into the server.
 
Also, better use getGuid instead of getName (more optimized)

Like this:
Code:
db.query("UPDATE `players` SET `skulltime` = '0' WHERE `id` = '" .. player:getGuid() .. "';")
 
Also, better use getGuid instead of getName (more optimized)

Like this:
Code:
db.query("UPDATE `players` SET `skulltime` = '0' WHERE `id` = '" .. player:getGuid() .. "';")

Sweet, that seems to work fine, thanks!

Shouldn't it now be able to kick myself before the query is entered so that it actually falls through? Because the character has to be offline for the query to work.

Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local playerskull = player:getSkull()
    local skulls = {
                    SKULL_NONE,
                    SKULL_YELLOW,
                    SKULL_GREEN,
                    SKULL_WHITE,
                    SKULL_BLACK
                    }

    if isInArray(skulls, playerskull) then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You can't remove that skull!")
        return 0
    else
        player:setSkull(skulls[1])
        item:remove(1)
        addEvent(player:remove())
        db.query("UPDATE `players` SET `skulltime` = '0' WHERE `id` = '" .. player:getGuid() .. "';")
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Your skull has been removed!")
    end
return true
end

function getPlayerID(cid)
    return getPlayerIdByName(getPlayerName(cid))
end

Even though I kick myself with player:remove() (which brings back an error, but still kicks me..) the values of skulltime comes back. (possibly skips query cus of error?)
1609242851865.png
 
Last edited:
player:getGuid() is probably nil, since you're kicking the player?

Try this:
1. Store the players GUID in a variable.
2. Kick the player.
3. Run the SQL Query


Lua:
else
    player:setSkull(skulls[1])
    item:remove(1)

    -- Store the ID in a local variable.
    local playerGUID = player:getGuid()
    
    -- Kick the player.
    player:remove()
    
    -- Run the query.
    db.query("UPDATE `players` SET `skulltime` = '0' WHERE `id` = '" .. playerGUID .. "';")
    
    -- Sending a message to the player won't work, since he/she is kicked.
    -- If you really want to send them a message, then create an addEvent() that tries
    -- to fetch the player by ID, and send them a message. Reapeating X times for Y tries.   
end
 
player:getGuid() is probably nil, since you're kicking the player?

Try this:
1. Store the players GUID in a variable.
2. Kick the player.
3. Run the SQL Query


Lua:
else
    player:setSkull(skulls[1])
    item:remove(1)

    -- Store the ID in a local variable.
    local playerGUID = player:getGuid()
   
    -- Kick the player.
    player:remove()
   
    -- Run the query.
    db.query("UPDATE `players` SET `skulltime` = '0' WHERE `id` = '" .. playerGUID .. "';")
   
    -- Sending a message to the player won't work, since he/she is kicked.
    -- If you really want to send them a message, then create an addEvent() that tries
    -- to fetch the player by ID, and send them a message. Reapeating X times for Y tries.  
end
no its not, he saved the guid before kicking player so its stored in variable.

Numbers are not escaped in db query, so this is wrong
SQL:
db.query("UPDATE `players` SET `skulltime` = '0' WHERE `id` = '" .. player:getGuid() .. "';")

Correct query should be:
SQL:
db.query("UPDATE `players` SET `skulltime` = 0 WHERE `id` = " .. player:getGuid())
 
Back
Top