Connecting reload animation with ContextActionService

I’ve watched multiple ContextActionService tutorials along with read the ROBLOX tutorial (which was the last helpful in all honesty.)

I’ve got the reload button working for mobile players - but it does not run my reload animation. I’ve tried connecting to different functions, placing the contextactionservice lines of code all around. No results.

Anybody familiar with this issue and possibly know a fix?

------------------------------------------Gun info
wait(1)
ToolName=script.Parent.Name
damage=16.5
ClipSize=30
ReloadTime=0
Firerate= 0
MinSpread=0.001
MaxSpread=0.0015
SpreadRate=0
Bulletdrop = 0.00001
automatic = true
burst = false
shot = false
patrolling = false
Tool = script.Parent
enabled = true 
local arms = nil
local torso = nil
local weld33 = nil -- right arm
local weld55 = nil -- left arm
local welds = {}

BarrlePos=Vector3.new(0,0,0)
Cursors={"http://www.roblox.com/asset/?id=47894837","rbxassetid://176067512"}
ReloadCursor="rbxasset://textures\\GunWaitCursor.png"

barrel_1  = script.Parent.FirePart

double = false	--Double Wielded 
doublemode = 1 -- 1 is alternating, 2 is both

-------------------------------------
Tool = script.Parent

repeat wait() until Tool.Parent:FindFirstChild("Humanoid")

local toPreload = {}

for _,child in next, Tool:GetChildren() do
	if child:IsA("Animation") then
		table.insert(toPreload, child)
	end
end

for _,child in next, game.ReplicatedStorage.GunStorage:GetChildren() do
	if child:IsA("Animation") then
		table.insert(toPreload, child)
	end
end

local toIgnore = {
	Tool.Parent,
	workspace.Bullets,
}

p = Instance.new("Part")
p.Parent = game.Lighting
p.Name = "BulletTexture"
p.CanCollide = false
p.formFactor = "Custom"
p.Size = Vector3.new(1,0.1,1)
p.Transparency = 1
g = Instance.new("SpecialMesh")
g.Parent = p

run = 0 
equipped=false
dw = false
sp=script.Parent
RayLength=1000
Spread=0.0001
enabled=true
reloading=false
down=false
mobile=false
r=game:service("RunService")
last=0
last2=0
last3=0
last4=0
last5=0
last6=0
UseDouble = false
fireRate = 60/750

function check()
	sp.Name = ToolName
end

function computeDirection(vec)
	return vec.unit
end

-----------------------------------------------------Raycasting functions
raycount = 0

function raycursive(start,dir,ignore)
	raycount = raycount + 1
	if raycount >= 10 then return end
	local random = Vector3.new(math.random(-Tool.Accuracy.Value, Tool.Accuracy.Value),math.random(-Tool.Accuracy.Value, Tool.Accuracy.Value),math.random(-Tool.Accuracy.Value, Tool.Accuracy.Value))
	local newdir = (start+dir)-start-random
	local hitPart,intersectingPoint = workspace:FindPartOnRay(Ray.new(start,newdir.unit*999.999),ignore)
		
	if not intersectingPoint then return end
	if hitPart and not hitPart.CanCollide and (hitPart.Name ~= "Left Arm" and hitPart.Name ~= "Left Leg" and hitPart.Name ~= "Right Arm" and hitPart.Name ~= "Right Leg") then
		return raycursive(intersectingPoint,dir,hitPart)
	else
		return hitPart,intersectingPoint
	end
end

-------------------------------------
local legs = nil
local torso2 = nil
local welds2 = {}
local bodyforce = nil
sprinting = false

function reload(mouse)
	if sprinting == true then return end
	reloading=true
	mouse.Icon=ReloadCursor
	wait(3)
	while sp.Ammo.Value<ClipSize and reloading and enabled do
		wait()
		if reloading then
			sp.Ammo.Value= 30
			local ui = game.Players.LocalPlayer.PlayerGui.GameGui
			ui.GameGui.AmmoFrame.Ammo.Text = sp.Ammo.Value
			check()
		else
			break
		end
	end
	check()
	mouse.Icon=Cursors[1]
	reloading=false
