Very buggy coin system, need help

I got a coin system thats super bugged out, it only works 75% of the time and i need HELP
I cant find whats wrong here

----- | Services | -----
local Players = game:GetService("Players")


----- | Variables | -----
WaitTime = 300
Amount = 75
Debounce = false


----- | Method | -----
function onTouch(part)
	if not Debounce then Debounce = true
		local Human = part.Parent:FindFirstChild("Humanoid")

		if Human then
			local Player = Players:GetPlayerFromCharacter(Human.Parent)

			if Player then
				local leaderStat = Player:FindFirstChild("leaderstats") :FindFirstChild("EggCoins")

				if leaderStat then
					leaderStat.Value += Amount
				end
			end

			script.Parent.Music:Play()
			script.Parent.Day.Enabled = true
			script.Parent.Transparency = 1

			task.wait(1.25)
			script.Parent.Day.Enabled = false
			script.Disabled = true

			task.wait(WaitTime)
			script.Parent.Transparency = 0
			script.Disabled = false	

			Debounce = false
		end
	end
end


----- | Callback | -----
script.Parent.Touched:Connect(onTouch)

image

What is the problem you are experiencing? Are the coins not being picked up?
I would not recommend using one scro[t per coin either. It looks like that is what you are doing.

Basically when the player touches the coin confetti comes up with a sound and updates the leaderstat value, Problem is 25% of the time the coin script doesn’t work and when i touch it nothing updates orhappens

Maybe it’s because the scripting has errors, causing the coins not to be picked up, but I recommend fixing some errors by finding errors in the script, that caused it not to be collected or picked up.

There really isnt any errors in the output, I tried some ways by tidying the script but theres nothing I can see

1 Like

.Touched will not always work, which is why people recommend you raycast to collect the coins.

Also, you can try creating a hitbox, those usually help.

1 Like

I agree, since we have to raycast to collect the coins to fix the bug, so that’s why .Touched doesn’t work as always, I agree with the idea of creating a hitbox, and that’s why it might help fix the bug as a resolution.

How do you make a hitbox or raycast

A Hitbox is very simple. Just create a Part you want the Touched Event to be fired in, and have it surround the entire coin, Alternatively, a Region3 would work too.

There are plenty of tutorials that show you how to raycast.

I agree with the recommendation that Cairo suggested you.
There are indeed plenty of tutorials that can show you how you can raycast.

Putting in .Touched won’t work, unless you put it in a hitbox.

image
can a mesh be a hitbox?

Creating a raycast for every player going down checking if they touched a coin is a yikes performance wise.

Raycasts are very efficient, and if you are experiencing lag from it, you are likely not handling it correctly.

I still dont know how to raychast

1 Like