Debounce is being ignored?


local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = char:WaitForChild("Humanoid")

--//Models\\--
local deadliftBar = game.Workspace.deadliftBar
local squatBar = game.Workspace.squatBar
local squatRack = game.Workspace:WaitForChild("squatRack")
local squatRackOccupied = squatRack:WaitForChild("squatRackOccupied")
local deadliftBool = squatRack:WaitForChild("deadliftBool")
local squatBool = squatRack:WaitForChild("squatBool")

--//Services\\--
local CAS = game:GetService("ContextActionService")
local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

--//Gui\\--
local playerGui = plr:WaitForChild("PlayerGui")
local squatRackGui = playerGui:WaitForChild("squatRackGui")
local DeadliftGui = squatRackGui:WaitForChild("Deadlift")
local SquatGui = squatRackGui:WaitForChild("Squat")
local squatRackFGui = playerGui.squatRackGuiFolder:WaitForChild("squatRackFGui")
local deadliftOffGui = squatRackGui:WaitForChild("deadliftOffGui")
local squatOffGui = squatRackGui:WaitForChild("squatOffGui")
local weightSelection = playerGui:WaitForChild("squatRackGui").deadliftWeightChange


--//Deadlift remotes\\--
local getOnDeadlift = RS.squatRackRemotes.deadliftRemote.getOnDeadlift
local deadlift = RS.squatRackRemotes.deadliftRemote.DeadliftEvent
local getOffDeadlift = RS.squatRackRemotes.deadliftRemote.getOffDeadlift


--//Squat remotes\\--
local squatting = RS.squatRackRemotes.squatRemote.SquatEvent
local getOnSquat = RS.squatRackRemotes.squatRemote.getOnSquat
local getOffSquat = RS.squatRackRemotes.squatRemote.getOffSquat

--//Values\\--
local debounce = false
local Owner = plr:WaitForChild("Owner").Value
local deadliftdebounce = false
local deadliftMinimum = 10
local squatdebounce = false
local squatMinimum = 10
local chosenWeight = false

local function deadliftUp(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		if plr.leaderstats.Energy.Value > 0 and plr.leaderstats.Energy.Value >= deadliftMinimum then
			if not deadliftdebounce then
				if chosenWeight == true then
					deadlift:FireServer(value)
					deadliftTrack2:Play()
				else
					print("Choose a weight.")
				end
				deadliftdebounce = true
				wait(2.5)
				deadliftdebounce = false
			end
		end
	end
end

deadliftOffGui.MouseButton1Click:Connect(function()
	if deadliftdebounce == false then
		chosenWeight = false
		weightSelection.Visible = false
		deadliftOffGui.Visible = false
		plr.Owner.Value = "None"
		CAS:UnbindAction("deadlift")
		getOffDeadlift:FireServer()
		deadliftTrack:Stop()
		deadliftTrack2:Stop()
	end
end)

So for some reason it keeps ignoring the if deadliftdebounce == false part when I click on the TextButton and automatically does everything inside of the function. How can I get it to check the conditional statement before doing the rest?

#EDIT: I included all of the missing variables.

2 Likes

I do not see any part of the script establishing deadliftdebounce as a variable

3 Likes

I assume it is just not a local variable, I think he also needs to put it out side of the function.

1 Like

My bad I forgot to include that before I posted. I didnt include all of the variables that I had earlier.

2 Likes

I readded all of the variables I forgot to include.

2 Likes

After rereading it, it seems that after checking the debounce is false, you for got to set it to true.

Here:

You should set the debounce to true after checking it is false. Like:

deadliftOffGui.MouseButton1Click:Connect(function()
	if deadliftdebounce == false then
		deadliftdebounce = true 
		chosenWeight = false
		weightSelection.Visible = false
		deadliftOffGui.Visible = false
		plr.Owner.Value = "None"
		CAS:UnbindAction("deadlift")
		getOffDeadlift:FireServer()
		deadliftTrack:Stop()
		deadliftTrack2:Stop()
		wait() -- Put a wait of you want to
		deadliftdebounce = false
	else
	end
end
3 Likes

I dont think that would be the case cause in the deadliftUp function I set deadliftdebounce = true and wait 2.5 seconds and turn it to deadliftdebounce = false

What I’m trying to do is if deadliftdebounce = false then the MouseButton1Click function wont run.

Just want to ask where do you call the function deadliftUp() ?

Edit: So it seems that the debounce is not going to work because the debounce was never set to true due to the fact you never called the deadliftUp() function.

1 Like

(Sorry in advance for the late replies)
I call it when the player presses their Q key. Okay I omitted a lot of the code that is contained in this cause I thought it would be too much, so I thought it’d be good to just focus where the problem lies.

Just to clear things up:

  • I have a GUI that a player can select whether they want to do the deadlift or a squat.
  • Once they select either deadlift or squat from the two GUIs then their Q key is binded by ContextActionService with either deadliftup() or squat(). (The squat and deadlift are practically the same code).

The issue I’m having is that when they press Q and the deadliftdebounce is set to true it completely ignores the conditional statement that is after they click the deadliftOffGui

My question is how I could get it to check the conditional statement after they press it so it doesnt go through unless deadliftdebounce is false?