Mobile controls are spammable before it gets put on cooldown

I have created mobile controls for the 1 keybind in my game, however I have run into a problem.

The button is spammable 10+ times before the debounce is set to true, and I don’t know how to fix it. (i have never made mobile controls before and i know its a lot harder than what I have but it’s the best i could do)

Code:

local player = game.Players.LocalPlayer
local debounce = false


script.Parent.MouseButton1Click:Connect(function()
	if debounce == false and not player.Character:FindFirstChild("Sacrificed") then
		debounce = true
		game:GetService("ReplicatedStorage"):WaitForChild("Sacrifice"):FireServer()
		print("fired")
		debounce = false
	end
end)

Not sure what you mean here, there is no Cooldown because you have none set. The debounce is set to true and then instantly sent back to false.

script.Parent.MouseButton1Click:Connect(function()
	if debounce == false and not player.Character:FindFirstChild("Sacrificed") then
		debounce = true
        spawn(function()
           task.Wait(5)
           debounce = false
        end)
print("fired")
		game:GetService("ReplicatedStorage"):WaitForChild("Sacrifice"):FireServer()
		
		
	end
end)
1 Like

Add a task.wait(1) after the event is fired, no debounce is being set.

there is a cooldown being set, it’s handled in this line here

In the server script, i insert a value to the player’s character called Sacrifice and delete it 15 seconds after. that’s how i do the cooldown for the keybind and it works fine so I’m not sure how it breaks when trying to convert it to a mobile button

you can also see that there’s a cooldown in the video when it stops printing “fired” after it reaches 13 prints, and i’m still spamming the button but it’s not doing anything

If this is the case you can wait until the Sacraficed is added then set debounce to false because there might be a bit of delay before Sacrificed is added:

local player = game.Players.LocalPlayer
local debounce = false


script.Parent.MouseButton1Click:Connect(function()
	if debounce == false and not player.Character:FindFirstChild("Sacrificed") then
		debounce = true
		game:GetService("ReplicatedStorage"):WaitForChild("Sacrifice"):FireServer()
		print("fired")
        repeat task.wait() until player.Character:FindFirstChild("Sacrificed")
		debounce = false
	end
end)