Index nil with Clone

Hello, for the third time today.
Yet again, I have another issue, relating to coins. At the moment, I am trying to fix a script, that allows coins to spawn randomly within a certain place. When I test the game to test the script, I get an error.

  22:05:35.920  ServerScriptService.Generators.Coins.GeneratorC1:12: attempt to index nil with 'Clone'  -  Server - GeneratorC1:12
  22:05:35.920  Stack Begin  -  Studio
  22:05:35.920  Script 'ServerScriptService.Generators.Coins.GeneratorC1', Line 12 - function gen  -  Studio - GeneratorC1:12
  22:05:35.920  Script 'ServerScriptService.Generators.Coins.GeneratorC1', Line 21 

I’ve tried a couple things but I haven’t learned about indexing nil or cloning yet so that’s why I’m here. This is also a script my friend added in the game when we started a while ago.

Here is the script:

function gen()
	
	local SS = game:GetService("ServerStorage")

	local List = SS:WaitForChild("CoinsWRLD1")

	local Items = List:GetChildren()

	local MaxItems = 50 --Max amount of coins spawned

	local PickedItem = Items[math.random(1, MaxItems)]
	local part = PickedItem:Clone()
	part.Parent = workspace:FindFirstChild('SpawnedCoins1')

	local x = math.random(130,366) 
	local z = math.random(-156,162) 

	part.Position = Vector3.new(x,part.Position.Y,z)
	
end
gen()
while wait(5) do
	gen()
end

I feel guilty for this being my third post today, but every one is a learning experience for me.
Any help would be amazing!!!

I also did look for any other topics like this, but none seemed to help.

First to say: It’s not LocalScript. Right?

1 Like

No, it is not a LocalScript. (normal script in ServerScriptService) sorry, forgot to add that in

local MaxItems = math.min(50,#Items)

Or ignoring “local MaxItems” and

local PickedItem = Items[math.random(1, #Items)]
1 Like

The MaxItems is how many coins can be spawned at one time within that area.

Oh! the second local worked, but then wont they spawn infinitely?

Yes, but the issue is that that line is getting something from the folder in server storage. There is obviously not 50 items so its returning nil therefore giving that error. You should change it to what he said

local PickedItem = Items[math.random(1, #Items)]

Edit: The only thing making it 50 changes is the error so there is no point having 50?

1 Like

Yes, I understand it fully but without that they will infinitely spawn and there is no limit (as i just tested)

if #Items < MaxItems then

ohhh so i should keep the “Local MaxItems” in then?

Yeah, value found it’s purpose.

How would I write this? sorry for the bother…lol

Move this line into

if #Items < MaxItems then
--here
end

now only one coin spawned?

303

The code should be like this right? Just making sure I put it in the right place…

function gen()
	
	local SS = game:GetService("ServerStorage")

	local List = SS:WaitForChild("CoinsWRLD1")

	local Items = List:GetChildren()

	local MaxItems = math.min(50, #Items)
	
	if #Items < MaxItems then
		local PickedItem = Items[math.random(1, 50)]
		local part = PickedItem:Clone()
		part.Parent = workspace:FindFirstChild('SpawnedCoins1')

		local x = math.random(130,366) 
		local z = math.random(-156,162) 

		part.Position = Vector3.new(x,part.Position.Y,z)
	end
	
end
gen()
while wait(5) do
	gen()
end
local MaxItems = 50
local coinsSpawned = 0

function gen()
	
	local SS = game:GetService("ServerStorage")

	local List = SS:WaitForChild("CoinsWRLD1")

	local Items = List:GetChildren()
	
	if coinsSpawned < MaxItems then
		local PickedItem = Items[math.random(1, 50)]
		local part = PickedItem:Clone()
		part.Parent = workspace:FindFirstChild('SpawnedCoins1')

		local x = math.random(130,366) 
		local z = math.random(-156,162) 

		part.Position = Vector3.new(x,part.Position.Y,z)
        coinsSpawned += 1
	end
	
end
gen()
while wait(5) do
	gen()
end
local MaxItems = 50
local coinsSpawned = 0

function gen()
	
	local SS = game:GetService("ServerStorage")

	local List = SS:WaitForChild("CoinsWRLD1")

	local Items = List:GetChildren()
	
	if coinsSpawned < MaxItems then
		local PickedItem = Items[math.random(1, #Items)]--I didn't see inside of CoinsWRLD1 and SpawnedCoins1
		local part = PickedItem:Clone()
		part.Parent = workspace:FindFirstChild('SpawnedCoins1')

		local x = math.random(130,366) 
		local z = math.random(-156,162) 

		part.Position = Vector3.new(x,part.Position.Y,z)
        coinsSpawned += 1
	end
	
end
gen()
while wait(5) do
	gen()
end

hmmm… now it gives the index nil with clone message again. This is soo confusing.

Are you putting out the same coin or is there different coins?
Is the number of coins you want always going to 50?
Do you want them to all spawn at the same time or slowly?
Once 50 coins are out do you want 50 coins to be out all the time?

1 Like

Ok so based on how its placed in the game, It should be like this.

The max amount of coins that will be able to be spawned is 50, so if people aren’t collecting them, there will be 50. In ServerStorage there is a folder named CoinsWRLD1, with the coin inside of it. I am pretty sure it is supposed to clone that coin, then spawn it within the area. And I want them to spawn slowly.