Problem with a coin and debounce?

I made this script and I added 1 debounce + task.wait, but currently when I touch a coin sometimes it gives +1 but sometimes it gives +4 or +10 or even more…

Can you help me to create an optimized script to avoid this problem please?

local CollectionService = game:GetService("CollectionService")

local Tagged = CollectionService:GetTagged("Coin")

local debounce = false

for _, taggedCoin in pairs(Tagged) do
	taggedCoin.Touched:Connect(function(hit)
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr then
			debounce = true
			plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 1
			taggedCoin.Parent = game.ServerStorage
			task.wait(3)
			debounce = false
			taggedCoin.Parent = game.Workspace.Game.Coins
		end
	end)
end
1 Like

Let’s analyse your code for a second.

You have declared a debounce variable, and even set it within the touched function, but you never actually implement it. This is the reason why the coins keep giving you a differing amount each time they are touched.

Secondly, it seems as though you have a singular debounce to account for all the coins, instead of each coin having their own. Let’s fix this up using attributes, because attributes are cool. :grin:

Let’s fix up your code a bit.

--// [ Variables ]

local CollectionService = game:GetService("CollectionService")
local Tagged = CollectionService:GetTagged("Coin")


--// [ Functions ]

--Function for us to check the state of the coin
local function checkState(obj)
	return obj:GetAttribute("Debounce") or false
end

--Function for us to set the state of the coin
local function setState(obj, val)
	obj:SetAttribute("Debounce", val)
end


--// [ Script ]
for _, taggedCoin in pairs(Tagged) do
	taggedCoin.Touched:Connect(function(hit)
		local plr = game.Players:GetPlayerFromCharacter(hit:FindFirstAncestorOfClass("Model"))
		
		if checkState(taggedCoin) == false and plr then
			setState(taggedCoin, true)
			
			plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 1
			
			taggedCoin.Parent = game.ServerStorage
			
			task.wait(3)
			
			setState(taggedCoin, false)
			
			taggedCoin.Parent = workspace.Game.Coins
		end
	end)
end

I changed up a few things to make it look cleaner too. Give this a try.

1 Like

So, wow this is excellent.

Now can you explain to me what this does (I really need to understand your logic, because it’s excellent…)

I have difficulties to understand :

  • checkState(obj)
  • setState(obj, val)
  • if checkState(taggedCoin) == false and plr then
    setState(taggedCoin, true)

I’m a beginner and I would really like to learn how to make scripts like that, 98% coins tutorials on youtube are very very bad…

Hoping I can make this understandable :sweat_smile:

A function is something that, when called, will run the code within itself.

print("Hello world")

--Will have the same effect as

local function something()

print("Hello world")

end

something() --calls the function and runs the code within.



--You can also pass things through aswell.

local function something(whatToPrint) --have descriptive words for your args.

print(whatToPrint) --prints 'Fabien is cool!'

end

something("Fabien is cool!")

checkState is a function I created that, when called, returns the state of a given objects “Debounce” attribute. This is the function that is used to check whether we can collect the coins or not. You don’t need a separate function just to do this, but it’s just personal preference.

setState is a function that is created so that we can set said Debounce state of an object. You don’t actually need to create a separate function for this, but I do it as a personal preference. The alternative would be typing

obj:SetAttribute("Debounce", true)
task.wait(3)
obj:SetAttribute("Debounce", false)

It just makes it look cleaner.

When calling ‘checkState(taggedCoin)’, the function returns the current state of the tagged coins debounce (true, false, etc). If you were to declare a variable and call that function as its value, the variable would be set to whatever it returns.

local currentState = checkState(taggedCoin)
--If the debounce is true, currentState will have its value set to true, etc.

The if statement runs the code within itself if the current debounce state [ true or false ] == false.

1 Like

Yes i just need learn ^^ tysm for your time and your help <3

1 Like

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