Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make my "Touch For Cash" Not spam cash?

I am currently making a new game, and I am making a obby that can get you coins, but my script keeps on spam giving me cash! What did I do wrong?

amnt = 500 
function onTouched(part)
	local h = part.Parent:findFirstChild("Humanoid")
	if (h~=nil) then
		local thisplr = game.Players:findFirstChild(h.Parent.Name)
		if (thisplr~=nil) then
			local stats = thisplr:findFirstChild("leaderstats")
			if (stats~=nil) then
				local coins = stats:findFirstChild("Coins")
				if (coins~=nil) then
					coins.Value = coins.Value + amnt
				end
			end
		end
	end
end

script.Parent.Touched:connect(onTouched)

Please help me. Thanks!

2 answers

As the player walks over the part, their limbs are constantly leaving and touching the object. What you could do is a add a debounce so that it won't register that player's hit's until a certain amount of time passed.

local Cooldown = {}
Object.Touched:Connect(function(Obj)
	if Cooldown[Obj.Parent] then return end
	Cooldown[Obj.Parent] = true
	
	wait(1)

	Cooldown[Obj.Parent] = nil
end

You can simply add a debounce, this is sample code and I'll explain it.

part.Touched:Connect(function(t)
	print("Touched")
end)

This will spam because it has mutliple parts constantly touching it. To get around this, you simply add a value that will enable / disable itself with a cooldown like done below: local debounce = false

part.Touched:Connect(function(t)
	if debounce == false then
		print("Touched")

		debounce = true
		wait(1)
		debounce = false
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.