Angle of rotation increases every time i reequip the gun

So im trying to make a system of recoil for my gun but theres one issue. Every time i reequip a weapon that i use the recoil system the angle gets bigger

Video of the issue

Heres the code of the recoil and anims (the important parts are highlighted with a comment saying IMPORTANT PART

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local LogService = game:GetService("LogService")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local function setupViewModel()
	-- Destroy existing viewmodel if it exists
	if workspace.CurrentCamera:FindFirstChild("ViewModelMP5SD") then
		workspace.CurrentCamera.ViewModelMP5SD:Destroy()
	end

	-- Clone the viewmodel from ReplicatedStorage
	local viewmodel = ReplicatedStorage:WaitForChild("ViewModelMP5SD"):Clone()
	viewmodel.Parent = workspace.CurrentCamera

	-- Get the Animator
	local animator = viewmodel:WaitForChild("Humanoid"):WaitForChild("Animator")

	-- Load the animations
	local Ranimation = Instance.new("Animation")
	Ranimation.AnimationId = "rbxassetid://114378899541080" -- Replace with your animation ID
	local RanimationTrack = animator:LoadAnimation(Ranimation)
	RanimationTrack.Priority = Enum.AnimationPriority.Action2
	local REanimation = Instance.new("Animation")
	REanimation.AnimationId = "rbxassetid://97988611711973" -- Replace with your animation ID
	local REanimationTrack = animator:LoadAnimation(REanimation)
	local REEmptyAnimation = Instance.new("Animation")
	REEmptyAnimation.AnimationId = "rbxassetid://120156932108231" -- Replace with your animation ID for empty reload
	local REEmptyAnimationTrack = animator:LoadAnimation(REEmptyAnimation)
	local RanimationL = Instance.new("Animation")
	RanimationL.AnimationId = "rbxassetid://107672290880560" -- Replace with your animation ID for last shot
	local RanimationLTrack = animator:LoadAnimation(RanimationL)
	local WalkAnimation = Instance.new("Animation")
	WalkAnimation.AnimationId = "rbxassetid://88135397333059" -- Replace with your animation ID
	local WalkAnimationTrack = animator:LoadAnimation(WalkAnimation)
	WalkAnimationTrack.Looped = true
	-- Function to stop all playing animations
	local function stopAllAnimations()
		for _, track in ipairs(animator:GetPlayingAnimationTracks()) do
			track:Stop()
		end
	end
	
	local camera = workspace.CurrentCamera
	-- IMPORTANT PART
	local function moveCameraUp()
		local currentCFrame = camera.CFrame
		local newCFrame = currentCFrame * CFrame.Angles(math.rad(0.8), 0, 0)
		camera.CFrame = newCFrame
	end
	
	-- Function to play the animations
	local function RecoilAnimation()
		stopAllAnimations()
		RanimationTrack:Play()
	end
	local function ReloadAnimation()
		stopAllAnimations()
		REanimationTrack.Priority = Enum.AnimationPriority.Action4
		REanimationTrack:Play()
	end
	local function ReloadEmptyAnimation()
		stopAllAnimations()
		REEmptyAnimationTrack:Play()
	end
	local function LastShotAnimation()
		stopAllAnimations()
		RanimationLTrack:Play()
	end

	-- Lock position at the end of the last shot animation
	RanimationLTrack.Stopped:Connect(function()
		local lastFrameCFrame = viewmodel.PrimaryPart.CFrame -- Assuming PrimaryPart is set
		viewmodel:SetPrimaryPartCFrame(lastFrameCFrame)
	end)

	-- Connect the log messages to the animations
	LogService.MessageOut:Connect(function(message, messageType)
		if message == "GunFiredMP5SD" then  -- IMPORTANT PART
				moveCameraUp()
				RecoilAnimation()
			end
		if message == "GunFiredMP5SDLast" then -- IMPORTANT PART
				moveCameraUp() 
				LastShotAnimation()
			end
		if message == "ReloadMP5SDNotEmpty" then
			ReloadAnimation()
		elseif message == "ReloadMP5SDEmpty" then
			ReloadEmptyAnimation()
		end
	end)

	-- Function to handle walking animation
	local function updateWalkingAnimation()
		if character.Humanoid.MoveDirection.Magnitude > 0 then
			if not WalkAnimationTrack.IsPlaying then
				WalkAnimationTrack:Play()
			end
		else
			if WalkAnimationTrack.IsPlaying then
				WalkAnimationTrack:Stop()
			end
		end
	end

	-- Connect the walking animation update to the RenderStepped event
	RunService.RenderStepped:Connect(updateWalkingAnimation)
end


-- Function to handle tool equipped
local function onToolEquipped(tool)
	if tool.Name == "MP5SD" then
		setupViewModel()
	end
end

-- Connect the tool equipped event
player.Character.ChildAdded:Connect(function(child)
	if child:IsA("Tool") then
		onToolEquipped(child)
	end
end)

-- Handle tool replacement
player.Character.ChildRemoved:Connect(function(child)
	if child:IsA("Tool") and child.Name == "MP5SD" then
		setupViewModel()
	end
end)

I’m kind of confused, you move the camera up, but is there any code that is moving the camera back down?

I think that’s because your functions just stacking when you equipping tool more than one time.
I think you can make connection variable, and when tool is unequipped connection:Disconnect().

local connection1
local connection2
local connection3
local function setupViewModel()
-- your script
local function RanimationLTrackStopped()
local lastFrameCFrame = viewmodel.PrimaryPart.CFrame -- Assuming PrimaryPart is set
		viewmodel:SetPrimaryPartCFrame(lastFrameCFrame)
end
local function MessageOut()
if message == "GunFiredMP5SD" then  -- IMPORTANT PART
				moveCameraUp()
				RecoilAnimation()
			end
		if message == "GunFiredMP5SDLast" then -- IMPORTANT PART
				moveCameraUp() 
				LastShotAnimation()
			end
		if message == "ReloadMP5SDNotEmpty" then
			ReloadAnimation()
		elseif message == "ReloadMP5SDEmpty" then
			ReloadEmptyAnimation()
		end
end
local function RunService()
updateWalkingAnimation()
end
connection1 = RanimationLTrack.Stopped:Connect(RanimationLTrackStopped)
connection2 = LogService.MessageOut:Connect(MessageOut)
connection3 = RunService.RenderStepped:Connect(RunService)
end
player.Character.ChildRemoved:Connect(function(child)
	if child:IsA("Tool") and child.Name == "MP5SD" then
		setupViewModel()
        connection1:Disconnect()
        connection2:Disconnect()
        connection3:Disconnect()
	end
end)

Hope I helped you!

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