Disconnect all remoteevents when unequipping a tool

You’re missing an equal sign between connections and {

ok, this fixed the output message but why is the second var undefined it should be ig

You’re missing a comma after the first item. When you have multiple items in the table, you have to use the comma at the end of each item to separate them.

There’s other issues that I see in your code, but whenever you run into the issue, I will let you know.

the comma also doesnt solve it. also mayb i have the code in a wrong pos bc in the last 2 lines are also highlighted red.

Okay so we’ve cleared out the comma and equal sign issue, now it’s the way that you’re adding the items into the table. What I mean by that, is that you already have the connections in your original code:

-- This table should be near the top of your script.
local connections = {}

-- These connections are in your setupAnimations() function.
	MP5SDFire.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		RecoilAnimation()
	end)
	
	MP5SDFireLast.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		LastShotAnimation()
	end)
	ReloadMP5SDNotEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadAnimation()
	end)
	ReloadMP5SDEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadEmptyAnimation()
	end)

So we can re-use this code and assign them to variables like so:

-- This table should be near the top of your script.
local connections = {}

-- These connections are in your setupAnimations() function.
	local MP5SDFireConnection = MP5SDFire.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		RecoilAnimation()
	end)
	
	local MP5SDFireLastConnection = MP5SDFireLast.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		LastShotAnimation()
	end)
	local MP5SDReloadNotEmptyConnection = ReloadMP5SDNotEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadAnimation()
	end)
	local ReloadMP5SDEmptyConnection = ReloadMP5SDEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadEmptyAnimation()
	end)

Then, after assign the variables, we use table.insert() to add them to the connections table:

-- This table should be near the top of your script.
local connections = {}

-- These connections are in your setupAnimations() function.
	local MP5SDFireConnection = MP5SDFire.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		RecoilAnimation()
	end)
	
	local MP5SDFireLastConnection = MP5SDFireLast.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		LastShotAnimation()
	end)
	local MP5SDReloadNotEmptyConnection = ReloadMP5SDNotEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadAnimation()
	end)
	local ReloadMP5SDEmptyConnection = ReloadMP5SDEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadEmptyAnimation()
	end)

	table.insert(connections, MP5SDFireConnection)
	table.insert(connections, MP5SDFireLastConnection)
	table.insert(connections, MP5SDReloadNotEmptyConnection)
	table.insert(connections, ReloadMP5SDEmptyConnection)

so is this right?

	local runConnection = RunService.RenderStepped:Connect(updateWalkingAnimation)
	
	local MP5SDFireConnection = MP5SDFire.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		RecoilAnimation()
	end)

	local MP5SDFireLastConnection = MP5SDFireLast.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		LastShotAnimation()
	end)
	local MP5SDReloadNotEmptyConnection = ReloadMP5SDNotEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadAnimation()
	end)
	local ReloadMP5SDEmptyConnection = ReloadMP5SDEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadEmptyAnimation()
	end)

	table.insert(connections, MP5SDFireConnection)
	table.insert(connections, MP5SDFireLastConnection)
	table.insert(connections, MP5SDReloadNotEmptyConnection)
	table.insert(connections, ReloadMP5SDEmptyConnection)

	local tool = player.Backpack:WaitForChild("MP5SD")

	tool.Unequipped:Connect(function()
		for _, connection in connections do
			-- We verify that the connection isn't nil or already disconnected
			if connection then
				connection:Disconnect()
			end
		end

	-- Clean the table's memory pointers
	connections = {}
	end)
end
	

Almost there. Here’s a clean up:

	local runConnection = RunService.RenderStepped:Connect(updateWalkingAnimation)
	
	local MP5SDFireConnection = MP5SDFire.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		RecoilAnimation()
	end)

	local MP5SDFireLastConnection = MP5SDFireLast.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		LastShotAnimation()
	end)
	local MP5SDReloadNotEmptyConnection = ReloadMP5SDNotEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadAnimation()
	end)
	local ReloadMP5SDEmptyConnection = ReloadMP5SDEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadEmptyAnimation()
	end)

	table.insert(connections, runConnection)
	table.insert(connections, MP5SDFireConnection)
	table.insert(connections, MP5SDFireLastConnection)
	table.insert(connections, MP5SDReloadNotEmptyConnection)
	table.insert(connections, ReloadMP5SDEmptyConnection)
end

local tool = player.Backpack:WaitForChild("MP5SD")

tool.Unequipped:Connect(function()
	for _, connection in connections do
		-- We verify that the connection isn't nil or already disconnected
		if connection then
			connection:Disconnect()
		end
	end

	-- Clean the table's memory pointers
	connections = {}
end)

the issue is still revolving tho heres a vid of it

I see, could you send the current version of your code so that I can try and identify the stacking?

