So im trying to make a gun system but theres one issue.
When i reequip my gun all the remoteevents multiple which causes the gun to have more recoil and deal more damage than intended
So i wanna disconnect all the remoteevents when i unequip my gun.
Heres the localscript.
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://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() -- 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)
-- 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
local function onToolEquipped(tool)
if tool.Name == "MP5SD" then
setupViewModel()
end
end
-- Connect the tool equipped event
player.Character.ChildAdded:Connect(function(child) -- IMPORTANT PART (i guess)
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 -- IMPORTANT PART
setupViewModel()
end
end)
All the important parts are higlighted with a tag saying IMPORTANT PART