How can I fix my math.clamp() in viewmodel script?

I do not know how to explain this issue, but I have a video of It.

Here’s the video:


And here’s my code:

local aimCF = CFrame.new()
local aimOffset = Vector2.new()
local camera = workspace.CurrentCamera
local LastCameraCFrame = camera.CFrame
local uis = game:GetService("UserInputService")
local viewmodel

local equipped = false

script.Parent.Equipped:Connect(function()
	viewmodel = game.ReplicatedStorage.Viewmodel:Clone()
	viewmodel.Parent = camera
	equipped = true
end)

script.Parent.Unequipped:Connect(function()
	viewmodel:Destroy()
	equipped = false
end)

game:GetService("RunService").RenderStepped:Connect(function()
	LastCameraCFrame = camera.CFrame
	if equipped then
		local mousedelta = uis:GetMouseDelta()
		
		--if camera.CFrame == LastCameraCFrame then
		aimOffset = aimOffset + Vector2.new(mousedelta.X, mousedelta.Y)
		--end
		
		local rot = camera.CFrame:ToObjectSpace(LastCameraCFrame)
		local x,y,z = rot:ToOrientation()
		
		local swayCF = CFrame.Angles(math.rad(math.clamp(-aimOffset.Y/8, -15, 15)),math.rad(math.clamp(-aimOffset.X/10, -15, 15)),0)
		
		local offset = CFrame.new()
		aimCF = aimCF:Lerp(swayCF, .2)
		
		viewmodel:SetPrimaryPartCFrame(camera.CFrame * aimCF)
		
		local humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
		if humanoid then
			--local currentTime = tick()
			if game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").MoveDirection.Magnitude > 0 then
				local bobOffset = CFrame.new(0, -humanoid.CameraOffset.Y/3, -humanoid.CameraOffset.Z/3) * CFrame.Angles(0, math.sin(tick() * -4) * -.05, math.cos(tick() * -4) *-.05)

				viewmodel:SetPrimaryPartCFrame(camera.CFrame * aimCF * bobOffset)
					--[[local bobbleX = math.abs(currentTime * 10) * .25
					local bobbleY = math.abs(math.sin(currentTime * 5))   
					
					local bobble = Vector3.new(bobbleX, bobbleY, 0)
					
					cf = cf:Lerp(CFrame.Angles(0, bobbleY, 0), .1)]]
			end
		end
	end
end)

I just want do make a game like “Deadline” but I want to make It world war 1.
I appreciate your help.

Is the sway the thing you are not liking?

Let me hop on my pc and I’ll give u a script

Ok. Here is how I have sway set up in my FPS Code.

local mouseDelta = uis:GetMouseDelta()/50

local swayX = math.clamp(mouseDelta.X, -0.25,0.25)
local swayY = math.clamp(mouseDelta.Y, -0.25,0.25)

swayCF = swayCF:Lerp(CFrame.new(-swayX, swayY, 0), .1)

That is how I have sway in my game.

Now I have another problem
I think It get worse

lol. Can you show me the code u have now?

Here’s my code:

local aimCF = CFrame.new()
local aimOffset = Vector2.new()
local camera = workspace.CurrentCamera
local LastCameraCFrame = camera.CFrame
local uis = game:GetService("UserInputService")
local viewmodel

local equipped = false

script.Parent.Equipped:Connect(function()
	viewmodel = game.ReplicatedStorage.Viewmodel:Clone()
	viewmodel.Parent = camera
	equipped = true
end)

script.Parent.Unequipped:Connect(function()
	viewmodel:Destroy()
	equipped = false
end)

game:GetService("RunService").RenderStepped:Connect(function()
	LastCameraCFrame = camera.CFrame
	if equipped then
		local mousedelta = uis:GetMouseDelta()
		local swayX = math.clamp(mousedelta.X, -15,15)
		local swayY = math.clamp(mousedelta.Y, -15,15)
		
		--if camera.CFrame == LastCameraCFrame then
		aimOffset = aimOffset + Vector2.new(swayY,swayX)
		--end
		
		local rot = camera.CFrame:ToObjectSpace(LastCameraCFrame)
		local x,y,z = rot:ToOrientation()
		
		--local swayCF = CFrame.Angles(math.rad(math.clamp(-aimOffset.Y/8, -15, 15)),math.rad(math.clamp(-aimOffset.X/10, -15, 15)),0)
		local swayCF = CFrame.Angles(math.rad(-aimOffset.X/8), math.rad(-aimOffset.Y/8), 0)
		
		--local offset = CFrame.new()
		aimCF = aimCF:Lerp(swayCF, .1)
		
		viewmodel:SetPrimaryPartCFrame(camera.CFrame * aimCF)
		
		local humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
		if humanoid then
			--local currentTime = tick()
			if game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").MoveDirection.Magnitude > 0 then
				local bobOffset = CFrame.new(0, -humanoid.CameraOffset.Y/3, -humanoid.CameraOffset.Z/3) * CFrame.Angles(0, math.sin(tick() * -4) * -.05, math.cos(tick() * -4) *-.05)

				viewmodel:SetPrimaryPartCFrame(camera.CFrame * aimCF * bobOffset)
					--[[local bobbleX = math.abs(currentTime * 10) * .25
					local bobbleY = math.abs(math.sin(currentTime * 5))   
					
					local bobble = Vector3.new(bobbleX, bobbleY, 0)
					
					cf = cf:Lerp(CFrame.Angles(0, bobbleY, 0), .1)]]
			end
		end
	end
end)