ok, heres all the code.

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 connections = {}

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://121937746273937" -- Replace with your animation ID
	local RanimationTrack = animator:LoadAnimation(Ranimation)
	RanimationTrack.Priority = Enum.AnimationPriority.Action2
	local REanimation = Instance.new("Animation")
	REanimation.AnimationId = "rbxassetid://75766370456110" -- Replace with your animation ID
	local REanimationTrack = animator:LoadAnimation(REanimation)
	REanimationTrack.Priority = Enum.AnimationPriority.Action4
	local REEmptyAnimation = Instance.new("Animation")
	REEmptyAnimation.AnimationId = "rbxassetid://111792193575216" -- Replace with your animation ID for empty reload
	local REEmptyAnimationTrack = animator:LoadAnimation(REEmptyAnimation)
	REEmptyAnimationTrack.Priority = Enum.AnimationPriority.Action4
	local RanimationL = Instance.new("Animation")
	RanimationL.AnimationId = "rbxassetid://118944095797406" -- 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

	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)

	local MP5SDFire = game.ReplicatedStorage["MP5SD RE"]:WaitForChild("MP5SDFire")
	local MP5SDFireLast = game.ReplicatedStorage["MP5SD RE"]:WaitForChild("MP5SDFireLast")
	local ReloadMP5SDNotEmpty = game.ReplicatedStorage["MP5SD RE"]:WaitForChild("ReloadMP5SDNotEmpty")
	local ReloadMP5SDEmpty = game.ReplicatedStorage["MP5SD RE"]:WaitForChild("ReloadMP5SDEmpty")
	
	MP5SDFire.OnClientEvent:Connect(function()
		moveCameraUp()
		RecoilAnimation()
	end)
	
	MP5SDFireLast.OnClientEvent:Connect(function()
		moveCameraUp()
		LastShotAnimation()
	end)
	ReloadMP5SDNotEmpty.OnClientEvent:Connect(function()
		ReloadAnimation()
	end)
	ReloadMP5SDEmpty.OnClientEvent:Connect(function()
		ReloadEmptyAnimation()
	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
	local runConnection = RunService.RenderStepped:Connect(updateWalkingAnimation)

	local MP5SDFireConnection = MP5SDFire.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		RecoilAnimation()
	end)

	local MP5SDFireLastConnection = MP5SDFireLast.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		LastShotAnimation()
	end)
	local MP5SDReloadNotEmptyConnection = ReloadMP5SDNotEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadAnimation()
	end)
	local ReloadMP5SDEmptyConnection = ReloadMP5SDEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadEmptyAnimation()
	end)

	table.insert(connections, runConnection)
	table.insert(connections, MP5SDFireConnection)
	table.insert(connections, MP5SDFireLastConnection)
	table.insert(connections, MP5SDReloadNotEmptyConnection)
	table.insert(connections, ReloadMP5SDEmptyConnection)
end

local tool = player.Backpack:WaitForChild("MP5SD")

tool.Unequipped:Connect(function()
	for _, connection in connections do
		-- We verify that the connection isn't nil or already disconnected
		if connection then
			connection:Disconnect()
		end
	end

	-- Clean the table's memory pointers
	connections = {}
end)
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)

Alright, I’ve removed the duplicate events that you left in that were located before the new changes. I’ve also spaced out the code for you, so that it’s more readable. That should clean up the old connections as it should.

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 connections = {}

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://121937746273937" -- Replace with your animation ID
	local RanimationTrack = animator:LoadAnimation(Ranimation)
	RanimationTrack.Priority = Enum.AnimationPriority.Action2
	local REanimation = Instance.new("Animation")
	REanimation.AnimationId = "rbxassetid://75766370456110" -- Replace with your animation ID
	local REanimationTrack = animator:LoadAnimation(REanimation)
	REanimationTrack.Priority = Enum.AnimationPriority.Action4
	local REEmptyAnimation = Instance.new("Animation")
	REEmptyAnimation.AnimationId = "rbxassetid://111792193575216" -- Replace with your animation ID for empty reload
	local REEmptyAnimationTrack = animator:LoadAnimation(REEmptyAnimation)
	REEmptyAnimationTrack.Priority = Enum.AnimationPriority.Action4
	local RanimationL = Instance.new("Animation")
	RanimationL.AnimationId = "rbxassetid://118944095797406" -- 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

	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)

	local MP5SDFire = game.ReplicatedStorage["MP5SD RE"]:WaitForChild("MP5SDFire")
	local MP5SDFireLast = game.ReplicatedStorage["MP5SD RE"]:WaitForChild("MP5SDFireLast")
	local ReloadMP5SDNotEmpty = game.ReplicatedStorage["MP5SD RE"]:WaitForChild("ReloadMP5SDNotEmpty")
	local ReloadMP5SDEmpty = game.ReplicatedStorage["MP5SD RE"]:WaitForChild("ReloadMP5SDEmpty")
	
	-- 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
	local runConnection = RunService.RenderStepped:Connect(updateWalkingAnimation)

	local MP5SDFireConnection = MP5SDFire.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		RecoilAnimation()
	end)

	local MP5SDFireLastConnection = MP5SDFireLast.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		LastShotAnimation()
	end)

	local MP5SDReloadNotEmptyConnection = ReloadMP5SDNotEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadAnimation()
	end)

	local ReloadMP5SDEmptyConnection = ReloadMP5SDEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadEmptyAnimation()
	end)

	table.insert(connections, runConnection)
	table.insert(connections, MP5SDFireConnection)
	table.insert(connections, MP5SDFireLastConnection)
	table.insert(connections, MP5SDReloadNotEmptyConnection)
	table.insert(connections, ReloadMP5SDEmptyConnection)
