I have a text button, which on clicked, gives the player the ability to double jump.

But when the button is clicked again, the ability of double jump should be removed.

The first part is finished, i.e. the the player gets the ability to double jump once the button is clicked. However, when the player again click the button, the ability of double jump does not get removed. I can't figure out how.

Here is the local script inside the text button:

local Player = game.Players.LocalPlayer
local Open = script.Parent.Open
local WalkSpeedId = 12498281

script.Parent.MouseButton1Click:Connect(function()
	
	if Open.Value == false then
		Open.Value = true
		
		
		
		script.Parent.Text = "Double Jump: On"
		script.Parent.BackgroundTransparency = 0
		Player.Character.Humanoid.WalkSpeed = 25
		
		
		
		
		local UserInputService = game:GetService("UserInputService")
		local localPlayer = game.Players.LocalPlayer
		local character
		local humanoid
		local canDoubleJump = false
		local hasDoubleJumped = false
		local oldPower
		local TIME_BETWEEN_JUMPS = 0.2
		local DOUBLE_JUMP_POWER_MULTIPLIER = 2
		
		function onJumpRequest()
			if not character or not humanoid or not character:IsDescendantOf(workspace) or
				humanoid:GetState() == Enum.HumanoidStateType.Dead then
				return
			end

			if canDoubleJump and not hasDoubleJumped then
				hasDoubleJumped = true
				humanoid.JumpPower = oldPower * DOUBLE_JUMP_POWER_MULTIPLIER
				humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
		end
		local function characterAdded(newCharacter)
			character = newCharacter
			humanoid = newCharacter:WaitForChild("Humanoid")
			hasDoubleJumped = false
			canDoubleJump = false
			oldPower = humanoid.JumpPower

			humanoid.StateChanged:connect(function(old, new)
				if new == Enum.HumanoidStateType.Landed then
					canDoubleJump = false
					hasDoubleJumped = false
					humanoid.JumpPower = oldPower
				elseif new == Enum.HumanoidStateType.Freefall then
					wait(TIME_BETWEEN_JUMPS)
					canDoubleJump = true
				end
			end)
		end
		if localPlayer.Character then
			characterAdded(localPlayer.Character)
		end

		localPlayer.CharacterAdded:connect(characterAdded)
		UserInputService.JumpRequest:connect(onJumpRequest)
		
	elseif Open.Value == true then
		
		Open.Value = false

		-- THIS IS THE PLACE WHERE I AM SUPPOSED TO TURN IT OFF.

		script.Parent.Text = "Double Jump: Off"
		script.Parent.BackgroundTransparency = .5
		Player.Character.Humanoid.WalkSpeed = 16
		
	end
	
	
	

	
end)

Thanks for any help.

3 comments

0

You can create variable like "CanUseDoubleJump" or something outside the if statement and set it to false when turning off double jump, then just check if the variable is true

0

Yes. I tried that and it worked :D

0

I mean your not using FE