Coins Tables Bug

What’s the problem?
Hi there! I made a coins system that sets every coin when you collect it in a table with the players userid and the coins position. It looks like this:

-- Services

local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Variables

local remoteEvents = ReplicatedStorage.RemoteEvents

local coinsTable = {}

-- Core Script

for _, coinsZone in pairs(game.Workspace.CoinsAreas:GetChildren()) do
	
	for _, coin in pairs(coinsZone:GetChildren()) do
		
		coin.Touched:Connect(function(hit)
			
			if hit.Parent:FindFirstChild("Humanoid") then
				
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				
				if not table.find(coinsTable , coin.Position, player.UserId) then
						
					print("Coin has been touched by "..player.Name)
					
					table.insert(coinsTable, player.UserId, coin.Position)
					
					remoteEvents.CoinsTouched:FireClient(player, coin)
					
				end
				
			end
			
		end)
	
	end
	
end

It works perfectly fine with 1 coin, but if I insert 2 or more coins, this happens when I collect the first coin you can’t collect it, but if you collect the second coin you can pick up the first coin again, without that I removed the coin from the table.

(Sorry if I do something wrong at adding things to tables, I didn’t used tables much before.)

Please let me know if you know how to fix this, or just have a whole better way of inserting a coin with a player in a table. Thanks for reading! :slightly_smiling_face:

1 Like

Unfortunately, I’ve noticed that table.find() doesn’t work with dictionaries. It has to be index, value pairs.

There is a better idea then using positions. You can assign each coin an “id” starting from 1 to the number to coins. Of course, each coin will have a different id, so it will be easy to distinguish them.

You can accomplish this by inserting an IntValue inside of each of the coins (via a script of course) and assign them an id that is 1 more than the previous one.

Here’s the system I’m talking about:

local players = {

	"144991842" = {32, 89, 731} --the coin ids

}

print(table.find(players[player.UserId], 89)) --2

--add new coin:
table.insert(players.UserId, coinId)

--create a new key (userId)
players[player.UserId] = {coinId} --it's created, now you can use table.insert()

I think you get what I’m saying right?

1 Like

I kinda get it, but I don’t really know how to do this now.