Help with jump charge system

Hello, I am trying to make a jump system in which your JumpPower depends on how long you hold space down. Here’s my code so far and JumpPower doesn’t reset to default when space is released even tho I cancelled the TweenBase which is charge. I thought :Cancel stops the interpolation and resets the value?

local CAS = game:GetService("ContextActionService")
local TweenService = game:GetService("TweenService")

local character = script.Parent
local humanoid = character.Humanoid
local rootPart = humanoid.RootPart
local animator = humanoid.Animator

local anticipationAnim = animator:LoadAnimation(character:WaitForChild("Anticipation"))
local charge = TweenService:Create(humanoid, TweenInfo.new(10), {JumpPower = 200})

local function jump(_, inputState)
	if inputState == Enum.UserInputState.Begin then
		anticipationAnim:Play()
				
		charge:Play()
	else				
		anticipationAnim:Stop()
		
		charge:Cancel()
	end
end

anticipationAnim.Stopped:Connect(function()
	humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end)

CAS:BindAction("Jump", jump, true, Enum.KeyCode.Space)
2 Likes

Cancel() only resets the variables of the tweens, not the properties changed by the tween, which means if you cancel it mid animation, it won’t reset the properties to it’s original values. - TweenBase | Roblox Creator Documentation

2 Likes

Mark question as solved if your problem has been solved, thanks :grin:

I’m still figuring it out. Thank you for the info tho.

1 Like

You can easily set the JumpPower back to 0 after cancelling the tween manually.

charge:Cancel()
JumpPower = 0
2 Likes

I think we solved it. Thank you for your help guys.

local CAS = game:GetService("ContextActionService")
local TweenService = game:GetService("TweenService")

local character = script.Parent
local humanoid = character.Humanoid
local rootPart = humanoid.RootPart
local animator = humanoid.Animator

local anticipationAnim = animator:LoadAnimation(character:WaitForChild("Anticipation"))
local charge = TweenService:Create(humanoid, TweenInfo.new(10), {JumpPower = 200})

local function jump(_, inputState)
	if inputState == Enum.UserInputState.Begin then		
		anticipationAnim:Play()
				
		charge:Play()
	else		
		anticipationAnim:Stop()
		
		charge:Cancel()
		
		humanoid.JumpPower = 50
	end
end

anticipationAnim.Stopped:Connect(function()	
	humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end)

CAS:BindAction("Jump", jump, true, Enum.KeyCode.Space)

-----[ TESTING ]-----

while wait(0.1) do
	print(humanoid.JumpPower)
end

I’m giving the solution to @Winbloo.