end
function reload_Mob()
	if sprinting == true then return end
	reloading=true
	wait(3)
	while sp.Ammo.Value<ClipSize and reloading and enabled do
		wait()
		if reloading then
			sp.Ammo.Value= 30
			local ui = game.Players.LocalPlayer.PlayerGui.GameGui
			ui.GameGui.AmmoFrame.Ammo.Text = sp.Ammo.Value
		else
			break
		end
	end
	reloading=false
	mobile=true
end


function onButtonPress()
	--wait(0.1)	
	--if onButtonPress() and not reloading then
	if script.Parent.Ammo.Value < 30 then
		wait(0.1)
		reload_Mob()
	end
end


	
PatrolAnim = Tool.Parent.Humanoid:LoadAnimation(game.ReplicatedStorage.GunStorage.PatrolAnim)
PatrolAnim.Priority = Enum.AnimationPriority.Action
ReloadAnimation = Tool.Parent.Humanoid:LoadAnimation(script.Parent.ReloadAnim)
ReloadAnimation.Priority = Enum.AnimationPriority.Action

 

function onKeyDown(key,mouse)
	key=key:lower()
	if key=="r"  and not reloading and not sprinting then
		ReloadAnimation:Play()
		ReloadAnimation:GetMarkerReachedSignal("MagOut"):Connect(function()
			script.Parent.Handle.MagOut:Play()
		end)
		ReloadAnimation:GetMarkerReachedSignal("MagIn"):Connect(function()
			script.Parent.Handle.MagIn:Play()
		end)
		ReloadAnimation:GetMarkerReachedSignal("BoltForward"):Connect(function()
			script.Parent.Handle.BoltForward:Play()
		end)
		reload(mouse)
	elseif mobile==true and not reloading and not sprinting then
		ReloadAnimation:Play()
		ReloadAnimation:GetMarkerReachedSignal("MagOut"):Connect(function()
			script.Parent.Handle.MagOut:Play()
		end)
		ReloadAnimation:GetMarkerReachedSignal("MagIn"):Connect(function()
			script.Parent.Handle.MagIn:Play()
		end)
		ReloadAnimation:GetMarkerReachedSignal("BoltForward"):Connect(function()
			script.Parent.Handle.BoltForward:Play()
		end)
		reload(mouse)
	elseif key == "p" and not reloading then
		if patrolling == false then
			patrolling = true
			enabled = false
			PatrolAnim:Play()
			IdleAnimation:Stop()
		elseif patrolling == true then	
			patrolling = false
			PatrolAnim:Stop()
			enabled = true
			IdleAnimation:Play()		
		end
	end
end



local UIS = game:GetService("UserInputService")

SprintAnim = Tool.Parent.Humanoid:LoadAnimation(game.ReplicatedStorage.GunStorage.SprintAnim)
SprintAnim.Priority = Enum.AnimationPriority.Action

UIS.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.LeftShift and equipped and not reloading then
		if sprinting == false then
			sprinting = true

			SprintAnim:Play()
			game.ReplicatedStorage.SprintEvent:FireServer(32)
		end
	end
end)