I think this may be the problem

        local mousedelta = uis:GetMouseDelta()
		local swayX = math.clamp(mousedelta.X, -15,15)
		local swayY = math.clamp(mousedelta.Y, -15,15)
		
		--if camera.CFrame == LastCameraCFrame then
		aimOffset = aimOffset + Vector2.new(swayY,swayX)
		--end
		
		local rot = camera.CFrame:ToObjectSpace(LastCameraCFrame)
		local x,y,z = rot:ToOrientation()
		
		--local swayCF = CFrame.Angles(math.rad(math.clamp(-aimOffset.Y/8, -15,    15)),math.rad(math.clamp(-aimOffset.X/10, -15, 15)),0)
		local swayCF = CFrame.Angles(math.rad(-aimOffset.X/8), math.rad(-aimOffset.Y/8), 0)
		
		--local offset = CFrame.new()
		aimCF = aimCF:Lerp(swayCF, .1)
		
		viewmodel:SetPrimaryPartCFrame(camera.CFrame * aimCF)

Try this:

local mousedelta = uis:GetMouseDelta()
		local swayX = math.clamp(mousedelta.X, -15,15)
		local swayY = math.clamp(mousedelta.Y, -15,15)
		
		--if camera.CFrame == LastCameraCFrame then
		aimOffset = aimOffset + Vector2.new(swayY,swayX)
		--end
		
		local rot = camera.CFrame:ToObjectSpace(LastCameraCFrame)
		local x,y,z = rot:ToOrientation()
		
		--local swayCF = CFrame.Angles(math.rad(math.clamp(-aimOffset.Y/8, -15, 15)),math.rad(math.clamp(-aimOffset.X/10, -15, 15)),0)
		swayCF = swayCF:Lerp(CFrame.new(-swayX, swayY, 0), .1)
		
		--local offset = CFrame.new()
		aimCF = aimCF:Lerp(swayCF, .1)
		
		viewmodel:SetPrimaryPartCFrame(camera.CFrame * aimCF)

Wait I’m not really sure what your trying to fix is it the aiming sway or the sway in general?

I want to fix aiming sway because I do not like how It works. It might be a math.clamp()

oh ok, my bad. I thought u wanted sway to fix in general. So you want the aiming sway to be fixed. What is wrong with it?

I filmed a video to explain my problem:

(Now I really need to go so I will answer later)

Here’s example what I want:

Do you have any other ideas? If not then it is very sad because I wanted to make this viewmodel 2 long years. Anyways thanks for help

OHHH MY BAD I know why its bugging so hard in the second clip u sent

don’t worry. What’s the problem for this issue?

I accidentally put

local swayX = math.clamp(mousedelta.X, -15,15)
local swayY = math.clamp(mousedelta.Y, -15,15)



It should actually be

local swayX = math.clamp(mousedelta.X, -0.3, 0.3)
local swayY = math.clamp(mousedelta.Y, -0.3, 0.3)

I’m so sorry I actually just sold u so hard right there

note: change the 0.3 to any number just set it to what u like

On the 0.3 I wasn’t very clear.

Keep it a decimal point, and also change the lerp amount on the SwayCF to come up with something u like.

Nope. Same issue.

Ok, another idea. Try setting the CFrame of the view model before calculating sway, and then set the CFrame again after u have calculated the sway. When I was making my fps system setting the CFrame before calculating the sway worked for me.

Here is an example:

viewmodel:SetPrimaryPartCFrame(Camera.CFrame)
local sway = -- Calculate the sway however you want
viewmodel:SetPrimaryPartCFrame(Camera.CFrame * sway)

Try this, and let me know what happens.

do something like

viewmodel:SetPrimaryPartCFrame(camera.CFrame * aimCF)
aimCF = aimCF:Lerp(swayCF, .1)
viewmodel:SetPrimaryPartCFrame(camera.CFrame * aimCF)

Like this?

yea like that

Just doing this for more characters