Weird tween issue

Hello,

I have a crouching script, where when I crouch the humanoid’s CameraOffset property tweens to a different value. when I run the script, the tween seems to always play, as when it finishes it reverts back a little.

video:

source code (StarterCharacterScripts/Movement/Crouching):

local char = script.Parent.Parent
local cam = workspace.CurrentCamera
local hum : Humanoid = char:WaitForChild("Humanoid")
local animator : Animator = hum:WaitForChild("Animator")
local hrp : Part = char:WaitForChild("HumanoidRootPart")
local head : Part = char:WaitForChild("Head")
local run = game:GetService("RunService")
local uis = game:GetService("UserInputService")
local ts = game:GetService("TweenService")
local _afold = game:GetService("ReplicatedStorage").animations -- animations

local _c = uis.InputBegan
local crouching = false

local crouch = _afold.crouchIdle
local walk = _afold.crouchwalk

local track1 = animator:LoadAnimation(crouch)
local track2 = animator:LoadAnimation(walk)

local info = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local tween1 = ts:Create(hum, info, {CameraOffset = Vector3.new(0, -1.5, 0)})
local tween2 = ts:Create(hum, info, {CameraOffset = Vector3.new(0, 0, 0)})

function CrouchMove()
	if tween1.PlaybackState ~= Enum.PlaybackState.Playing then
		tween1:Play()
		tween2:Pause()
	end
	print(tween1.PlaybackState)
	if hum.MoveDirection.Magnitude > 0.1 then
		if not track2.IsPlaying then
			track1:Stop()
			track2:Play()
			hum.CameraOffset = Vector3.new(0, -0.5, 0)
		end
	else
		if not track1.IsPlaying then
			track2:Stop()
			track1:Play()
		end
	end
end

_c:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.C or input.KeyCode == Enum.KeyCode.LeftControl then
		if hum.WalkSpeed <= 16 then
			if crouching == true then
				print("end")
				crouching = not crouching
				tween2:Play()
				tween1:Pause()
				track2:Stop()
				track1:Stop()
				run:UnbindFromRenderStep("crouchmove")
			else
				print("crouch")
				crouching = not crouching
				run:BindToRenderStep("crouchmove", 99, CrouchMove)
			end
		end
	end
end)

any help is appreciated!

1 Like