Coin Spawn Generator Not Working

Before I explain the problem im going to tell you what I have working.

What is working:

  • Random Coin Spawn Between Two Parts
  • Repeats After Certain Amount Of Coins Have Been Collected

What is not working:

  • When I Touch The Coin I Want It To Add To The Players Current Coin Amount, But It Gives Me An Error.

Error:

What can I do to fix this.

Code:

local p1 = game.Workspace.HeightCoinSpawn.HCA
local p2 = game.Workspace.HeightCoinSpawn.HCB
local hc = game.ReplicatedStorage.HeightCoinMod
local maxCoinCollect = 0
local repstorage = game:GetService("ReplicatedStorage")

local function spawnCoins()
	for i = 1, 100 do
		wait(0.01)
		local mathX = math.random(math.min(p1.Position.X, p2.Position.X), math.max(p1.Position.X, p2.Position.X))
		local mathZ = math.random(math.min(p1.Position.Z, p2.Position.Z), math.max(p1.Position.Z, p2.Position.Z))
		local newCoin = hc:Clone()
		newCoin.Parent = workspace.HeightCoinSpawn
		newCoin.Position = Vector3.new(mathX, 0, mathZ)
		newCoin.Touched:Connect(function(player)
			if player then
				maxCoinCollect = maxCoinCollect +1
				player.leaderstats.HeightCoin.Value += 10
				newCoin:Destroy()

				print(maxCoinCollect)

				if maxCoinCollect >= 10 then
					print("Passed")
					for i, v in pairs(game.Workspace.HeightCoinSpawn:GetChildren()) do
						if v.Name == "HeightCoinMod" then
							v:Destroy()
						end
					end
					maxCoinCollect = 0
					spawnCoins()
				end
			end
		end)
	end
end

spawnCoins()

Touched doesn’t return the player that touched the part, it returns what Part touched it. You have to get the parent of that part and get the player via Players:GetPlayerFromCharacter(character), where character is the part’s parent