UIS.InputEnded:Connect(function(input,gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.LeftShift  and equipped and not reloading then
		if sprinting == true then
			sprinting = false
			SprintAnim:Stop()
			game.ReplicatedStorage.SprintEvent:FireServer(16)
		end
	end
end)

function movecframe(p,pos)
	p.Parent=game.Lighting
	p.Position=pos
	p.Parent=game.Workspace
end
fireAnimation = Tool.Parent.Humanoid:LoadAnimation(script.Parent.FiringAnim)
fireAnimation.Priority = Enum.AnimationPriority.Movement


function fire(aim, mouse)
	
	if sprinting then return end
	
	script.Parent.FireFX:FireServer(fireRate)

	t=r.Stepped:wait()

	local startpoint = Tool.FirePart
	
	local totalDist  = 0
	local startpoint = barrel_1.CFrame.p

	local dir=(aim)-startpoint
	dir=dir.unit
	
	local cfrm=CFrame.new(startpoint, dir+startpoint)
	raycount = 0
	local part,position=raycursive(startpoint, cfrm.lookVector * 999)
	
	if part~=nil then
		
		if part.Parent:FindFirstChild("Middle") then
			part = part.Parent	
		elseif part.Parent.className == "Hat" then
			part = part.Parent
		elseif part.Parent.className == "Tool" then
			part = part.Parent
		end
		
		local humanoid= part.Parent:FindFirstChild("Humanoid")
		
		if not humanoid then
			game.ReplicatedStorage.BulletEvent:FireServer(position, part, mouse.TargetSurface)
		end

		if humanoid and part.Parent ~= Tool.Parent then
			if part.Name == "Head" and not part.Parent:FindFirstChild("Middle") then
				mouse.Icon = Cursors[2]
				local hitmarkerSound = game.ReplicatedStorage.GunStorage.Hitmarker:Clone()
				hitmarkerSound.Parent = workspace.CurrentCamera
				hitmarkerSound:Play()
				game.ReplicatedStorage.GunEvent:FireServer(humanoid,damage*2)
			elseif part.Name ~= "Head" and not part.Parent:FindFirstChild("Middle") then
				mouse.Icon = Cursors[2]
				local hitmarkerSound = game.ReplicatedStorage.GunStorage.Hitmarker:Clone()
				hitmarkerSound.Parent = workspace.CurrentCamera
				hitmarkerSound:Play()
				game.ReplicatedStorage.GunEvent:FireServer(humanoid,damage)
			elseif part.Parent:FindFirstChild("Middle") and part.Name ~= "Head" then
				mouse.Icon = Cursors[2]
				local hitmarkerSound = game.ReplicatedStorage.GunStorage.Hitmarker:Clone()
				hitmarkerSound.Parent = workspace.CurrentCamera
				hitmarkerSound:Play()
				game.ReplicatedStorage.GunEvent:FireServer(humanoid,damage/2)			
			end
		end
	end	

	if not fireAnimation.IsPlaying == true then
		fireAnimation:Play()
	end

	local deb = game:GetService("Debris")
	check()
	wait(fireRate)
	mouse.Icon = Cursors[1]
end

function onButton1Up(mouse)
	down=false
end

function onButton1Down(mouse)
	
	h=sp.Parent:FindFirstChild("Humanoid")
	if not enabled or reloading or down or h==nil then
		return
	end
	if sp.Ammo.Value>0 and h.Health>0 and h.Jump == false then
		down=true
		enabled=false

		while down and h.Jump == false and h.Health>0 do
			if sp.Ammo.Value<=0 then
				break
			end
			
			Tool.Accuracy.Value = 0.01
	
			automatic = true
					
			mouse.TargetFilter = game.Players.LocalPlayer.Character
	
			sp.Ammo.Value=sp.Ammo.Value-1
			local ui = game.Players.LocalPlayer.PlayerGui.GameGui
			ui.GameGui.AmmoFrame.Ammo.Text = sp.Ammo.Value
			local startpoint=barrel_1.Position
			local mag=(mouse.hit.p-startpoint).magnitude
			local rndm=Vector3.new(math.random(-Tool.Accuracy.Value,Tool.Accuracy.Value), math.random(-Tool.Accuracy.Value,Tool.Accuracy.Value ),math.random(-Tool.Accuracy.Value,Tool.Accuracy.Value))
			fire(mouse.hit.p+rndm, mouse)
			
		wait(Firerate)
	end	
		
		enabled=true
		
	else
		
		wait()
		
	end
end

local aiming = false


IdleAnimation = Tool.Parent.Humanoid:LoadAnimation(script.Parent.IdleAnim)
IdleAnimation.Priority = Enum.AnimationPriority.Movement

function onEquippedLocal(mouse)

if Tool.Parent.Humanoid.SeatPart ~= nil then
	Tool.Parent.Humanoid:UnequipTools()
end

script.Parent.Motor:FireServer("Make")

enabled = true

IdleAnimation:Play()

local ui = script.GameGui:Clone()
ui.Parent = game.Players.LocalPlayer.PlayerGui
ui.GameGui.AmmoFrame.Title.Text = Tool.Name
	
arms = {Tool.Parent:FindFirstChild("Left Arm"), Tool.Parent:FindFirstChild("Right Arm")}
torso = Tool.Parent:FindFirstChild("Torso")
	if mouse==nil then
		print("Mouse not found")
		return 
	end
	local ContextActionService = game:GetService("ContextActionService")
	local mobilebutton = ContextActionService:BindAction("ReloadButton",onButtonPress,true,"r")
	Mouse = mouse
	mouse.Icon=Cursors[1]
	mouse.KeyDown:connect(function(key) onKeyDown(key,mouse) end)
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)
	mouse.Button1Up:connect(function() onButton1Up(mouse) end)
	check()
	equipped=true
	if #Cursors>1 then
		while equipped do
			t=r.Stepped:wait()
			
			wait(Firerate*.9)
		end
	end
