Respawnable coin problem

I’m looking for a more stable way to generate these coins other than while true do. My son tried it out in Roblox and it seems to work for a while and then stops spawning coins although today I couldn’t recreate the issue. Is it locked up or glitching? I can’t seem to find out as there is no errors. I tried using RunService.Heartbeat instead and it creates an error.

-- Coin Spawner in ServerScriptService --

local silverCoin = game.ServerStorage.SilverCoin
local maxCoins = 200

local function loopBegin()
	while true do -- Need to replace with a more stable way
	local numSilverCoins = game.Workspace.CoinGoInHere.SilverCoins:GetChildren()
		
		if #numSilverCoins <= maxCoins then
			local clone = silverCoin:Clone()
			clone.Parent = workspace.CoinGoInHere.SilverCoins
			
			-- Checking which area to spawn into --
			local randomNumber = math.random(1,3)
			if randomNumber == 1 then
				clone.Position = Vector3.new(math.random(-457, -413.4), -4, math.random(-74.6, 25))
			else if randomNumber == 2 then
				clone.Position = Vector3.new(math.random(-413.4, -360), -4, math.random(-31.8, 25))
			else
				clone.Position = Vector3.new(math.random(-360, -317), -4, math.random(-74.6, 25))
			end
		end
			task.wait(math.random(0.1,0.5)) -- time between spawning
		else
			if #numSilverCoins >= maxCoins then 
				print("The number of Silver Coins is ", #numSilverCoins)
				task.wait(0.01)
			loopBegin()
			end
		end
	end
end

loopBegin()

I think the problem might occur with you recursively calling the function loopBegin function inside itself. What this is doing is every time your game reaches 200 coins it starts a new while loop which again will start another while loop and so on.

All code is only as stable as you make it, while true should work fine as long as you dont give it a reason to error.

1 Like

So it’s starting a new loop rather than restarting the loop? How would I go about making it just recheck the conditions (as players will gather coins while it’s running).

Adding onto @Cens_r a while loop will keep looping as long as the condition is true so you wouldn’t need to call the function again when it reaches the maximum amount of coins.

1 Like

The while loop is already doing that. I think you’re confused because you might not understand what “while true do” is actually doing.

Normally you’d have an check, to determine how long the while loop should run for. But since you have true in its place the while loop will constantly loop whatever’s inside it forever.

1 Like

Thx all. I really wasn’t sure why that function call was there as I was doing multiple tasks yesterday with coins and rebirths and I put this on the backburner since I was also having computer problems.

1 Like

I rewrote what you were trying to accomplish with Heartbeat so it won’t keep spawning until the random delay timer is reached. I also renamed some things to be more readable but it’s all preference. This should work for your project. A while loop will not exit until it’s conditions fail or until you call the loop control statement called ‘break’. If you need any help understanding anything feel free to DM me.

local RunService = game:GetService("RunService")

local originalSilverCoin = game.ServerStorage.SilverCoin
local lastTimeSinceSpawnedCoin = 0

local MAX_COINS = 200

function spawnSilverCoinInRandomPosition()
	local silverCoin = originalSilverCoin:Clone()
	silverCoin.Parent = workspace.CoinGoInHere.SilverCoins	
	
	local randomNumber = math.random(1,3)
	local randomX, randomZ = nil, nil
	local COIN_Y_POS = 13
	if randomNumber == 1 then
		randomX = math.random(-50, 50)
		randomZ = math.random(-50, 50)
	elseif randomNumber == 2 then
		randomX = math.random(0, 50)
		randomZ = math.random(0, 50)
	else
		randomX = math.random(-25, 75)
		randomZ = math.random(-25, 75)
	end
	silverCoin:MoveTo(Vector3.new(randomX, COIN_Y_POS, randomZ))
	lastTimeSinceSpawnedCoin = tick()
end

function maxSilverCoinMessage(amountOfSilverCoins)
	print("The number of Silver Coins is ", amountOfSilverCoins)
end

RunService.Heartbeat:Connect(function(step)
	local numSilverCoins = game.Workspace.CoinGoInHere.SilverCoins:GetChildren()
	local randomWaitTime = (math.random(0.1, 5) / 10)

	if #numSilverCoins < MAX_COINS and (tick() - lastTimeSinceSpawnedCoin) > randomWaitTime then
		spawnSilverCoinInRandomPosition()
	end
	if #numSilverCoins >= MAX_COINS then 
		maxSilverCoinMessage(#numSilverCoins)
	end
end)

So I’m assuming the randomX, randomZ correspond to the area in which the coin spawns? I had to do 3 areas as the coins are spawning around a fountain in the way.

also it says in the output…
12:55:46.141 MoveTo is not a valid member of MeshPart “Workspace.CoinGoInHere.SilverCoins.SilverCoin” - Server - TestScript:25
12:55:46.141 Stack Begin - Studio
12:55:46.141 Script ‘ServerScriptService.TestScript’, Line 25 - function spawnSilverCoinInRandomPosition - Studio - TestScript:25
12:55:46.141 Script ‘ServerScriptService.TestScript’, Line 38 - Studio - TestScript:38
12:55:46.141 Stack End - Studio

,

RunService.Heartbeat:Connect(function(step)
	local numSilverCoins = game.Workspace.CoinGoInHere.SilverCoins:GetChildren()
	local randomWaitTime = (math.random(0.1, 5) / 10)

	if #numSilverCoins < MAX_COINS and (tick() - lastTimeSinceSpawnedCoin) > randomWaitTime then
		spawnSilverCoinInRandomPosition() --Seems to error here!
	end
	if #numSilverCoins >= MAX_COINS then 
		maxSilverCoinMessage(#numSilverCoins)
	end
end)

,

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.