Push-Up script fires twice when pressing J

I want to make a push-up script that makes you go in-and out of the position by pressing j (An on and off switch basically) and I want it to switch each time between being on and off.

Currently it toggles it twice in a row.

Here’s currenty what I have in the local script.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local PushUpsEvent = ReplicatedStorage:WaitForChild("PushUps")

local DoPushUpsEvent = ReplicatedStorage:WaitForChild("DoPushUps")

local EndPushUpsEvent = ReplicatedStorage:WaitForChild("EndPushUps")

local Mouse = game.Players.LocalPlayer:GetMouse()

local Debounce = false

local InPushUps = false

local InPushBool = false

Mouse.KeyDown:Connect(function(KeyCode)
	
	if KeyCode == "j" then
		
	if Debounce then return end
		
		if Debounce == false then

			Debounce = true

			if not InPushBool then
				print("Starting pushups")
				PushUpsEvent:FireServer()
				InPushUps = true
				InPushBool = true

				wait(1.5)
				
				EndPushUpsEvent:FireServer()
				InPushUps = false
				InPushBool = false
				Debounce = false
			end
		end		
end
	
	if InPushUps then

		if KeyCode == "k" then
			if Debounce == false then

				Debounce = true

				print("Doing Pushups")

				DoPushUpsEvent:FireServer()

				wait(1.5)

				Debounce = false

			end
		end
	end
	
end)

Seems like you need to separate your debounces for the ‘j’ and ‘k’ key to work at the same time. What does the output log print? What are you expecting it to print?

if Debounce then return end
		
if Debounce == false then

Here you check the debounce twice unnecessarily.

image
Use the document formatter before pasting code, it will fix indentation for you!

The J and K are unrelated, the part is

			if not InPushBool then
				print("Starting pushups")
				PushUpsEvent:FireServer()
				InPushUps = true
				InPushBool = true

				wait(1.5)
				
				EndPushUpsEvent:FireServer()
				Debounce = false
			end

But I do not know how to make this into a switch function, after the debounce it automatically initiates the second part of the function

This will switch your booleans and fire to the appropriate event. Do not set debounce here, you will not need it.

local event = if InPushBool then EndPushUpsEvent else PushUpsEvent
InPushBool = not InPushBool
InPushUps = InPushBool
event:FireServer()
print("New pushup state", InPushBool)
1 Like

How did I not think of that? Thank you very much.