How to respawn the coins and chests

Hello, Everybody I have this script that spawns coins, bags, and chests. Is there a way to respawn them like if there no more coins left I spawn the coins again

local StarterZoneCurrency = script.Parent

--(-222,58,-28)
game.Players.PlayerAdded:Connect(function()


	local StarterZoneCurrencyItems = {
		StarterZoneCurrency["Bag 1"],
		StarterZoneCurrency["Bag2"],
		StarterZoneCurrency["Coin"],
		StarterZoneCurrency["Gem"],
		StarterZoneCurrency["Chest"]

	}




	for i,v in pairs(StarterZoneCurrencyItems) do
		--print(i,v)
		if i == 1 then
			local Count = 1


			while true do
				wait()
				local  RandomPosX = math.random(-294,-113)
				local RandomPosY = math.random(8,8)
				local RandomposZ = math.random(-88,40)

				local Position = CFrame.new(RandomPosX ,RandomPosY,RandomposZ)

				Count = Count + 1


				wait(0.2)
				local CloneBag1 = v:Clone()
				CloneBag1.Parent = game.Workspace
				CloneBag1:PivotTo(Position)

				local x = 20
				if Count > x then

					break
				end
			end

			i = 2
			if i == 2 then
				local Count2 = 1


				while true do
					wait()
					local  RandomPosX2 = math.random(-602,-422)
					local RandomPosY2 = math.random(8.6,8.6)
					local RandomposZ2 = math.random(-88,40)

					local Position2 = CFrame.new(RandomPosX2 ,RandomPosY2,RandomposZ2)

					Count2 = Count2 + 1


					wait(0.2)
					local CloneBag2 = game.Workspace.Currencies.StarterZoneCurrency.Bag2:Clone()
					CloneBag2.Parent = game.Workspace
					CloneBag2:PivotTo(Position2)
					local x2 = 5 
					if Count2 > x2 then
						break
					end

				end
				i = 3
				if i == 3 then
					local Count3 = 1


					while true do
						wait()
						local  RandomPosX3 = math.random(-602,-455)

						local RandomposZ3 = math.random(-88,40)

						local Position3 = CFrame.new(RandomPosX3 ,6.3,RandomposZ3)

						Count3 = Count3 + 1


						wait(0.2)
						local Coin = game.Workspace.Currencies.StarterZoneCurrency.Coin:Clone()
						Coin.Parent = game.Workspace
						Coin:PivotTo(Position3)
						local x3 = 25
						if Count3 > x3 then
							break
						end


					end
				end
				i = 4 
				if i == 4 then 
					local Count4 = 1 
					while true do
						wait()
						local RandomposX4 = math.random(-602,-455)
						local RandomposZ4 = math.random(-88,40)

						local Position4 = CFrame.new(RandomposX4,7,RandomposZ4)

						Count4 = Count4 + 1 

						wait(0.2)
						local Gem = game.Workspace.Currencies.StarterZoneCurrency.Gem:Clone()
						Gem.Parent = game.Workspace
						Gem:PivotTo(Position4)
						local x4 = 10
						if Count4 > x4 then
							break
						end
					end

				end

				i = 5

				if i == 5 then
					local Count5 = 1
					while true do
						wait()
						local RandomposX5 = math.random(-580,-453)
						local RandomposZ5 = math.random(-393,-260)

						local Position5 = CFrame.new(RandomposX5,13.8,RandomposZ5)	

						Count5 = Count5 + 1

						wait(0.2)
						local ChestClone = game.Workspace.Currencies.StarterZoneCurrency.Chest:Clone()
						ChestClone.Parent = game.Workspace
						ChestClone:PivotTo(Position5)
						local x5 = 3
						if Count5 > x5 then
							break
						end
					end
				end



			end
		end


	end



end)

You can do your func or event when the coin gets collected or whatever.
that is if you want to do it that way,

of course if it was maybe a folder where you for example kept them you could use the function where childremoved

Or you can do as you kind of started that it keeps count of the amount of them and if there are not enough to your liking maybe then add more?

basically just spawn more when any of these happen ( however it is to your liking)

1 Like