How to detect correct if a player is holding a button?

  1. What do you want to achieve? While a player holding down a button add 1 to an int value but with debounc beetween pressing Keep it simple and clear!

  2. What is the issue? I have the script that detects it but I tried to add cooldown between pressing but when I spam the button it add more then 1. Any ideas how to fix it? Include screenshots / videos if possible!

  3. What solutions have you tried so far? I tried to add normal debounce but it doesn’t helped Did you look for solutions on the Developer Hub? yes, I did but I couldn’t find anything about it

Ignore the Strength
While holding the button:

https://gyazo.com/4c7893b8e02fbc5d0102572acd28b5ad

When spamming the button:

https://gyazo.com/c1fe9079e52f8d7a8d63aab2b7160308

-- local Script
        UIS.InputBegan:Connect(function(input,isTyping)
		if isTyping then return end
		if input.KeyCode == Enum.KeyCode.R then
			if not restDb then
				restDb = true
				restRemote:FireServer("Begin")
			end
		end
	end)
	
	UIS.InputEnded:Connect(function(input,isTyping)
		if isTyping then return end
		if input.KeyCode == Enum.KeyCode.R then
			if restDb then
				restRemote:FireServer("Ended")
			end
		end
	end)

restRemote.OnClientEvent:Connect(function()
	wait(restCd)
	restDb = false
end)

--ServerScript

restRemote.OnServerEvent:Connect(function(player,Action)
	local char = player.Character
	local hum = char:WaitForChild("Humanoid")
	
	local energy = player.leaderstats.Energy
	local maxEnergy = player.Backpack.Values.MaxEnergy
	
	local hold = player.Backpack.Values.Hold
	local holdDb = player.Backpack.Values.HoldDebounce
	
	print("Server")
	
	if Action == "Begin" then
		print("Begin")
		hold.Value = true
		while hold.Value do
			energy.Value += 1
			wait(1)
		end
		
	elseif Action == "Ended" then
		print("End")
		hold.Value = false
		restRemote:FireClient(player)
	end
	

	
end)

What exactly are you trying to do?

make the script that you cant spam the button, like if you spam there is a little cooldown

As @hostnod said, you need to make is so when they press the button, release it, and press it again the InputBegan function doesn’t fire, so put the task.wait(1) in that section, not the if Action == "Begin" section.

So I have to make the while loop inside the client script ?

Oh wait I got it wrong, hold on.

Are you looking for something like this?

-- local Script
UIS.InputBegan:Connect(function(input,isTyping)
	if isTyping then return end
	if input.KeyCode == Enum.KeyCode.R then
		restRemote:FireServer("Begin")
	end
end
end)

UIS.InputEnded:Connect(function(input,isTyping)
	if isTyping then return end
	if input.KeyCode == Enum.KeyCode.R then
		restRemote:FireServer("Ended")
	end
end)



--ServerScript
local CS = game:GetService("CollectionService")

restRemote.OnServerEvent:Connect(function(player,Action)
	local char = player.Character
	local hum = char:WaitForChild("Humanoid")

	local energy = player.leaderstats.Energy
	local maxEnergy = player.Backpack.Values.MaxEnergy

	local hold = player.Backpack.Values.Hold
	local holdDb = player.Backpack.Values.HoldDebounce

	print("Server")

	if Action == "Begin" and not CS:HasTag(player, "CD") then
		CS:AddTag(player, "CD")
		print("Begin")
		hold.Value = true
		while hold.Value do
			energy.Value += 1
			wait(1)
		end

	elseif Action == "Ended" then
		CS:RemoveTag(player, "CD")
		print("End")
		hold.Value = false
		restRemote:FireClient(player)
	end



end)
hold.Value = true
while hold.Value do
	energy.Value += 1
	wait(1)
end

The issue with your code is that every time restRemote is fired, you immediately increment their energy and then yield. Simply swap the order of energy.Value += 1 and wait(1) and you will find players are no longer able to repeatedly spam click for large amounts of energy:

hold.Value = true
while hold.Value do
    task.wait(1)
	energy.Value += 1
end

An alternative would be to have some sort of cooldown as @weakroblox35 has mentioned, although I don’t believe the exact code they provided would function.

It’s an alternative to the cooldown, the player won’t fire the begin state if they have the tag and it gets removed if the player ends the “Begin” state. Not to mention it’s for anti-exploiting purposes.

both of those scripts work fine, but if I start spamming it starts bugging somehow and add more then 1
I’m trying to make a charge effect like in shindo life chi charge or something like that.

any ideas for that ? what am i doing wrong ?

You need to put the while loop outside the server event connection. It should be like this.

task.spawn(function()
	while true do
		task.wait(1)
		if hold.Value == true then
			energy.Value += 1
		else
			continue
		end
	end
end)

but if i put that outside of the loop then i can’t get the energy value and the hold value

Nvm guys I figured it out. Thanks for the help

Please tell everyone how you solved it so if someone searches the forum they’ll see your post answered, and the information can help them too.

instead of a “local db = false” inside the script, I used a bool value and change the debounce inside the server script. That solved my problem.

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