Hi, I am making a generation system with storage and in this case I am using flowers. So basically if flowers < storage then the while loop stops. Problem is when the flowers decrease, the generation completely stops and I don’t know how to fix that.
Here’s the script
local function GenerateFlowers()
local Flower = FlowerAssets[chooseIndex(Rarity)]
local FlowerX = (FlowerBed.Position.X + math.random(-Size.X/2,Size.X/2))
local FlowerY = (FlowerBed.Position.Y * 4)
local FlowerZ = (FlowerBed.Position.Z + math.random(-Size.Z/2,Size.Z/2))
local C_Flower = Flower:Clone()
C_Flower.Parent = FlowerFolder
C_Flower:SetPrimaryPartCFrame(CFrame.new(FlowerX,FlowerY,FlowerZ))
end
while #FlowerFolder:GetChildren() < PlayerData.Storage do
wait(PlayerData.GrowthTime)
GenerateFlowers()
end
local regenDebounce = false
local function GenerateFlowers()
local Flower = FlowerAssets[chooseIndex(Rarity)]
local FlowerX = (FlowerBed.Position.X + math.random(-Size.X/2, Size.X/2))
local FlowerY = (FlowerBed.Position.Y * 4)
local FlowerZ = (FlowerBed.Position.Z + math.random(-Size.Z/2, Size.Z/2))
local C_Flower = Flower:Clone()
C_Flower:PivotTo(CFrame.new(FlowerX, FlowerY, FlowerZ))
C_Flower.Parent = FlowerFolder
end
local function RegenLoop()
while #FlowerFolder:GetChildren() < PlayerData.Storage do
regenDebounce = true
task.wait(PlayerData.GrowthTime)
GenerateFlowers()
regenDebounce = false
end
end
FlowerFolder.ChildRemoved:Connect(function()
if regenDebounce then
return
end
RegenLoop()
end)
RegenLoop()
I think you are trying to achieve this? The while loop would break once the statement would turn false. So I assume you want an ongoing generation, that only generates if the current items is less than the storage. If so, the script below should be fine!
local function GenerateFlowers()
local Flower = FlowerAssets[chooseIndex(Rarity)]
local FlowerX = (FlowerBed.Position.X + math.random(-Size.X/2,Size.X/2))
local FlowerY = (FlowerBed.Position.Y * 4)
local FlowerZ = (FlowerBed.Position.Z + math.random(-Size.Z/2,Size.Z/2))
local C_Flower = Flower:Clone()
C_Flower.Parent = FlowerFolder
C_Flower:SetPrimaryPartCFrame(CFrame.new(FlowerX,FlowerY,FlowerZ))
end
while task.wait(PlayerData.GrowthTime) do
if #FlowerFolder:GetChildren() < PlayerData.Storage then
GenerateFlowers()
end
end