Why doesn't my value change?

I’m currently making a “super jump” which will make the player jump higher, the longer they hold the jump button.

LocalScript:

local contextActionService = game:GetService("ContextActionService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local primaryPart = character.PrimaryPart
local height = 10
local maxHeight = 50

canCrouch = true
canJump = false

local vectorForce = script:WaitForChild("VectorForce")
vectorForce.Attachment0 = primaryPart:WaitForChild("RootRigAttachment")

local crouchTrack = humanoid.Animator:LoadAnimation(script:WaitForChild("Hold"))
local jumpTrack = humanoid.Animator:LoadAnimation(script:WaitForChild("Jump"))

local function superJump(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin and canCrouch == true then
		canCrouch = false
		canJump = true
		humanoid.WalkSpeed = 4
		crouchTrack:Play()
		repeat
			task.wait(1)
			if height < maxHeight then
				height += 10
			end
		until inputState == Enum.UserInputState.End
	elseif inputState == Enum.UserInputState.End and canJump == true then
		canJump = false
		vectorForce.Force = Vector3.new(primaryPart.CFrame.LookVector.X, height, primaryPart.CFrame.LookVector.Z) * 100
		crouchTrack:Stop()
		jumpTrack:Play()
		humanoid.WalkSpeed = 16
		height = 10
		wait(1)
		vectorForce.Force = Vector3.new(0,0,0)
		jumpTrack:Stop()
		task.wait(5)
		canCrouch = true
		canJump = false
	end
end

contextActionService:BindAction("Jump", superJump, true, Enum.KeyCode.Space, Enum.KeyCode.ButtonA)

For some reason when I try to change the height value back to 10, it doesn’t work. Then the next time they jump, no matter how long they hold the jump button, it’s set to the previous height.

Any thoughts as to why this is happening?

1 Like

Inside the function, the parameters are constant local variables.

In the inputState == Begin half of the if-statement, you’re check until inputState == End. Because inputState is staying the same as when the function was first triggered, this will never finish.

You’ll need to use another variable like canJump/canCrouch, but an easier way would be to use some kind of state variable for all of them.

Something like this (I removed the animations so I could run it)

local contextActionService = game:GetService("ContextActionService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local primaryPart = character.PrimaryPart
local height = 10
local maxHeight = 50

local state = "standing"

local vectorForce = script:WaitForChild("VectorForce")
vectorForce.Attachment0 = primaryPart:WaitForChild("RootRigAttachment")

local function superJump(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin and state == "standing" then
		state = "crouched"
		print(state)
		humanoid.WalkSpeed = 4
		repeat
			task.wait(1)
			if height < maxHeight then
				height += 10
			end
			print(state, height)
		until state == "jumping"
	elseif inputState == Enum.UserInputState.End and state == "crouched" then
		state = "jumping"
		print(state, height)
		vectorForce.Force = Vector3.new(primaryPart.CFrame.LookVector.X, height, primaryPart.CFrame.LookVector.Z) * 100
		humanoid.WalkSpeed = 16
		task.wait(1)
		vectorForce.Force = Vector3.new(0,0,0)
		task.wait(5)
		state = "standing"
		height = 10
		print(state)
	end
end

contextActionService:BindAction("Jump", superJump, true, Enum.KeyCode.Space, Enum.KeyCode.ButtonA)
1 Like

Thanks a lot, this really helped me out. :+1: