Loop not breaking correctly

Basically I’m checking a table to see if a house has a price. Check. Then checking to see if the players owns that house. If they do, it should NOT create the coin label, however if they don’t own said house then it does.

if houseData.Price > 0 then
	for i, v in pairs(PlayersHouses) do
		if i == house.Name then
			if house.Name == PlayersHouse then
				Price.Text = 'SELECTED'
				Bottom.ImageColor3 = Color3.fromRGB(7, 189, 255)
				Top.BackgroundColor3 = Color3.fromRGB(7, 189, 255)
			else
				Price.Text = 'SELECT'
				Bottom.ImageColor3 = FavouriteColor.Value
				Top.BackgroundColor3 = FavouriteColor.Value
			end
				
            break
		end
	end

	Coin = Instance.new('ImageLabel')
	Coin.AnchorPoint = Vector2.new(0, 0.5)
	Coin.BackgroundTransparency = 1
	Coin.Name = 'Coin'
	Coin.Position = UDim2.new(0.1, 0, 0.5, 0)
	Coin.Size = UDim2.new(0.75, 0, 0.75, 0)
	Coin.ZIndex = 4
	Coin.Image = 'rbxgameasset://Images/Coin'
	Coin.ScaleType = Enum.ScaleType.Fit

	local AspectRatio = Instance.new('UIAspectRatioConstraint')
	
	Price.Text = houseData.Price
	
	Bottom.ImageColor3 = Color3.fromRGB(255, 183, 0)
	Top.BackgroundColor3 = Color3.fromRGB(255, 183, 0)
	
	AspectRatio.Parent = Coin
	Coin.Parent = Bottom
end

Problem is even if they own the house, it still creates the coin.
If I chuck print(i) it will print all 4 houses (Default, Default2, Default3, Default4)

  Default4
  Default3
  Default4
  Default3
  Default
  Default2

But with Default2 and Default3 it still creates the coin

1 Like

What you should do is insert the coin creation code into the part that checks if the player DOESN’T own the specified house. That way, if the player does own it, the coin isn’t created, but if they don’t, it is.

Also, the break in the function is unnecessary. You don’t have anything to break from, because at that point, you will have already checked the two possible conditions: player owns house and not player owns house.

if houseData.Price > 0 then
		for i, v in pairs(PlayersHouses) do
			print(i)
			if i == house.Name then
				if house.Name == PlayersHouse then
					Price.Text = 'SELECTED'
					Bottom.ImageColor3 = Color3.fromRGB(7, 189, 255)
					Top.BackgroundColor3 = Color3.fromRGB(7, 189, 255)
				else
					Price.Text = 'SELECT'
					Bottom.ImageColor3 = FavouriteColor.Value
					Top.BackgroundColor3 = FavouriteColor.Value
				end
			else
				Coin = Instance.new('ImageLabel')
				Coin.AnchorPoint = Vector2.new(0, 0.5)
				Coin.BackgroundTransparency = 1
				Coin.Name = 'Coin'
				Coin.Position = UDim2.new(0.1, 0, 0.5, 0)
				Coin.Size = UDim2.new(0.75, 0, 0.75, 0)
				Coin.ZIndex = 4
				Coin.Image = 'rbxgameasset://Images/Coin'
				Coin.ScaleType = Enum.ScaleType.Fit
		
				local AspectRatio = Instance.new('UIAspectRatioConstraint')
				
				Price.Text = houseData.Price
				
				Bottom.ImageColor3 = Color3.fromRGB(255, 183, 0)
				Top.BackgroundColor3 = Color3.fromRGB(255, 183, 0)
				
				AspectRatio.Parent = Coin
				Coin.Parent = Bottom
			end
		end

If I do else tho it still creates the coin

It looks like you’re using a dictionary key to hold the house name. If so, you can just do

if PlayersHouses[house.Name] then
    --they own it
else
   -- they do not 
end