House Selection Script Only Spawning One House

I want to make a script that spawn a house a a specific plot when you click a button. The problem is that only one of the buttons work. I have everything in the right spot, named everything accordingly, and there are no errors in the output.

Local Script:

local C = script.Parent:GetChildren()
local Houses = game.Workspace:WaitForChild("Houses")
local PlotNum = script.Parent.PlotNum
local Event = game.ReplicatedStorage.HouseSelectEvent
local E = true
local Player = game.Players.LocalPlayer

for i = 1,#C do
	if C[i]:IsA("ImageButton") then
		C[i].MouseButton1Click:Connect(function()
			if E == true then
				E = false
				local Home = Houses:FindFirstChild(C[i].Name).Name
				Event:FireServer(Home, PlotNum.Value)
				print(Player, Home, PlotNum.Value)
				wait(5)
				E = true
			else return
			end
		end)
	end
end

Script:

local Event = game.ReplicatedStorage.HouseSelectEvent
local Houses = game.Workspace.Houses
local C = Houses:GetChildren()
local Players = game:GetService("Players")
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420

function SpawnHouse(Player, House, Plot)
	for i = 1,#C do
		if C[i].Name == House then
			print(Player, House, Plot)
			local clone = C[i]:Clone()
			clone.Parent = Plot
			clone:MakeJoints()
			clone.Door.SurfaceGui.Owner.Text = Player.Name
			clone:SetPrimaryPartCFrame(Plot.CFrame)
			local ID = Player.UserId
			local userId = tonumber(ID)
			local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
			Plot:FindFirstChildWhichIsA("Model").WindowBody.Window.SurfaceGui.ImageLabel.Image = content
		else return
		end
	end
end

Event.OnServerEvent:Connect(function(Player, House, PlotNum)
	local OwnsHouse = Player:FindFirstChild("OwnsHouse")
	Plot = game.Workspace:FindFirstChild("Plot "..PlotNum)
	Owner = Plot.Owner
	if ((Owner.Value == Player.Name or Owner.Value == "") and OwnsHouse.Value == false) and OwnsHouse.Value == false or (Owner.Value == Player.Name and OwnsHouse.Value == true) then
		Owner.Value = Player.Name
		OwnsHouse.Value = true
		if Plot:FindFirstChildWhichIsA("Model") then
			Plot:FindFirstChildWhichIsA("Model"):Destroy()
		end
		SpawnHouse(Player, House, Plot)
	else
		return
	end
	Players.PlayerRemoving:Connect(function(Plr)
		if Plr.Name == Owner.Value and Plot then
			Plot:FindFirstChildWhichIsA("Model"):Destroy()
			Owner.Value = ""
		end
	end)
end)

This is what the output prints when I click the different buttons:
image
You can see when I click the button for the modern house, it prints just fine and spawns in the house. When I click the button for the Daycare, it only prints in the client script, and doesn’t spawn in the Daycare.

When you try to spawn in the Daycare, does it destroy the previous plot and not spawn it or doesn’t destroy it? It probably one of the conditions in the if statement not matching up and thus returning the event.

The only thing I think it could be is that nothing sets OwnsHouse to false, meaning it’ll only work for the first time

Also why is there an event declaration in the RemoteEvent code? Shouldn’t that be outside so ti doesn’t get made many times?

It destroys the model on the plot before spawning. When I click the button for the Daycare, it destroy the model that is on the plot, but it doesn’t spawn in.

I have made it where saves the owner of the plot, so even if OwnsHouse is true, the player will still be able to spawn a house on that plot.

So it’s an issue with SpawnHouse. I see the issue

In this code, it will check the first house in table, is it the name of the house you selected? No? Then stop the loop. Did you mean to do else continue?

Basically, you’re making the loop check the first thing in the table and if it isn’t the name of your house, it will stop the loop completely because it’s in a function and a return in a function stops the code to return waht needs to be returned, either change else return to else continue or remove else return entirely

1 Like

It works now. I did not know that continue was a thing. Thank you!

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

1 Like