Visual bug or scripting error?

Hello, currently Im making a framework system. when starting to walk ever since I changed it from an animation to Cframes its been glitching. It only happens when I stop and walk again.

RunService.RenderStepped:Connect(function()
	local currentCameraRotation = workspace.CurrentCamera.CFrame
	local deltaRotation = currentCameraRotation:ToObjectSpace(lastCameraRotation)

	local _, deltaY, deltaZ = deltaRotation:ToOrientation()

	local targetSwayX = math.clamp(deltaY * swayIntensity, -0.05, 0.05)
	local targetSwayY = math.clamp(deltaZ * swayIntensity, -0.05, 0.05)

	local targetSway = CFrame.new(targetSwayX, targetSwayY, 0) * CFrame.Angles(0, -targetSwayX * 0.5, targetSwayY * 0.5)
	swayAmount = swayAmount:Lerp(targetSway, swaySmoothing)

	lastCameraRotation = currentCameraRotation

	local targetBob = CFrame.new()
	if character and character.Humanoid then
		local walkVector = character.Humanoid.MoveDirection
		local isMoving = walkVector.Magnitude > 0

		if isMoving and not Manager.isAimed then
			if not wasMoving then
				local cycleRemainder = bobCycle % (math.pi * 2)
				if cycleRemainder > math.pi then
					bobCycle = math.pi * 2
				else
					bobCycle = 0
				end
			end

			bobCycle = bobCycle + (RunService.RenderStepped:Wait() * bobSpeed)

			local bobX = math.sin(bobCycle) * bobIntensity
			local bobY = math.abs(math.sin(bobCycle * 2)) * bobIntensity
			local bobZ = math.cos(bobCycle * 0.5) * bobIntensity * 0.5

			local bobRotX = math.sin(bobCycle * 1.5) * 0.02
			local bobRotY = math.sin(bobCycle) * 0.015
			local bobRotZ = math.cos(bobCycle * 1.2) * 0.01

			targetBob = CFrame.new(bobX, bobY, bobZ) * CFrame.Angles(bobRotX, bobRotY, bobRotZ)
		else
			bobCycle = bobCycle * 0.9
		end

		wasMoving = isMoving
	end

	bobAmount = bobAmount:Lerp(targetBob, bobSmoothing)

	local targetTransform = aimCF * swayAmount * bobAmount
	combinedTransform = combinedTransform:Lerp(targetTransform, transformSmoothing)

	for i, v in pairs(workspace.Camera:GetChildren()) do
		if v:IsA("Model") then
			v:SetPrimaryPartCFrame(workspace.CurrentCamera.CFrame * combinedTransform)
		end
	end

	if Framework and Framework.AimPart then
		if Manager.isAimed then
			local offset = Framework.AimPart.CFrame:ToObjectSpace(Framework.PrimaryPart.CFrame)
			aimCF = aimCF:Lerp(offset, 0.1)
		else
			aimCF = aimCF:Lerp(CFrame.new(), 0.1)
		end
	end
end)

CFrame part of the script

local aimCF = CFrame.new()
local Framework = nil
local currentGunModule = nil

local lastCameraRotation = workspace.CurrentCamera.CFrame
local swayAmount = CFrame.new()
local swayIntensity = 0.2
local swaySmoothing = 0.1

local bobAmount = CFrame.new()
local bobCycle = 0
local bobSpeed = 8
local bobIntensity = 0.03
local bobSmoothing = 0.15
local wasMoving = false

local combinedTransform = CFrame.new()
local transformSmoothing = 0.2

variables

video of the visual glitch

You could store the last move direction, and then for the calculation just lerp it to the current. Not sure how well it would work, but it’s worth a shot.

1 Like

No tried that still does the same thing. Sorry for the late reply :sweat_smile:

1 Like

I FIGURED IT OUT AFTER 2 DAYS

The issue was caused by the bobbing cycle (bobCycle) being reset or interrupted when movement state changed. The original code would either reset the cycle or stop updating it when the player stopped moving, causing discontinuity when movement resumed.

The Solution: I implemented a continuous bobbing system with intensity fading instead of starting/stopping the bobbing cycle:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.