• 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 check a player's specific house by ID

Obito

0x1337
Joined
Feb 17, 2011
Messages
351
Solutions
8
Reaction score
151
Location
Egypt
Hi,
I'm trying to figure out how to check if a player has a custom house by name, not any house only a specific one, and if so, how to get the location of that house.
Additionally, I need to create a custom item that only the owner of a custom house can use.
Specifically, I need to be able to check the custom house of a player named Mark for example and I don't know if this can be done using queries or not, and then assign a custom item ID to an item that only Mark can use to teleport to his custom house and nobody else could use it except the house owner.
Does anyone know how I can do this? Any help or examples would be greatly appreciated.

I'm aware that the script linked below works to teleport the player to their house. However, I need to modify it so that it only works for a specific house, and only the house owner can use it.


Thanks in advance.

1683303627703.png

Edit: I had an idea that I think would work. We could use a storage value. If the player bought this item, I could basically assign it to the previous script to make the item work for the player who bought it. However, there is a problem. If another player bought the same item, it would ruin the storage. This would mean that both players would have the same storage.
I'm not sure if this is the best way to write this and I don't know if there is any shortcut function that would catch what I want, but I appreciate any suggestions.
 
Last edited:
Solution
Lua:
local wantedHouseName = "Whatever you called it."
local wantedHouseId = -1
local houses = Game.getHouses()
for i = 1, #houses do
    local houseName = houses[i]:getName()
    if houseName == wantedHouseName then
        wantedHouseId = houses[i]:getId()
        break
    end
end

-- do whatever you want with the houseId
But as said above, it's easier to just use player:getHouse() if you can manually provide the houseId
But let's assume you wanted to use the houseName like you said.

Lua:
local specificHouseName = "The house name, with correct Capitilization"
local specificHouseItemId = 1111