end

local tool = player.Backpack:WaitForChild("MP5SD")

tool.Unequipped:Connect(function()
	for _, connection in connections do
		-- We verify that the connection isn't nil or already disconnected
		if connection then
			connection:Disconnect()
		end
	end

	-- Clean the table's memory pointers
	connections = {}
end)

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)
1 Like

AAAAA IT WORKS TYSMJKsfn vgojefbvoudg

1 Like

No problem big dawg, I hope this whole process was a good learning opportunity for you. I could’ve just given you the code from the start, but I wanted to make sure that you learn about how these things work so that you know how to fix it right away next time.

Basically what I’m saying is, instead of giving you a fish, I am teaching you how to fish. Hope you continue this project, it looks good so far.

1 Like

oh and btw is there any way to replace all of one word with another word so like make all MP5SD M4A1

Yeah for sure. If you plan to re-use the same code for all guns, all you need to do is remove MP5SD from all of the connection names and give them general names. Something like ReloadConnection, FireConnection, etc.

That way, you can just copy-paste the code into your M4A1 or AK-47 or HK-416. That’s the beautiful part about improving your programming; you keep it simple and it will work for any similar system.

1 Like

idk what just happened but it stopped working. it worked now it doesnt wth is happening

Did you also rename the parts where you insert the connections into the table?

yes, and it also stopped working in the mp5

Could you send the code once again?

ok

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 connections = {}

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://121937746273937" -- Replace with your animation ID
	local RanimationTrack = animator:LoadAnimation(Ranimation)
	RanimationTrack.Priority = Enum.AnimationPriority.Action2
	local REanimation = Instance.new("Animation")
	REanimation.AnimationId = "rbxassetid://75766370456110" -- Replace with your animation ID
	local REanimationTrack = animator:LoadAnimation(REanimation)
	REanimationTrack.Priority = Enum.AnimationPriority.Action4
	local REEmptyAnimation = Instance.new("Animation")
	REEmptyAnimation.AnimationId = "rbxassetid://111792193575216" -- Replace with your animation ID for empty reload
	local REEmptyAnimationTrack = animator:LoadAnimation(REEmptyAnimation)
	REEmptyAnimationTrack.Priority = Enum.AnimationPriority.Action4
	local RanimationL = Instance.new("Animation")
	RanimationL.AnimationId = "rbxassetid://118944095797406" -- 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

	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)

	local MP5SDFire = game.ReplicatedStorage["MP5SD RE"]:WaitForChild("MP5SDFire")
	local MP5SDFireLast = game.ReplicatedStorage["MP5SD RE"]:WaitForChild("MP5SDFireLast")
	local ReloadMP5SDNotEmpty = game.ReplicatedStorage["MP5SD RE"]:WaitForChild("ReloadMP5SDNotEmpty")
	local ReloadMP5SDEmpty = game.ReplicatedStorage["MP5SD RE"]:WaitForChild("ReloadMP5SDEmpty")
	
	MP5SDFire.OnClientEvent:Connect(function()
		moveCameraUp()
		RecoilAnimation()
	end)
	
	MP5SDFireLast.OnClientEvent:Connect(function()
		moveCameraUp()
		LastShotAnimation()
	end)
	ReloadMP5SDNotEmpty.OnClientEvent:Connect(function()
		ReloadAnimation()
	end)
	ReloadMP5SDEmpty.OnClientEvent:Connect(function()
		ReloadEmptyAnimation()
	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
	local runConnection = RunService.RenderStepped:Connect(updateWalkingAnimation)

	local MP5SDFireConnection = MP5SDFire.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		RecoilAnimation()
	end)

	local MP5SDFireLastConnection = MP5SDFireLast.OnClientEvent:Connect(function() -- IMPORTANT PART
		moveCameraUp()
		LastShotAnimation()
	end)
	local MP5SDReloadNotEmptyConnection = ReloadMP5SDNotEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadAnimation()
	end)
	local ReloadMP5SDEmptyConnection = ReloadMP5SDEmpty.OnClientEvent:Connect(function() -- IMPORTANT PART
		ReloadEmptyAnimation()
	end)

	table.insert(connections, runConnection)
	table.insert(connections, MP5SDFireConnection)
	table.insert(connections, MP5SDFireLastConnection)
	table.insert(connections, MP5SDReloadNotEmptyConnection)
	table.insert(connections, ReloadMP5SDEmptyConnection)
end

local tool = player.Backpack:WaitForChild("MP5SD")

tool.Unequipped:Connect(function()
	for _, connection in connections do
		-- We verify that the connection isn't nil or already disconnected
		if connection then
			connection:Disconnect()
		end
	end

	-- Clean the table's memory pointers
	connections = {}
end)
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)