Check if player owns a different hosue?

So im trying to check if a player owns a different house and its not working, its letting me buy the house still, and help?

		for _,s in pairs(game.Workspace.Map.BuyableDoors:GetChildren()) do
		if s.Owner.Value ~= Player.Name then
		script.Parent.Owner.Value = Player.Name
		else
1 Like

Everything looks fine from this snippet of code. Can you show us the entire script?

Also, I assume you’re running this server-sided? If you’re running this on the client, the change to the Owner value isn’t going to replicate to other players.

I am running it on the server both times yes, here is the full code.

script.Parent.dothething.OnServerInvoke = function(Player,Action)
	if Action == 'Knock' then
		script.Parent.Door.Door.Knock:Play()
	elseif Action == 'Buy' then
		for _,s in pairs(game.Workspace.Map.BuyableDoors:GetChildren()) do
		if s.Owner.Value ~= Player.Name then
		script.Parent.Owner.Value = Player.Name
		else
	end
end
end
end

if s.Owner.Value ~= Player.Name then
This will probably return true UNLESS the player owns ALL of the houses. The loop checks for each house, not the entire thing.

You should do this instead.

function checkOwned(player)
    local owned = false

    for _,s in pairs(game.Workspace.Map.BuyableDoors:GetChildren()) do
        if s.Owner.Value == player.Name then
            owned = true
        end
    end

    return owned
end

if checkOwned(player) then
-- player already owns a house!
else
-- player doesn't own a house
end
2 Likes
-- Player, the player that is trying to buy the house
-- House, the house that player is trying to buy
local function canBeBought(Player, House)
	if House.Owner.Value ~= nil then
		-- House is already taken
		return false
	else
		local OwnsOtherHouse = false
		for _, v in pairs(workspace.Map.BuyableDoors:GetChildren()) do
			if v.Owner.Value == Player then
				OwnsOtherHouse = true
				break
			end
		end
		if OwnsOtherHouse then
			-- Cannot take another house, because player already owns one
			return false
		else
			-- Player can take the house
			return true
		end
	end
end

I made function to check based on each Object Value that contains player object or nil

1 Like