-- this function assumes that every house in the game has a unique name
local function teleportPlayerToSpecificHouse(playerId...
If it's only going to be a specific house, why does it have to be by name. Surely you know the ID of this house?
I apologize, I meant to post the ID thought it can be done with the house name for example as I posted the house photo called "Starfall citadel" to check who owns it. I have edited the post.
 
Last edited:
if it's an on use script. You just call player:getHouse(), check its not nil, and then call :getId(), and compare that to the house id.
 
if it's an on use script. You just call player:getHouse(), check its not nil, and then call :getId(), and compare that to the house id.
Thank you for your solution. I will try it out and let you know how it goes.
 
Lua:
local wantedHouseName = "Whatever you called it."
local wantedHouseId = -1
local houses = Game.getHouses()
for i = 1, #houses do
    local houseName = houses[i]:getName()
    if houseName == wantedHouseName then
        wantedHouseId = houses[i]:getId()
        break
    end
end

-- do whatever you want with the houseId
But as said above, it's easier to just use player:getHouse() if you can manually provide the houseId
But let's assume you wanted to use the houseName like you said.

Lua:
local specificHouseName = "The house name, with correct Capitilization"
local specificHouseItemId = 1111

-- this function assumes that every house in the game has a unique name
local function teleportPlayerToSpecificHouse(playerId, houseName)

    local houseId = -1
    local houses = Game.getHouses()
    
    for i = 1, #houses do
        local _houseName = houses[i]:getName()
        if _houseName == houseName then
            houseId = houses[i]:getId()
            break
        end
    end
    
    if houseId == -1 then
        return "house not found"
    end
    
    local player = Player(playerId)
    if not player then
        return "player not found"
    end
    
    local playerHouse = player:getHouse()
    if not playerHouse then
        return "player does not own a house"
    end
    
    local playerHouseId = playerHouse:getId()
    if playerHouseId ~= houseId then
        return "player is not owner of this house"
    end
    
    local houseExit = playerHouse:getExitPosition()
    player:teleportTo(houseExit)
    return true
end

local specificHouseTeleport = Action()

function specificHouseTeleport.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local teleported = teleportPlayerToSpecificHouse(player:getId(), specificHouseName)
    if teleported ~= true then
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        player:sendTextMessage(MESSAGE_STATUS_SMALL, teleported)
        return false
    end
    return true
end

specificHouseTeleport:id(specificHouseItemId)
specificHouseTeleport:register()
 
Solution
Lua:
local wantedHouseName = "Whatever you called it."
local wantedHouseId = -1
local houses = Game.getHouses()
for i = 1, #houses do
    local houseName = houses[i]:getName()
    if houseName == wantedHouseName then
        wantedHouseId = houses[i]:getId()
        break
    end
end

-- do whatever you want with the houseId
But as said above, it's easier to just use player:getHouse() if you can manually provide the houseId
But let's assume you wanted to use the houseName like you said.

Lua:
local specificHouseName = "The house name, with correct Capitilization"
local specificHouseItemId = 1111

-- this function assumes that every house in the game has a unique name
local function teleportPlayerToSpecificHouse(playerId, houseName)

    local houseId = -1
    local houses = Game.getHouses()
   
    for i = 1, #houses do
        local _houseName = houses[i]:getName()
        if _houseName == houseName then
            houseId = houses[i]:getId()
            break
        end
    end
   
    if houseId == -1 then
        return "house not found"
    end
   
    local player = Player(playerId)
    if not player then
        return "player not found"
    end
   
    local playerHouse = player:getHouse()
    if not playerHouse then
        return "player does not own a house"
    end
   
    local playerHouseId = playerHouse:getId()
    if playerHouseId ~= houseId then
        return "player is not owner of this house"
    end
   
    local houseExit = playerHouse:getExitPosition()
    player:teleportTo(houseExit)
    return true
end

local specificHouseTeleport = Action()

function specificHouseTeleport.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local teleported = teleportPlayerToSpecificHouse(player:getId(), specificHouseName)
    if teleported ~= true then
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        player:sendTextMessage(MESSAGE_STATUS_SMALL, teleported)
        return false
    end
    return true
end

specificHouseTeleport:id(specificHouseItemId)
specificHouseTeleport:register()
Lua:
local player = Player(playerId)
if not player then
    return "player not found"
end
    
local playerHouse = player:getHouse()
if not playerHouse then
    return "player does not own a house"
end
Would be best to do these checks before even looping through the houses, especially if you have a lot, just for efficiency.
 
Lua:
local wantedHouseName = "Whatever you called it."
local wantedHouseId = -1
local houses = Game.getHouses()
for i = 1, #houses do
    local houseName = houses[i]:getName()
    if houseName == wantedHouseName then
        wantedHouseId = houses[i]:getId()
        break
    end
end

-- do whatever you want with the houseId
But as said above, it's easier to just use player:getHouse() if you can manually provide the houseId
But let's assume you wanted to use the houseName like you said.

Lua:
local specificHouseName = "The house name, with correct Capitilization"
local specificHouseItemId = 1111

-- this function assumes that every house in the game has a unique name
local function teleportPlayerToSpecificHouse(playerId, houseName)

    local houseId = -1
    local houses = Game.getHouses()
  
    for i = 1, #houses do
        local _houseName = houses[i]:getName()
        if _houseName == houseName then
            houseId = houses[i]:getId()
            break
        end
    end
  
    if houseId == -1 then
        return "house not found"
    end
  
    local player = Player(playerId)
    if not player then
        return "player not found"
    end
  
    local playerHouse = player:getHouse()
    if not playerHouse then
        return "player does not own a house"
    end
  
    local playerHouseId = playerHouse:getId()
    if playerHouseId ~= houseId then
        return "player is not owner of this house"
    end
  
    local houseExit = playerHouse:getExitPosition()
    player:teleportTo(houseExit)
    return true
end

local specificHouseTeleport = Action()

function specificHouseTeleport.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local teleported = teleportPlayerToSpecificHouse(player:getId(), specificHouseName)
    if teleported ~= true then
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        player:sendTextMessage(MESSAGE_STATUS_SMALL, teleported)
        return false
    end
    return true
end

specificHouseTeleport:id(specificHouseItemId)
specificHouseTeleport:register()
It's exactly what I needed and it works perfectly. Thanks so much, @Xikini for your help! and it's a great idea!
 
Last edited:
Back
Top