How do i make a gun sway

How do i make gun sway here is my whole script:
local Event = game.ReplicatedStorage.RemoteEvent
local Mouse = game.Players.LocalPlayer:GetMouse()
local stop = true
local isShooting = false
local Runservice = game:GetService(“RunService”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local ViewModel = ReplicatedStorage:WaitForChild(“ViewModel”)
local Camera = workspace.CurrentCamera
local ShootingOffset = CFrame.new(0,0,0.4)
local CameraOffset = CFrame.new(0,-0.5,0.7)
local ShootedOffset = CFrame.new(0,0,0.4)
local AimingOffset = CFrame.new(0,0,0)
local AimedOffset = CFrame.new(-0.75,0.17,2.3) * CFrame.Angles(0.001,0,0)
local isAiming = false
local player = game.Players.LocalPlayer
local Character = player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild(“Humanoid”)
local WalkingOffset = CFrame.new(0,0,0)
local isWalking = false
local OriginSpeed = Humanoid.WalkSpeed
local swayOffset = CFrame.new(0,0,0) * CFrame.Angles(0,0,0)
local CurrentCFrame = CFrame.Angles(0,0,0)

local WalkSpeed = Humanoid.WalkSpeed

local UIS = game:GetService(“UserInputService”)

local x1,x2,x3 = 0,0,0

ViewModel.Parent = workspace

Humanoid.Died:Connect(function()
workspace:WaitForChild(“ViewModel”).Parent = ReplicatedStorage
script:Destroy()
end)

Humanoid.Running:Connect(function(Speed)
if Speed > 0.1 then
isWalking = true
else
isWalking = false
end
end)

UIS.InputBegan:Connect(function(Input)
spawn(function()
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
isShooting = true
wait()
isShooting = false
end
if Input.UserInputType == Enum.UserInputType.MouseButton2 then
UIS.MouseIconEnabled = false
isAiming = true
end
end)
end)

UIS.InputEnded:Connect(function(Input)
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
stop = true
end
if Input.UserInputType == Enum.UserInputType.MouseButton2 then
UIS.MouseIconEnabled = true
isAiming = false
end
end)

Runservice.RenderStepped:Connect(function()
local WalkSpeed = Humanoid.WalkSpeed
x1 = x1 + 0.1
if isWalking then
WalkingOffset = WalkingOffset:Lerp(CFrame.new(0,math.sin(x1*(WalkSpeed/OriginSpeed))/6,0),0.3)
else
WalkingOffset = WalkingOffset:Lerp(CFrame.new(),0.2)
end
if isAiming then
AimingOffset = AimingOffset:Lerp(AimedOffset,0.3)
else
AimingOffset = AimingOffset:Lerp(CFrame.new(),0.2)
end

if isShooting then
	stop = false
	repeat 
		Event:FireServer(Mouse.Hit.p)
		ShootingOffset = ShootingOffset:Lerp(ShootedOffset,0.7)
		Camera.CFrame = Camera.CFrame * CFrame.Angles(math.rad(0.3),0,0)
		wait(0.1)
		Event:FireServer(Mouse.Hit.p)
		ShootingOffset = ShootingOffset:Lerp(ShootedOffset,0.7)
		Camera.CFrame = Camera.CFrame * CFrame.Angles(math.rad(-0.15),0,0)
	until stop == true	
else
	ShootingOffset = ShootingOffset:Lerp(CFrame.new(),0.2)
end

ViewModel:SetPrimaryPartCFrame(Camera.CFrame * CameraOffset * ShootingOffset * AimingOffset * WalkingOffset)

end)

1 Like

oh sorry this is my first post

Here is a open sourced fps, which includes the code for gun sway using springs, you will have to know OOP to understand it though : https://www.roblox.com/games/2198837034/Open-source-FPS, here is a link to the forum post about it : The First Person Element Of A First Person Shooter

thanks for the tip you gave me.

Here is another source that seems good

https://devforum.roblox.com/t/making-an-fps-framework-2020-edition/503318