end
function onUnequippedLocal(mouse)
	if game.Players.LocalPlayer.PlayerGui:FindFirstChild("GameGui") then
		game.Players.LocalPlayer.PlayerGui:FindFirstChild("GameGui"):Destroy()
	end
	IdleAnimation:Stop()
	if PatrolAnim then
		PatrolAnim:Stop()
	end
	if SprintAnim then
		SprintAnim:Stop()
	end
	equipped=false
	reloading=false
end

sp.Equipped:connect(onEquippedLocal)
sp.Unequipped:connect(onUnequippedLocal)
check()
1 Like

If you read your code, it would be easy to see that you never actually run an animation for mobile clients. You check inside the keydown function if they’re on mobile but, if you’re a mobile player, you don’t exactly have a way to call the keydown function therefore you never check if they’re mobile.

I am pretty sure that you are going to have to create a GUI system that only loads on Mobile Clients, and have a Reload Button that connects the Reload function.

Edit:
It would probably be best if you made a script to handle the scripts that you have already made, and only loads certain scripts based on the players platform.

That way you do not have to completely remake your current scripts.

UPDATE

I had the sound working for the gun @ one point; but wasn’t waiting appropriate time before playing

I now have it back to normal where it is animating and reloading properly - just the sound isn’t playing?

REPLICATING

The issue with no sound for animation is not only broken on PC - but on mobile as well. 7:48 [PM] - 12/19/20

CODE EDITTED:

function reload(mouse)
	if sprinting == true then return end
	reloading=true
	mouse.Icon=ReloadCursor
	wait(3)
	while sp.Ammo.Value<ClipSize and reloading and enabled do
		wait()
		if reloading then
			sp.Ammo.Value= 30
			local ui = game.Players.LocalPlayer.PlayerGui.GameGui
			ui.GameGui.AmmoFrame.Ammo.Text = sp.Ammo.Value
			check()
		else
			break
		end
	end
	check()
	mouse.Icon=Cursors[1]
	reloading=false
end
function reload_mob()
	if sprinting == true then return end
	reloading=true
	wait(3)
	while sp.Ammo.Value<ClipSize and reloading and enabled do
		wait()
		if reloading then
			sp.Ammo.Value= 30
			local ui = game.Players.LocalPlayer.PlayerGui.GameGui
			ui.GameGui.AmmoFrame.Ammo.Text = sp.Ammo.Value
			check()
		else
			break
		end
	end
	check()
	reloading=false
