Creating and maintaining 50 instances

I’m new at scripting and I’m trying to write this code as simply as possible to understand the process and I’m not particularly worried about the efficiency of the script. I’ve been working all day trying to create and then maintain 50 randomly located coins on a baseplate.

I’ve tried multiple loop methods: while true do, repeat until etc. The farthest I made it was actually creating 50 coins but not being able to instance more as the coins were picked up. I think that was with a specific while do true loop but I’ve tried so many variants in code I don’t even remember what the closest successful script was. I even tried using global variables to modify my coin count on touch from the part’s getCoin script when my count was stopping at 50 but it didn’t work.

Sorry this is super noob, after copying and learning aspects of coding for a few weeks this is my first attempt to actually code from nothing, I’ve been searching for spawning fixed numbers of instances but nothing is coming in as simple of terms that I’m looking for. Thanks for any information!

This current code is actually only spawning 10 coins with randomly increasing “Print Count” and it thinks 10 coins is 50. If I remove the getCount () from the loop it continues producing coins forever which is the problem in most of the code sequences I’ve tried.

local players = game:GetService("Players")
function playerSpawned(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local gold = Instance.new("IntValue", leaderstats)
	gold.Name = "Gold"
	gold.Value = 0
end

players.PlayerAdded:Connect(playerSpawned)

local coin = game.ServerStorage.Coin 
local count = 0

local function getCount()
	for _, v in pairs(workspace.Coins:GetChildren()) do
	count=count+1
	end
end

local function makeCoin()
	local clone = coin:Clone()
	clone.Parent = game.Workspace.Coins
	clone.Position = Vector3.new(math.random(-226, 234), 2.834, math.random(-230, 231))
	print(count)
end
	
if count<50 then repeat 
		makeCoin()
		getCount() 
		wait()
		until count >=50
end
1 Like

When you are counting the coins are you resetting the coin count? Because getCount will count all coins, however it will accumulate the coin coin to the last one it counted

1 Like

I added a reset into my coinCount function and it still counts indefinitely. That may have been the problem in one of the code sequences I tried earlier.

1 Like

Also having the getCount function is redundant, you can get the number of children by doing “#workspace.Coins:GetChildren()”

2 Likes
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Value = 0
	gold.Parent = leaderstats
end)


local coin = game.ServerStorage.Coin 
local count = Instance.new("IntValue")


local function makeCoin()
	local clone = coin:Clone()
	clone.Position = Vector3.new(math.random(-226, 234), 2.834, math.random(-230, 231))
	clone.Parent = workspace.Coins
	count.Value += 1
	local connection
	connection = clone:GetPropertyChangedSignal("Parent"):Connect(function()
		connection:Disconnect()
		count.Value -= 1
		if count.Value < 50 then
			makeCoin()
		end
	end)
end


for i = 1, 50 do
	makeCoin()
	wait()
end

Coins.rbxl (22.2 KB)

2 Likes

Thanks, it liked that line a lot more than it liked my getCount function. I had tried that earlier but I think I may have messed the syntax up.

1 Like

Thank you, I was thinking making a connection was going to be the next step in efficient production. You just solved my next few problems! :smiley:

1 Like