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!
make a numberValue. Everytime a clone is made, the value goes up by 1.
script.Parent:Clone().Parent script.Parent.NumebrValue.Value = script.Parent.NumebrValue.Value + 1 --or even easier script.Parent:Clone().Parent script.Parent.NumebrValue.Value:Increment(1)--something like that, look up how to increment values
and every time before cloning it, check to make sure there are no more than the maximum amount of clones. for example.
if Clones.Value >= 4 then --do not clone break--ends the code so it doesnt run again in the game. if you dont want it take it out. else--if its not 4 or over (sometimes glitched happen and it goes over limit, so i use greater in case that happens) script.Parent:Clone().Parent script.Parent.NumebrValue.Value = script.Parent.NumebrValue.Value + 1 end