end


PatrolAnim = Tool.Parent.Humanoid:LoadAnimation(game.ReplicatedStorage.GunStorage.PatrolAnim)
PatrolAnim.Priority = Enum.AnimationPriority.Action
ReloadAnimation = Tool.Parent.Humanoid:LoadAnimation(script.Parent.ReloadAnim)
ReloadAnimation.Priority = Enum.AnimationPriority.Action

function onKeyDown(key,mouse)
	key=key:lower()
	if key=="r" and not reloading and not sprinting then
		ReloadAnimation:Play()
		ReloadAnimation:GetMarkerReachedSignal("MagOut"):Connect(function()
			script.Parent.Handle.MagOut:Play()
		end)
		ReloadAnimation:GetMarkerReachedSignal("MagIn"):Connect(function()
			script.Parent.Handle.MagIn:Play()
		end)
		ReloadAnimation:GetMarkerReachedSignal("BoltForward"):Connect(function()
			script.Parent.Handle.BoltForward:Play()
		end)
		reload(mouse)
	elseif key == "p" and not reloading then
		if patrolling == false then
			patrolling = true
			enabled = false
			PatrolAnim:Play()
			IdleAnimation:Stop()
		elseif patrolling == true then	
			patrolling = false
			PatrolAnim:Stop()
			enabled = true
			IdleAnimation:Play()		
		end
	end
end


function onButtonPress()
	if script.Parent.Ammo.Value < 30 then
		wait(0.1)
		if not reloading and not sprinting then
			ReloadAnimation:Play()
			ReloadAnimation:GetMarkerReachedSignal("MagOut"):Connect(function()
				script.Parent.Handle.MagOut:Play()
			end)
			ReloadAnimation:GetMarkerReachedSignal("MagIn"):Connect(function()
				script.Parent.Handle.MagIn:Play()
			end)
			ReloadAnimation:GetMarkerReachedSignal("BoltForward"):Connect(function()
				script.Parent.Handle.BoltForward:Play()
			end)
	reload_mob()
	end
	end
	end

local mobilebutton = ContextActionService:BindAction("ReloadButton",onButtonPress,true,"r") 

FULL SCRIPT:

wait(1)
ToolName=script.Parent.Name
damage=16.5
ClipSize=30
ReloadTime=0
Firerate= 0
MinSpread=0.001
MaxSpread=0.0015
SpreadRate=0
Bulletdrop = 0.00001
automatic = true
burst = false
shot = false
patrolling = false
Tool = script.Parent
enabled = true 
local arms = nil
local torso = nil
local weld33 = nil -- right arm
local weld55 = nil -- left arm
local welds = {}

local ContextActionService = game:GetService("ContextActionService")


BarrlePos=Vector3.new(0,0,0)
Cursors={"http://www.roblox.com/asset/?id=47894837","rbxassetid://176067512"}
ReloadCursor="rbxasset://textures\\GunWaitCursor.png"

barrel_1  = script.Parent.FirePart

double = false	--Double Wielded 
doublemode = 1 -- 1 is alternating, 2 is both

-------------------------------------
Tool = script.Parent

repeat wait() until Tool.Parent:FindFirstChild("Humanoid")

local toPreload = {}

for _,child in next, Tool:GetChildren() do
	if child:IsA("Animation") then
		table.insert(toPreload, child)
	end
end

for _,child in next, game.ReplicatedStorage.GunStorage:GetChildren() do
	if child:IsA("Animation") then
		table.insert(toPreload, child)
	end
end

local toIgnore = {
	Tool.Parent,
	workspace.Bullets,
}

p = Instance.new("Part")
p.Parent = game.Lighting
p.Name = "BulletTexture"
p.CanCollide = false
p.formFactor = "Custom"
p.Size = Vector3.new(1,0.1,1)
p.Transparency = 1
g = Instance.new("SpecialMesh")
g.Parent = p

