• 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 House info, data to string [TFS1.2]

X X X

Newb
Joined
Jul 26, 2015
Messages
148
Reaction score
13
Hello,

Trying to create (what I thought) was a simple talkaction script that players could use to get info about their houses. Here is what I have:
Lua:
function onSay(player, words, param)
    if player:getHouse() == nil then
       player:sendCancelMessage("You do not currently own a house.")
    return false
    end
    
    local house = player:getHouse()
    local town = house:getTown(house)
    local rent = house:getRent(house)
    
    print(house)
    print(town)
    print(rent)

    player:sendTextMessage(MESSAGE_INFO_DESCR, house ..'\n'.. town ..'\n'.. rent)
    
return true
end

I was getting errors about concatenating userdata, so that is why I have the prints in the script. The "rent" variable is assigned to the correct number, 220, but the data assigned to the local "house" and "town" variables are not strings of the house name or town name, but memory locations. The print is this:

Code:
userdata: 0x402bc058
userdata: 0x402bc980
220

My question is, how do I get this to print out a string instead of data?
 
Solution
Just query the database (assuming you use MyAAC):

Lua:
function onSay(player, words, param)
    if player:getHouse() == nil then
       player:sendCancelMessage("You do not currently own a house.")
    return false
    end
   
    local playerId = player:getGuid() --get the player database ID
    local resultId = db.storeQuery("SELECT `town_id` FROM `houses` WHERE `owner` = " .. playerId) --queries the database for the town id (a number value) based on the owner id
    if resultId ~= false then --function to only get the true query
        --define variables
        local period = configManager.getString(configKeys.HOUSE_RENT_PERIOD)
        local house = player:getHouse()
        local name = house:getName()
        local rent =...
Hello,

Trying to create (what I thought) was a simple talkaction script that players could use to get info about their houses. Here is what I have:
Lua:
function onSay(player, words, param)
    if player:getHouse() == nil then
       player:sendCancelMessage("You do not currently own a house.")
    return false
    end
  
    local house = player:getHouse()
    local town = house:getTown(house)
    local rent = house:getRent(house)
  
    print(house)
    print(town)
    print(rent)

    player:sendTextMessage(MESSAGE_INFO_DESCR, house ..'\n'.. town ..'\n'.. rent)
  
return true
end

I was getting errors about concatenating userdata, so that is why I have the prints in the script. The "rent" variable is assigned to the correct number, 220, but the data assigned to the local "house" and "town" variables are not strings of the house name or town name, but memory locations. The print is this:

Code:
userdata: 0x402bc058
userdata: 0x402bc980
220

My question is, how do I get this to print out a string instead of data?
Tried like this?

Lua:
local house = player:getHouse()
local town = house:getTown()
local rent = house:getRent()
 
Thanks for the reply, unfortunately whether or not there is a variable doesn't change the fact that it only returns userdata.
 
Lua:
function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. dump(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end

print("House Table" .. "\n" .. dump(house))
 
Just query the database (assuming you use MyAAC):

Lua:
function onSay(player, words, param)
    if player:getHouse() == nil then
       player:sendCancelMessage("You do not currently own a house.")
    return false
    end
   
    local playerId = player:getGuid() --get the player database ID
    local resultId = db.storeQuery("SELECT `town_id` FROM `houses` WHERE `owner` = " .. playerId) --queries the database for the town id (a number value) based on the owner id
    if resultId ~= false then --function to only get the true query
        --define variables
        local period = configManager.getString(configKeys.HOUSE_RENT_PERIOD)
        local house = player:getHouse()
        local name = house:getName()
        local rent = house:getRent()
        local townId = result.getNumber(resultId, "town_id") --set variable to numeric value based on query result
        local town = Town(townId) --returns a string of town name from numeric value
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You own '.. name ..' in '.. town:getName(town) ..'. The rent is '.. rent ..' gold '.. period ..'.') --print message, no matter what you change your town name too or rent period too the message will be correct
        result.free(resultId) --free the memory associated with the query result before ending the function
    end
   
return false
end
 
Solution
Back
Top