Coin spawning issue

I don’t understand why this is happening. The script I duplicated was already working for silver coins. The gold coins spawn in a different location so I changed some coordinates and updated all the relevant details and I get this error.

I’ve tried switching the math.random coordinates around so the small coordinate is first (even though the original has the larger value first) to see if anything changes. I’ve told the game to WaitForChild(“GoldCoin”) in case the coin wasn’t spawned in when the game began but I’m not sure where the error lies. The silver coin script works perfectly and is almost exactly the same except for the coin and locations.

-- Gold Coin Spawner in ServerScriptService --

local goldCoin = game.ServerStorage:WaitForChild("GoldCoin")
local maxCoins = 300

local function loopOneBegin()
	while true do -- Need to replace with a more stable way
	local numGoldCoins = game.Workspace.CoinGoInHere.GoldCoins:GetChildren()
		
		if #numGoldCoins <= maxCoins then
			local clone = goldCoin:Clone()
			clone.Parent = workspace.CoinGoInHere.GoldCoins
			
			-- Checking which area to spawn into --
			local randomNumber = math.random(1,6)
				if randomNumber == 1 or 2 then
					clone.Position = Vector3.new(math.random(-323.5, -447), -4, math.random(34, 115))
				else if randomNumber == 3 then
					clone.Position = Vector3.new(math.random(-323.5, -363.5), -4, math.random(112, 160.5))
				else if randomNumber == 4 then
					clone.Position = Vector3.new(math.random(-405, -447), -4, math.random(111, 161.5))
				else
					clone.Position = Vector3.new(math.random(-323, -447), -4, math.random(157, 225))
				end
				end
				end
			
			task.wait(math.random(0.1,0.5)) -- time between spawning
		else
			if #numGoldCoins >= maxCoins then 
				--print("The number of Gold Coins is ", #numGoldCoins)
				task.wait(0.01)
			end
		end
	end
end

loopOneBegin()

The working script (That I copied from) is further down the comments!

1 Like

math.random() only works on positive, whole numbers. For example, I can’t use math.random with negative numbers such as -3, and in terms of decimals, it will only pick a whole number (ie. math.random(1.5, 2.8) will only ever give you 1 or 2.

To remedy this, I recommend taking all the negatives, and originating them as positive numbers, then preforming some math to get the desired negative number.

Also, for something like this

you could probably quickly remedy that by doing:

task.wait(math.random(1, 5) / 10) 

just to put you on the right track on what I mean by math operations.

Edit: Totally forgot to mention that it may also be easier to convert positives to negatives with the following:

local ExampleNumber = math.random(323, 447)
ExampleNumber = -ExampleNumber
1 Like

Yeah I thought that too but the silver coins respawn as I’m collecting them regardless. What I’m not understanding is why when I change the positions for the new coins to spawn (in exactly the same way as the working script for silver coins) it creates an error. I even renamed the function for the new script just in case running 2 concurrent functions with the same name caused issues.

– The works just fine script, compare this with the new one

-- Silver 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)
			end
		end
	end
end

loopBegin()
1 Like

Who told you that it only works on positive numbers?
image

image

Edit : If you want decimals, you can try

local r = Random.new() for i = 1, 5 do print(r:NextNumber()) end

in the console as it will return a decimal number between [0,1]

Edit 2 : Nevermind, if you just put math.random() without any interval, it will be a random decimal number from [0,1]

1 Like

When you call math.random() with 2 arguments the second argument must be the max and the first one must be the min.

-447 is less than -323.5
So you should do math.random(-447, -323.5) instead of math.random(-323.5, -447)

This is what causes invalid argument #2 to 'random' (interval is empty)

1 Like

Sorry, you’re 100% correct, I should of clarified more, it does in fact work on negative numbers and the wording I used to convey my point is incorrect.

What I was referring to is that it wont work on negative number in the typical sense OP was using it. (ie. math.random(-7, -10) ) won’t work because of the issue ketrab described with an invalid interval, as -10 is less than -7 because it’s negative, thus making no interval.

But yes, negative numbers clearly do work as shown in your screenshots.

Hopefully with all the information provided here, you should be able to tweak your script effectively now with both the Random.new() method and the information regarding the specific interval being empty.

1 Like

I had the same issue when the numbers were around the other way with the larger value first. I switched then to check.

Ty, that’s what was missing. I failed to consider “0” as being the point between both locations and that Arguement #2 (as roblox pointed out) . I thought it was because I rolled a 2 in the random numbers lol. Crank this up to learning the hard way!

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