run = 0 
equipped=false
dw = false
sp=script.Parent
RayLength=1000
Spread=0.0001
enabled=true
reloading=false
down=false
r=game:service("RunService")
last=0
last2=0
last3=0
last4=0
last5=0
last6=0
UseDouble = false
fireRate = 60/750

function check()
	sp.Name = ToolName
end

function computeDirection(vec)
	return vec.unit
end

-----------------------------------------------------Raycasting functions
raycount = 0

function raycursive(start,dir,ignore)
	raycount = raycount + 1
	if raycount >= 10 then return end
	local random = Vector3.new(math.random(-Tool.Accuracy.Value, Tool.Accuracy.Value),math.random(-Tool.Accuracy.Value, Tool.Accuracy.Value),math.random(-Tool.Accuracy.Value, Tool.Accuracy.Value))
	local newdir = (start+dir)-start-random
	local hitPart,intersectingPoint = workspace:FindPartOnRay(Ray.new(start,newdir.unit*999.999),ignore)

	if not intersectingPoint then return end
	if hitPart and not hitPart.CanCollide and (hitPart.Name ~= "Left Arm" and hitPart.Name ~= "Left Leg" and hitPart.Name ~= "Right Arm" and hitPart.Name ~= "Right Leg") then
		return raycursive(intersectingPoint,dir,hitPart)
	else
		return hitPart,intersectingPoint
	end
end

-------------------------------------
local legs = nil
local torso2 = nil
local welds2 = {}
local bodyforce = nil
sprinting = false

function reload(mouse)
	if sprinting == true then return end
	reloading=true
	mouse.Icon=ReloadCursor
	wait(3)
	while sp.Ammo.Value<ClipSize and reloading and enabled do
		wait()
		if reloading then
			sp.Ammo.Value= 30
			local ui = game.Players.LocalPlayer.PlayerGui.GameGui
			ui.GameGui.AmmoFrame.Ammo.Text = sp.Ammo.Value
			check()
		else
			break
		end
	end
	check()
	mouse.Icon=Cursors[1]
	reloading=false
end
function reload_mob()
	if sprinting == true then return end
	reloading=true
	wait(3)
	while sp.Ammo.Value<ClipSize and reloading and enabled do
		wait()
		if reloading then
			sp.Ammo.Value= 30
			local ui = game.Players.LocalPlayer.PlayerGui.GameGui
			ui.GameGui.AmmoFrame.Ammo.Text = sp.Ammo.Value
			check()
		else
			break
		end
	end
	check()
	reloading=false
end


PatrolAnim = Tool.Parent.Humanoid:LoadAnimation(game.ReplicatedStorage.GunStorage.PatrolAnim)
PatrolAnim.Priority = Enum.AnimationPriority.Action
ReloadAnimation = Tool.Parent.Humanoid:LoadAnimation(script.Parent.ReloadAnim)
ReloadAnimation.Priority = Enum.AnimationPriority.Action

function onKeyDown(key,mouse)
	key=key:lower()
	if key=="r" and not reloading and not sprinting then
		ReloadAnimation:Play()
		ReloadAnimation:GetMarkerReachedSignal("MagOut"):Connect(function()
			script.Parent.Handle.MagOut:Play()
		end)
		ReloadAnimation:GetMarkerReachedSignal("MagIn"):Connect(function()
			script.Parent.Handle.MagIn:Play()
		end)
		ReloadAnimation:GetMarkerReachedSignal("BoltForward"):Connect(function()
			script.Parent.Handle.BoltForward:Play()
		end)
		reload(mouse)
	elseif key == "p" and not reloading then
		if patrolling == false then
			patrolling = true
			enabled = false
			PatrolAnim:Play()
			IdleAnimation:Stop()
		elseif patrolling == true then	
			patrolling = false
			PatrolAnim:Stop()
			enabled = true
			IdleAnimation:Play()		
		end
	end
