How can i make reloading stop if the player unequips the gun

local players = game:GetService("Players")
local player = players.LocalPlayer
local playergui = player:FindFirstChild("PlayerGui")
local mouse = player:GetMouse()
local char = nil
local hum = nil
local ReloadAnim
local ReloadTrack = nil
local shotanim
local shotanimtrack = nil
local tool = script.Parent
local handle = tool:FindFirstChild("Handle")
local larrel = handle and handle:FindFirstChild("larrel")
local debounce = false
local userinput = game:GetService("UserInputService")
local ammo = 8
local maxAmmo = 8
local reloading = false
local jammed = false
local STOP = false 


local function reload ()
	if reloading == false then
		reloading = true
		ReloadAnim = tool:WaitForChild("Reload")
		ReloadTrack = hum:LoadAnimation(ReloadAnim)
		ReloadTrack:Play()
		local part = Instance.new("Part")
		part.Position = tool.Handle.Position
		part.Size = Vector3.new(0.201, 0.776, 0.32)
		part.CFrame = tool.Handle.CFrame * CFrame.Angles(0, math.rad(90), 0)
		part.Parent = workspace
		part.CanCollide = true
		part.BrickColor = BrickColor.new("Black")
		part.Material = Enum.Material.Metal
		script.Parent.Handle["Gun Reload"]:Play()
		tool.Parent:WaitForChild("run?").Value = false
		tool.Parent:WaitForChild("Humanoid").WalkSpeed = 8
		task.wait(2.1)
		local random = math.random(1,2)
		if random == 1 then
			print("gunnotjammed")
		else
			jammed = true
			script.Parent.Handle.JAMMED:Play()
			print("gungotjammed")
		end
		tool.Parent:WaitForChild("run?").Value = true
		tool.Parent:WaitForChild("Humanoid").WalkSpeed = 17
		ammo = maxAmmo
		player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Text = ""..ammo.."/"..maxAmmo
		player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Transparency = 0
		script.Parent.Handle.theslidle.Transparency = 1
		script.Parent.Handle.theslidle2.Transparency = 0
		script.Parent.Handle["Tactical Pistol Slide Release"]:Play()
		task.wait(.5)
		script.Parent.Handle.theslidle.Transparency = 0
		script.Parent.Handle.theslidle2.Transparency = 1
		if ReloadTrack then
			ReloadTrack:Stop()
		end
		reloading = false
		part:Destroy()
	end
end

-- Shooting



tool.Activated:Connect(function()
	if debounce == false and ammo > 0 and reloading == false and jammed == false and STOP == false then
		debounce = true 
		ammo -= 1
		local direction = (mouse.Hit.Position - tool.Handle.Barrel.Position).Unit
		tool.Shoot:FireServer(direction)
		if shotanimtrack then
			shotanimtrack:Play()
		end
		workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.Angles(math.rad(2.5),0,0)
		player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Text = ""..ammo.."/"..maxAmmo
		player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Transparency = 0
		task.wait(0.1)
		debounce = false
	elseif ammo <= 0 and reloading == false then
		reload()
	end
end)


tool.Equipped:Connect(function()
	player.PlayerGui.jammedgui.Frame.Visible = true
	mouse.Icon = "rbxasset://textures/GunCursor.png"
	player.PlayerGui.AmmoGui3.Enabled = true
	player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Transparency = 0
	player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Text = ""..ammo.."/"..maxAmmo
	shotanim = tool:WaitForChild("Animation")
	shotanimtrack = player.Character.Humanoid:LoadAnimation(shotanim)
	char = tool.Parent
	hum = char:WaitForChild("Humanoid")
	STOP= true
	task.wait(0.5)
	STOP = false
end)

tool.Unequipped:Connect(function()
	player.PlayerGui.jammedgui.Frame.Visible = false
	player.Character:WaitForChild("run?").Value = true
	player.Character:WaitForChild("Humanoid").WalkSpeed = 17
	reloading = false ----this isnt working some how
	script.Parent.Handle["Gun Reload"]:Stop()
	mouse.Icon = ""
	player.PlayerGui.AmmoGui3.Enabled = false
	player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Transparency = 1
	script.Parent.Handle["Gun Reload"]:Stop()
	hum = nil
	char = nil
	shotanim = nil
	shotanimtrack = nil
	if shotanim then
		shotanimtrack:Stop()
	end
	if ReloadTrack then
		ReloadTrack:Stop() ---animantions dont stop for some reason
	end
	if ReloadAnim then
		ReloadAnim = nil
	end
end)

ive tried making the reloading boolean to false but it didnt work

1 Like

I like how u’ve made a jamming system, I don’t see that often in games altho math.random(1, 2) might be a lil too extreme and at the same time there’s no built in fouling to influence jamming chance

anyway, about ur reloading issue, u’re not checking whether the player is still reloading when setting ammo to maxAmmo in the reload function

and animations don’t stop because u’re setting them to nil before stopping them

thanks and ho could i check if they are reloading tho do i make like a boolean checking they are reloading?..

u already have a reloading boolean so why not check if it’s still true in the reload function before setting ammo to maxAmmo?

hmmm…lemme do that real quick..

errrr i didnt really work i got an error

gunnotjammed - Client - LocalScriptglock:46
16:39:37.909 Infinite yield possible on ‘Players.largrgy.Backpack:WaitForChild(“run?”)’ - Studio
16:39:37.909 Stack Begin - Studio
16:39:37.910 Script ‘Workspace.largrgy.broken.LocalScriptglock’, Line 52 - function reload - Studio - LocalScriptglock:52
16:39:37.910 Script ‘Workspace.largrgy.broken.LocalScriptglock’, Line 146 - Studio - LocalScriptglock:146

i fixed it but the reload unequip thingy didn’t work

errrr what’s line 146 and also it’s not an error it’s just a warning

oh its thiz

I meant line 52 :sob: Idk why it’s so much harder for me to read ts if it’s not colored properly sorry

OOOOOO MA BADFF…

HERE YA GO-

ah yeah when u unequip ur gun u are reparenting the gun the player’s backpack so just add
if not reloading then return end before that line

sorry but where do i add that?

before line 52, the part where u have tool.Parent:WaitForChild('run?')

oh so like thiss?

1 Like

yep, u also don’t need the other reloading boolean check so just replace it with ammo = maxAmmo again

1 Like

o alrighty lemme test it out!!

yay it works thank you so much ive been trying to fix this for 1 year!!!

1 Like

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