Coin Spawning System

*Post edited for clarity.
So, I have been working on a fixed location, coin spawn system, while learning to use tables. Please keep in mind that I’m new to scripting in general.

I'm writing this for two reasons.

The first reason, is that I would like help in how to make this system the best that it can be. I want to learn and understand What/How I can do things better in the future.

The second is that I don’t know any developers, in real life or otherwise. So I would like to build some relationships with other people, to build a network of support, encouragement, etc.

With that said:

  1. What do you want to achieve? Keep it simple and clear!
    ** What I wanted was a way to have a set number of coins spawn into a fixed place to start, and when a coin gets collected, a new coin randomly spawns at a new fixed location.**
The server script:
local module = require(script.ModuleScript)
local specialCoinsModule = require(script.SpecialCoin)
specialCoinsModule.setCoins()
module.setCoins()

debounce = true

while wait() do
	
	--NORMAL COIN COLLECTION--
	for i, v in pairs(game.Workspace.Spawners:GetDescendants()) do
		if v.Name == "Coin" then
			v.Touched:Connect(function(part)
				if not debounce then
					return
				end
				debounce = false
				local player = game.Players:GetPlayerFromCharacter(part.Parent)
				v:Destroy()
				debounce = true
				module.CollectCoin(player,v)
			end)
		end
	end
	
	--SPECIAL COIN COLLECTION--
	for i, v in pairs(game.Workspace.SpecialCoinSpawners:GetDescendants()) do
		if v.Name == "Coin" then
			v.Touched:Connect(function(part)
				if not debounce then
					return
				end
				debounce = false
				local player = game.Players:GetPlayerFromCharacter(part.Parent)
				v:Destroy()
				debounce = true
				specialCoinsModule.CollectCoin(player,v)
			end)
		end
	end
end
The module scripts
local module = {}

--Services--
    local ServerStorage = game:GetService("ServerStorage")
--Variables--
    local coin = ServerStorage.Coins.Coin
    local spawners = game.Workspace.Spawners
    local spawnersTable = {}
    local coinsSetTable = {}
--Functions--
  function module.setCoins()
	local needed_Coins = 10
	for i = 1, needed_Coins - #coinsSetTable, 1 do
		for i, spawner in pairs(spawners:GetChildren()) do
			if not spawner:FindFirstChild("Coin")then
				table.insert(spawnersTable, spawner)
			end
		end

    local randomSpawner = math.random(1,#spawnersTable) -- Get random number with 1 to length of table.
	local picked_value = spawnersTable[randomSpawner] -- Pick value from table
	local newCoin = coin:Clone()
		newCoin.Position = picked_value.Position
		newCoin.Name = "Coin"
		newCoin.Parent = picked_value

		table.remove(spawnersTable, randomSpawner)
		table.insert(coinsSetTable, picked_value)

		for k in pairs(spawnersTable)do
			spawnersTable[k] = nil
		end
	end
end
--Coin Touched from Script--
        function module.CollectCoin(player, v)
        	local randValue = math.random(2,10)
        	player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + randValue
        	table.remove(coinsSetTable, coinsSetTable[v.parent])
        	wait(1)
        	module.setCoins()
        end

        return module
Basics

It uses one coin in server storage and multiple spawn locations in the workspace.

It has a gold coin as a basic coin, and a blue one to signify a special coin. Each coin gives a random amount of cash when collected.

Feedback Needed

It all works, but it rechecks every coin spawn location, every time a coin is collected. Which means if I put a wait in there it just means 4-5 coins can be collected, before they all respawn at the same time, which is also okay because they are spread out. I’m not sure how this will work when 50 people are getting coins all at the same time. I’m sure there’s a better way to do all of this. I just don’t know what it is.

If you would be willing to check it out and provide some feedback, I would greatly appreciate it.

9 Likes

It’s a good system, a great start! Here is what I think you should add:

Maybe have the coins spin and hover going up and down, and add a little more detail to them.

Maybe you could also have them respawn, I’m not sure if they do but they didn’t when I was there.

But overall, it is a perfect start to scripting! Good job.

3 Likes

Thank you for the feedback. I fixed the respawn of the blue coins, and made them spin. I’ll work on the up and down movement. Thanks again for checking it out.

1 Like