This is my script for cloning a max of 4 houses from replicated storage. (cloning is done by remote event). this script is inside a spawn gui, which when clicks clones one house.
--local script
local intg = 0
local player = game.Players.LocalPlayer
local FindHotel = game.Workspace.Hotels:WaitForChild("Hotel")
script.Parent.MouseButton1Click:Connect(function()
if intg <= 3 and player.leaderstats.Cash.Value >= 50 and not FindHotel then
game.ReplicatedStorage.AddBuildings.AddHouse:FireServer()
intg = intg + 1
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 50
script.Parent.Visible = true
elseif intg <= 3 and player.leaderstats.Cash.Value < 50 and not FindHotel then
script.Parent.Parent.parent.Insufficient.Visible = true
wait(1)
script.Parent.Parent.parent.Insufficient.Visible = false
script.Parent.Visible = true
end
local House = game.Workspace.Houses:WaitForChild("House")
while intg >= 1 do
House.AncestryChanged:Wait()
if not House:IsDescendantOf(workspace) then
intg = intg - 1
print("intg=intg+1 executed")
end
end
end)
--server script
game.ReplicatedStorage.AddBuildings.AddHouse.OnServerEvent:Connect(function(plr, intg)
local House = game.ReplicatedStorage.Buildings.House1
local ClonedHouse = House:Clone()
ClonedHouse.Parent = workspace.Houses
ClonedHouse.Position = Vector3.new(146.5, 2.5, -236.5)
ClonedHouse.Name = "House"
ClonedHouse.Anchored = false
end)
I also have a destroy gui which destroys I clone if clicked once. Destroy Gui: --local script
local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
if game.Workspace.Houses:FindFirstChild("House") then
game.ReplicatedStorage.AddBuildings.DestroyHouse:FireServer()
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 25
end
end)
--server script
game.ReplicatedStorage.AddBuildings.DestroyHouse.OnServerEvent:Connect(function(plr, intg)
local House = game.Workspace.Houses:WaitForChild("House")
House:Destroy()
end)
The Issue
So when I click the spawn gui after I join the game, I can get only a max of 4 clones. I keep clicking beyond that but nothing happens.
Then I click the destroy gui once and again start clicking the spawn gui. now cloned houses get spammed.
I have used an integer variable to limit the clones. Why is it inefficient? Is there any mistake in the script which has to do anything with it? Please help me thanks in advance!

2 comments
but I have also made the same thing, right?
is there another way to prevent that glitch (4 or over) without breaking the script?