• 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 How to storage guildname

Fortera Global

Intermediate OT User
Joined
Nov 20, 2015
Messages
1,180
Solutions
2
Reaction score
117
I want to save the guild name in the database so that after server save it will not be deleted.

I tested (works but in server save this disappear):
Lua:
Game.setStorageValue(966632, getPlayerGuildName(cid))

and (dont work, give an error for store):
Lua:
 setGlobalStorageValue(966632, getPlayerGuildName(cid))

tfs 1.2
how I can store the guild name?
 
Solution
Thanks, but I want to save the guild so that it can appear in the message when someone tries to enter teleport and show which guild is the owner of the place.
The only method would be to save the guild ID in the database, we know how to save the guild ID, now how to get the guild name from the guildID?

Look at this code:
Lua:
function onStepIn(creature, item, position, fromPosition)
    if creature:isPlayer() then
        local player_guild = creature:getGuild()
        local zone_guild = Guild(getGlobalStorageValue(9999))
       
        if zone_guild:getId() == player_guild:getId() then
            position:sendMagicEffect(14)
        else
            creature:sendTextMessage(MESSAGE_INFO_DESCR, "You do not belong to the guild...
Wait what?

Guild names are already in the database. (Check guilds table).

To get guild name from player:
Lua:
local guild = player:getGuild()
if guild ~= nil then -- If player is in a guild
    local guildname = guild:getName() -- Returns the name of the guild the player is in.
end
 
If it gave you an error, why didn't you post it?
Along with your global storage function.
 
Wait what?

Guild names are already in the database. (Check guilds table).

To get guild name from player:
Lua:
function onStepIn(cid, item, pos, fromPosition)
local pos = getThingPos(cid)
local player = Player(cid)
local guild = player:getGuild()
if guild ~= nil then -- If player is in a guild
    local guildname = guild:getName() -- Returns the name of the guild the player is in.
end

I wanted to save a guild so that only she could enter a teleport, when the player is not from this guild X will appear a message to him saying that guild X is the owner of the place.

Lua:
function onStepIn(cid, item, pos, fromPosition)
local pos = getThingPos(cid)
local player = Player(cid)
local guildId = guild:getId()
local newGuild = Guild(guildId)

if getGlobalStorageValue(9999) == newGuild:getName() then
doSendMagicEffect(getThingPos(cid), 14)
else
doPlayerSendCancel(cid, "[MSG] You do not belong to the guild "..getGlobalStorageValue(9999)..".")
end

Script.lua:4: attempt to index local 'guild' (a nil value)
 
This script only sees if player have access to enter somewhere.
Can you post the script that decides which guild owns this place?

Lua:
function onStepIn(creature, item, position, fromPosition)
    if creature:isPlayer() then
        local player_guild = creature:getGuild()
         
        if getGlobalStorageValue(9999) == player_guild:getName() then
            position:sendMagicEffect(14)
        else
            creature:sendTextMessage(MESSAGE_INFO_DESCR, "You do not belong to the guild "..getGlobalStorageValue(9999)..".")
            creature:teleportTo(fromPosition, true)
        end
    end
    return true
end
 
This script only sees if player have access to enter somewhere.
Can you post the script that decides which guild owns this place?

Lua:
function onStepIn(creature, item, position, fromPosition)
    if creature:isPlayer() then
        local player_guild = creature:getGuild()
    
        if getGlobalStorageValue(9999) == player_guild:getName() then
            position:sendMagicEffect(14)
        else
            creature:sendTextMessage(MESSAGE_INFO_DESCR, "You do not belong to the guild "..getGlobalStorageValue(9999)..".")
            creature:teleportTo(fromPosition, true)
        end
    end
    return true
end

Yes, its good, but how to setGlobalStorageValue(9999) with this player_guild:getName() ?
The check we have, now we have to set/save

This script decide who own the guild BUT its disappear after server save:
Lua:
Game.setStorageValue(9999, getPlayerGuildName(cid))
because this above dont save in database, I want save this on database with setGlobalStorageValue

If I use setGlobalStorageValue(9999, player_guild) , give an error:
attempt to concatenate local 'value' (a userdata value)

@Znote
 
Last edited by a moderator:
Depends when the guild is being rewarded.
I mean, this script's supposed to pass a player through only if he's a member of some winner guild, right?
You gotta add a line to the script responsible for selecting a winner.

It may also happen that this function doesn't operate with strings, so i would recommend using guild Id as value instead.
 
I understand what you want to do, Checks if the player belongs to the guild once he enters the teleport or whatever.
Problem is that setGlobalStorageValue one allows a certain type as a value while you're trying to enter a userdata value, they are different types.
You could perhaps try to use the Guild id instead.
ex setGlobalStorageValue(9999, Guild:getId())
Regards Alw
 
Depends when the guild is being rewarded.
I mean, this script's supposed to pass a player through only if he's a member of some winner guild, right?
You gotta add a line to the script responsible for selecting a winner.

It may also happen that this function doesn't operate with strings, so i would recommend using guild Id as value instead.

"You gotta add a line to the script responsible for selecting a winner."
This is what I'm trying to do, save the winning guild by name and not id
 
setGlobalStorageValue function???

Lua:
function setGlobalStorageValue(key, value)
    if getGlobalStorageValue(key, true) then
       db.query("UPDATE `global_storage` SET `value` = '".. value .. "' WHERE `key` = ".. key .. ";")
    else
        db.query("INSERT INTO `global_storage` (`key`, `value`) VALUES (".. key ..", ".. value ..");")
    end
    return true
end

We can't save names, we got error when try
 
Don't use guild name, use guild id instead. Much faster. guild:getId()
Lua:
function onStepIn(creature, item, position, fromPosition)
    if creature:isPlayer() then
        local player_guild = creature:getGuild()
        local zone_guild = Guild(getGlobalStorageValue(9999))
         
        if zone_guild:getId() == player_guild:getId() then
            position:sendMagicEffect(14)
        else
            creature:sendTextMessage(MESSAGE_INFO_DESCR, "You do not belong to the guild ".. zone_guild:getName() ..".")
            creature:teleportTo(fromPosition, true)
        end
    end
    return true
end

SQL:
CREATE TABLE IF NOT EXISTS `global_storage` (
  `key` int(10) unsigned NOT NULL DEFAULT '0',
  `value` int(11) DEFAULT NULL,
  PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;

forgottenserver/game.lua at master · otland/forgottenserver · GitHub

Lua:
if not globalStorageTable then
    globalStorageTable = {}
end

function Game.getStorageValue(key)
    -- Return from local table if possible
    if globalStorageTable[key] ~= nil then
        return globalStorageTable[key]
    end

    -- Else look for it on the DB
    local dbData = db.storeQuery("SELECT `value` FROM `global_storage` WHERE `key` = " .. key .. " LIMIT 1;")
    if dbData ~= false then
        local value = result.getNumber(dbData, "value")
        if value ~= nil then
            -- Save it to globalStorageTable
            globalStorageTable[key] = value
            return value
        end
    end

    return nil
end

function Game.setStorageValue(key, value)
    globalStorageTable[key] = value

    local dbData = db.storeQuery("SELECT `value` FROM `global_storage` WHERE `key` = " .. key .. " LIMIT 1;")
    if dbData ~= false then
        db.query("UPDATE `global_storage` SET `value`='".. value .."' WHERE `key` = " .. key .. " LIMIT 1;")
    else
        db.query("INSERT INTO `global_storage` (`key`, `value`) VALUES (" .. key .. ", " .. value .. ");")
    end
end
 
Last edited:
Don't use guild name, use guild id instead. Much faster. guild:getId()
Lua:
function onStepIn(creature, item, position, fromPosition)
    if creature:isPlayer() then
        local player_guild = creature:getGuild()
        local zone_guild = Guild(getGlobalStorageValue(9999))
        
        if zone_guild:getId() == player_guild:getId() then
            position:sendMagicEffect(14)
        else
            creature:sendTextMessage(MESSAGE_INFO_DESCR, "You do not belong to the guild ".. zone_guild:getName() ..".")
            creature:teleportTo(fromPosition, true)
        end
    end
    return true
end

SQL:
CREATE TABLE IF NOT EXISTS `global_storage` (
  `key` int(10) unsigned NOT NULL DEFAULT '0',
  `value` int(11) DEFAULT NULL,
  PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;

forgottenserver/game.lua at master · otland/forgottenserver · GitHub

Lua:
if not globalStorageTable then
    globalStorageTable = {}
end

function Game.getStorageValue(key)
    -- Return from local table if possible
    if globalStorageTable[key] ~= nil then
        return globalStorageTable[key]
    end

    -- Else look for it on the DB
    local dbData = db.storeQuery("SELECT `value` FROM `global_storage` WHERE `key` = " .. key .. " LIMIT 1;")
    if dbData ~= false then
        local value = result.getNumber(dbData, "value")
        if value ~= nil then
            -- Save it to globalStorageTable
            globalStorageTable[key] = value
            return value
        end
    end

    return nil
end

function Game.setStorageValue(key, value)
    globalStorageTable[key] = value

    local dbData = db.storeQuery("SELECT `value` FROM `global_storage` WHERE `key` = " .. key .. " LIMIT 1;")
    if dbData ~= false then
        db.query("UPDATE `global_storage` SET `value`='".. value .."' WHERE `key` = " .. key .. " LIMIT 1;")
    else
        db.query("INSERT INTO `global_storage` (`key`, `value`) VALUES (" .. key .. ", " .. value .. ");")
    end
end


Thanks, but I want to save the guild so that it can appear in the message when someone tries to enter teleport and show which guild is the owner of the place.
The only method would be to save the guild ID in the database, we know how to save the guild ID, now how to get the guild name from the guildID?
 
Thanks, but I want to save the guild so that it can appear in the message when someone tries to enter teleport and show which guild is the owner of the place.
The only method would be to save the guild ID in the database, we know how to save the guild ID, now how to get the guild name from the guildID?

Look at this code:
Lua:
function onStepIn(creature, item, position, fromPosition)
    if creature:isPlayer() then
        local player_guild = creature:getGuild()
        local zone_guild = Guild(getGlobalStorageValue(9999))
       
        if zone_guild:getId() == player_guild:getId() then
            position:sendMagicEffect(14)
        else
            creature:sendTextMessage(MESSAGE_INFO_DESCR, "You do not belong to the guild ".. zone_guild:getName() ..".")
            creature:teleportTo(fromPosition, true)
        end
    end
    return true
end

Lua:
local zone_guild = Guild(getGlobalStorageValue(9999))
zone_guild:getName() -- Returns guild name
 
Solution
Back
Top