Coin system is broken

My coin collection system is broken… It uses a meshpart containing a hitbox to make the coin.

Here’s the script:

local player = game:GetService("Players")
local coin = script.Parent -- Path to coin

local used = false

function giveCoin(part)
	if part.Parent:FindFirstChild("Humanoid") == nil or used == true then
		return
	end
	used = true
	local player = player:GetPlayerFromCharacter(part.Parent)
	player.leaderstats.Candy.Value = player.leaderstats.Candy.Value + 5

	coin.Parent:Destroy()
end

coin.Touched:Connect(giveCoin)
1 Like

What do you mean “it’s broken” can you clarify?

It just does nothing… should i use prints to find the source?

1 Like

I figure that the code is probably returning at this point - gimme a sec

1 Like

I tested it myself - and it appears to work, I’m not really sure what is wrong here.

:confused: that’s weird… idk what is wrong with mine

I think that it has something to do with your hitbox, is it bigger than the meshpart, can-collide off/on?

Wrong method.
It should be:

local players = game:GetService("Players")

and:

local player = players:GetPlayerFromCharacter(part.Parent)

Your variables conflict with each other.

its bigger and they both has cancollide off

Still doesn’t work :confused: any other ideas?

Is CanTouch enabled for the hitbox?

Do not paste a script in each coin!
You can easily use collection service and tag to just have 1 script
Put this in server script service:

local collectionService = game:GetService("CollectionService")
local player = game:GetService("Players")
local tag = "coin"

local function newCoin(coin)
	coin.Touched:Connect(function(hit)
		local player = player:GetPlayerFromCharacter(hit.Parent)
		if not player then return end
		
		coin.Parent:Destroy()

		player.leaderstats.Candy.Value += 5
	end)
end

collectionService:GetInstanceAddedSignal(tag):Connect(newCoin)
for _,v in collectionService:GetTagged(tag) do
	newCoin(v)
end

now just give each coin the “coin” tag
image

ok I think my game is bugged this doesn’t work either :confused:

Try turning CanCollide on if it isn’t anchored otherwise I think it’s just falling through the ground.

Lol I am so dumb… ofc I forgot to anchor it. Ok so now it does get to the destroy part, but I am not awarded any Candy. Keep in mind I am now using @Hzodx script so it’s different to the original post.

local collectionService = game:GetService("CollectionService")
local player = game:GetService("Players")
local tag = "coin"

local function newCoin(coin)
	coin.Touched:Connect(function(hit)
		local player = player:GetPlayerFromCharacter(hit.Parent)
		if not player then return end

		coin.Parent:Destroy()

		player.leaderstats.Candy.Value += 5
	end)
end

collectionService:GetInstanceAddedSignal(tag):Connect(newCoin)
for _,v in collectionService:GetTagged(tag) do
	newCoin(v)
end

You’re not getting any candy because you destroy the coin before you award the candy - swap those 2 lines and the code will function!

you defined player twice its might be conflicting and causing overwriting

more likely that he didn’t tag the coins and they dont work at all
the code is no longer meant to be inside of the coins so destroying it earlier shouldn’t change anything

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.