I have a script where it generates random stuff and for some reason it creates 4 obstacles instead of the 1 that its supposed to create

Code:

local worldFolder = game.Workspace.ZaWarudo
local modelsFolder = game.ReplicatedStorage:WaitForChild("Models")

local randomModel
local randomName

local hasCheckedObs = false
local hasCheckedBase = false
local Debounce = true


for _, child in ipairs(worldFolder.Placements:GetChildren()) do
	child.Transparency = 1
end

local function checkBases()
	if Debounce then
		Debounce = false
		for _, child in ipairs(worldFolder.Placements:GetChildren()) do
			if child.Name == "BigBasePlacement" then
				local models = modelsFolder:WaitForChild("Platforms"):WaitForChild("BigBase"):GetChildren()
				n = math.random(1, #models)
				randomModel = models[n]
				theCFrame = child.CFrame
			else if child.Name == "MediumBasePlacement" then
					local models = modelsFolder:WaitForChild("Platforms"):WaitForChild("MediumBase"):GetChildren()
					n = math.random(1, #models)
					randomModel = models[n]
					theCFrame = child.CFrame
				else if child.Name == "CheckpointPlacement" then
						local models = modelsFolder:WaitForChild("Platforms"):WaitForChild("Checkpoint"):GetChildren()
						n = math.random(1, #models)
						randomModel = models[n]
						theCFrame = child.CFrame
					else 
						print("No bases to place")
					end
				end
			end

			if not randomModel == nil then
				print("checkBases " .. randomName)
				print("checkBases " .. randomModel)
			end

			newModel = randomModel:Clone()
			newModel.Parent = worldFolder.Models
		end
	end
	Debounce = true
end

local function checkObstacles()
	if Debounce then
		Debounce = false
		for _, child in ipairs(worldFolder.Placements:GetChildren()) do
			if child.Name == "ObstacleBase1x1Placement" then
				local models = modelsFolder:WaitForChild("Obstacles"):WaitForChild("Obstacle1x1"):GetChildren()
				n = math.random(1, #models)
				randomModel = models[n]
				theCFrame = child.CFrame
			else if child.Name == "ObstacleBase2x1Placement" then
					local models = modelsFolder:WaitForChild("Obstacles"):WaitForChild("Obstacle2x1"):GetChildren()
					n = math.random(1, #models)
					randomModel = models[n]
					theCFrame = child.CFrame
				else if child.Name == "ObstacleBase3x2Placement" then
						local models = modelsFolder:WaitForChild("Obstacles"):WaitForChild("Obstacle3x2"):GetChildren()
						n = math.random(1, #models)
						randomModel = models[n]
						theCFrame = child.CFrame
					else 
						print("No obstacles to place")
					end
				end
			end

			if not randomModel == nil then
				print("checkBases " .. randomName)
				print("checkBases " .. randomModel)
			end

			newModel = randomModel:Clone()
			newModel.Parent = worldFolder.Models
		end
	end
	Debounce = true
end






while true do
	checkObstacles()
	checkBases()
	wait(5)
	worldFolder.Models:ClearAllChildren()
	wait(1)
end

2 comments

0

To be specific it only does 4 of each obstacles the bases / platforms work fine

0

Try to add a wait before setting the debounce to true again.