end


function onButtonPress()
	if script.Parent.Ammo.Value < 30 then
		wait(0.1)
		if not reloading and not sprinting then
			ReloadAnimation:Play()
			ReloadAnimation:GetMarkerReachedSignal("MagOut"):Connect(function()
				script.Parent.Handle.MagOut:Play()
			end)
			ReloadAnimation:GetMarkerReachedSignal("MagIn"):Connect(function()
				script.Parent.Handle.MagIn:Play()
			end)
			ReloadAnimation:GetMarkerReachedSignal("BoltForward"):Connect(function()
				script.Parent.Handle.BoltForward:Play()
			end)
	reload_mob()
	end
	end
	end

local mobilebutton = ContextActionService:BindAction("ReloadButton",onButtonPress,true,"r") 


local UIS = game:GetService("UserInputService")

SprintAnim = Tool.Parent.Humanoid:LoadAnimation(game.ReplicatedStorage.GunStorage.SprintAnim)
SprintAnim.Priority = Enum.AnimationPriority.Action

UIS.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.LeftShift and equipped and not reloading then
		if sprinting == false then
			sprinting = true

			SprintAnim:Play()
			game.ReplicatedStorage.SprintEvent:FireServer(32)
		end
	end
end)

UIS.InputEnded:Connect(function(input,gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.LeftShift  and equipped and not reloading then
		if sprinting == true then
			sprinting = false
			SprintAnim:Stop()
			game.ReplicatedStorage.SprintEvent:FireServer(16)
		end
	end
end)

function movecframe(p,pos)
	p.Parent=game.Lighting
	p.Position=pos
	p.Parent=game.Workspace
end
fireAnimation = Tool.Parent.Humanoid:LoadAnimation(script.Parent.FiringAnim)
fireAnimation.Priority = Enum.AnimationPriority.Movement


function fire(aim, mouse)

	if sprinting then return end

	script.Parent.FireFX:FireServer(fireRate)

	t=r.Stepped:wait()

	local startpoint = Tool.FirePart

	local totalDist  = 0
	local startpoint = barrel_1.CFrame.p

	local dir=(aim)-startpoint
	dir=dir.unit

	local cfrm=CFrame.new(startpoint, dir+startpoint)
	raycount = 0
	local part,position=raycursive(startpoint, cfrm.lookVector * 999)

	if part~=nil then

		if part.Parent:FindFirstChild("Middle") then
			part = part.Parent	
		elseif part.Parent.className == "Hat" then
			part = part.Parent
		elseif part.Parent.className == "Tool" then
			part = part.Parent
		end

		local humanoid= part.Parent:FindFirstChild("Humanoid")

		if not humanoid then
			game.ReplicatedStorage.BulletEvent:FireServer(position, part, mouse.TargetSurface)
		end

		if humanoid and part.Parent ~= Tool.Parent then
			if part.Name == "Head" and not part.Parent:FindFirstChild("Middle") then
				mouse.Icon = Cursors[2]
				local hitmarkerSound = game.ReplicatedStorage.GunStorage.Hitmarker:Clone()
				hitmarkerSound.Parent = workspace.CurrentCamera
				hitmarkerSound:Play()
				game.ReplicatedStorage.GunEvent:FireServer(humanoid,damage*2)
			elseif part.Name ~= "Head" and not part.Parent:FindFirstChild("Middle") then
				mouse.Icon = Cursors[2]
				local hitmarkerSound = game.ReplicatedStorage.GunStorage.Hitmarker:Clone()
				hitmarkerSound.Parent = workspace.CurrentCamera
				hitmarkerSound:Play()
				game.ReplicatedStorage.GunEvent:FireServer(humanoid,damage)
			elseif part.Parent:FindFirstChild("Middle") and part.Name ~= "Head" then
				mouse.Icon = Cursors[2]
				local hitmarkerSound = game.ReplicatedStorage.GunStorage.Hitmarker:Clone()
				hitmarkerSound.Parent = workspace.CurrentCamera
				hitmarkerSound:Play()
				game.ReplicatedStorage.GunEvent:FireServer(humanoid,damage/2)			
			end
		end
	end	

	if not fireAnimation.IsPlaying == true then
		fireAnimation:Play()
	end

	local deb = game:GetService("Debris")
	check()
	wait(fireRate)
	mouse.Icon = Cursors[1]
end

function onButton1Up(mouse)
	down=false
end

function onButton1Down(mouse)

	h=sp.Parent:FindFirstChild("Humanoid")
	if not enabled or reloading or down or h==nil then
		return
	end
	if sp.Ammo.Value>0 and h.Health>0 and h.Jump == false then
		down=true
		enabled=false

		while down and h.Jump == false and h.Health>0 do
			if sp.Ammo.Value<=0 then
				break
			end

			Tool.Accuracy.Value = 0.01

			automatic = true

			mouse.TargetFilter = game.Players.LocalPlayer.Character

			sp.Ammo.Value=sp.Ammo.Value-1
			local ui = game.Players.LocalPlayer.PlayerGui.GameGui
			ui.GameGui.AmmoFrame.Ammo.Text = sp.Ammo.Value
			local startpoint=barrel_1.Position
			local mag=(mouse.hit.p-startpoint).magnitude
			local rndm=Vector3.new(math.random(-Tool.Accuracy.Value,Tool.Accuracy.Value), math.random(-Tool.Accuracy.Value,Tool.Accuracy.Value ),math.random(-Tool.Accuracy.Value,Tool.Accuracy.Value))
			fire(mouse.hit.p+rndm, mouse)

			wait(Firerate)
		end	

		enabled=true

	else

		wait()

	end
end

local aiming = false


IdleAnimation = Tool.Parent.Humanoid:LoadAnimation(script.Parent.IdleAnim)
IdleAnimation.Priority = Enum.AnimationPriority.Movement

function onEquippedLocal(mouse)

	if Tool.Parent.Humanoid.SeatPart ~= nil then
		Tool.Parent.Humanoid:UnequipTools()
	end

	script.Parent.Motor:FireServer("Make")

	enabled = true

	IdleAnimation:Play()

	local ui = script.GameGui:Clone()
	ui.Parent = game.Players.LocalPlayer.PlayerGui
	ui.GameGui.AmmoFrame.Title.Text = Tool.Name

	arms = {Tool.Parent:FindFirstChild("Left Arm"), Tool.Parent:FindFirstChild("Right Arm")}
	torso = Tool.Parent:FindFirstChild("Torso")
	if mouse==nil then
		print("Mouse not found")
		return 
	end
	local Mouse = mouse
	mouse.Icon=Cursors[1]
	mouse.KeyDown:connect(function(key) onKeyDown(key,mouse) end)
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)
	mouse.Button1Up:connect(function() onButton1Up(mouse) end)
	check()
	equipped=true
	if #Cursors>1 then
		while equipped do
			t=r.Stepped:wait()

			wait(Firerate*.9)
		end
	end
end
function onUnequippedLocal(mouse)
	if game.Players.LocalPlayer.PlayerGui:FindFirstChild("GameGui") then
		game.Players.LocalPlayer.PlayerGui:FindFirstChild("GameGui"):Destroy()
	end
	IdleAnimation:Stop()
	if PatrolAnim then
		PatrolAnim:Stop()
	end
	if SprintAnim then
		SprintAnim:Stop()
	end
	equipped=false
	reloading=false
end

sp.Equipped:connect(onEquippedLocal)
sp.Unequipped:connect(onUnequippedLocal)
check()

As in when it fires correct, if so, try debugging to see if reaches the point where it plays the sound, also make sure your volume is on as a precaution.