Very bugged coin system, 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

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

----- | 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

1 Like

Have you printed in certain parts of your code to make sure it ran correctly? Did any error show up during the 25%?

I was just thinking the same thing about printing. :grinning:

I added a bunch of print statements to your script:

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


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


----- | Method | -----
function onTouch(part)
	print("Touched")
	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
				else
					warn("No Leaderstat")
				end
			else
				warn("No Player")
			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
		else
			warn("No Human")
		end
	else
		warn("Wait")
	end
end


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

Nothing printed. I think its probably the Callback
image
The coin itself is a Mesh, is that a problem?

Did you paste the code above with all the print message added?

I made my own

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


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


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

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

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

				if leaderStat then
					print('leader')
					leaderStat.Value += Amount
				end
			end
			print('extrastuff')
			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
			print('works')
		end
	end
end


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

Add one here:

function onTouch(part)
print(“Touched”)

image

If it doesn’t print anything at all then check that your part is set to CanTouch and that it is large enough.

I have noticed that small items can be glitchy.

You can add a larger part to your coin. Like at least 3 X 3 X 3 Studs.

Make it invisible and set CanCollide to false. You just want it for the CanTouch ability.

Your debounce is only setting back again if the coin touched a human. If it touched anyth else before, it will get locked with no way to pass debounce again.

So how do i fix it without the player getting like 15000 coins

Move the debounce to be after it checks for a humanoid.

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


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


----- | Method | -----
function onTouch(part)
print('touched')
		print("bounced")
		local Human = part.Parent:FindFirstChild("Humanoid")

		if Human then
			print("human")
		local Player = Players:GetPlayerFromCharacter(Human.Parent)
		if not Debounce then Debounce = true

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

				if leaderStat then
					print('leader')
					leaderStat.Value += Amount
				end
			end
			print('extrastuff')
			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
			print('works')
		end
	end
end


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

use a time based debounce like anyone else lol

local Debounce = false
function onTouch(part)
    if Debounce then return end
    Debounce = true
    task.delay(1, function() -- 1 second
        Debounce = false
    end)

    -- code code code

end

how does that work
it has a 1 second delay before debounce is released

You need to make sure there is a humanoid before assigning it to a variable.

Also, it is useful to build warnings into your code instead of print statements.

That way they only show up in the Output window when there is a problem.

Instead of blowing up your Output window with print statements.

Try this:

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


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


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

			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
					else
						warn("No Leaderstat")
					end
				else
					warn("No Player")
				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	

			else
				warn("No Human")
			end
		else
			warn("Wait")
		end
		Debounce = false
	end
end


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

Ah, my bad, i havent even read the rest of the code to understand the context.

Sinse the coin is disapearing and reapearing after task.wait, you can set the debounce false and true there too.

Remove the Debounce setting true from the beginning and put it together with the part where the coin goes transparent, and set back at the part the transparency is retored.

Wdym “Put it together” I literally can’t visualize that

That script managed to update my Leaderstats by 1000, Sure the coin thingy works but its just the leaderstats thats left

Add a wait(10)

or how ever long you want to wait just before the Debounce = false.

wait(8)
Debounce = false