How can i cancel the reload if gun is unequipped during a reload?

--Not sure on how to do this this is very confusing for me.    

local RunService = game:GetService("RunService")
    local UIS = game:GetService("UserInputService")
    local player = game.Players.LocalPlayer
    local mouse = player:GetMouse()
    local gun = script.Parent
    local db = true
    local equipped = false
    local shooting = false
    local value = script.Parent:WaitForChild("Value")
    local MAX_VALUE = 30
    local rld = 5.5
    local debounce = false
    local Up = false
    local Players = game:GetService('Players') -- get the players service

    local player = Players.LocalPlayer -- the client

    local tool = script.Parent -- get the tool
    local value = tool:WaitForChild('Value')

    local playerGui = player.PlayerGui -- get the playergui

    local gui = playerGui:WaitForChild('Ammo') 
    local text = gui:WaitForChild('Frame'):WaitForChild("TextLabel") 

    gun.Activated:Connect(function()
    	local Connection
    	local function StartFiring(Input, Chatted)
    		if Chatted then
    			return
    		end
    		
    		if Input.UserInputType == Enum.UserInputType.MouseButton1 then
    			shooting = true
    		end
    	end
    	
    	UIS.InputEnded:Connect(function(Input, Chatted)
    		if Chatted then
    			return
    		end

    		if Input.UserInputType == Enum.UserInputType.MouseButton1 then
    			shooting = false
    			Connection:Disconnect()
    		end

    	end)
    	
    	Connection = UIS.InputBegan:Connect(StartFiring)
    end)

    gun.Unequipped:Connect(function()
    	shooting = false
    end)

    while true do
    	if shooting == true then
    		script.Parent.Fire:FireServer(mouse.Hit.Position)
    		script.Parent.Muzzle.MuzzleEffect.Transparency = NumberSequence.new(0)
    		workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.Angles(math.rad(0.55),0,0)
    		wait(0.05)
    		script.Parent.Muzzle.MuzzleEffect.Transparency = NumberSequence.new(1)

    		if value.Value == MAX_VALUE then
    			value.Value +=1
    			text.Text = value.Value
    			
    			print("clicked 10 times. resting value")
    			value.Value = 0
    			local Reload = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Reload)
    			Reload:Play()
    			wait(rld)
    			text.Text = value.Value
    		else
    				value.Value += 1
    			text.Text = value.Value 
    		
    			local shoot = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.shoot)
    			shoot:Play()
    			print("clicked times: "..tostring(value.Value))
    		end
    		wait(.08)
    	end
    	RunService.RenderStepped:Wait()
    end
1 Like

Please be more descriptive with your posts, and include descriptive text of what you are trying to achieve. Do not expect people to read your title and spoon feed you answers.

Anyway, besides that, you could possibly detect unequiption through a connection
to when the weapon is being unequipped. Then set a variable that the gun has been unequipped to true, and stop the reloading.

Example:

local unEquipped = false -- Setting a variable so we can detect if the tool has been unequipped
local connection -- Setting up a prior variable for connection so we can use it later

connection = tool.Unequipped:Connect(function() -- Detecting unequiption
 reloadAnim:Stop() -- Stopping the reload animation
 unEquipped = true -- Setting the unequip variable to true
 connection:Disconnect() -- Disabling the connection so it doesn't fire again.
end)

wait(rld)
if not unEquipped  then -- Has the gun not been unequipped?
 connection:Disconnect() -- Preventing too many unequip triggers lagging the game.
-- Reload
end

How would i place that in my code im a bit confused.

Kind of like this:

local Reload = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Reload)
Reload:Play()

local unEquipped = false
local connection

connection = tool.Unequipped:Connect(function()
 Reload:Stop()
 unEquipped = true
 connection:Disconnect()
end)

wait(rld)
if not unEquipped  then
 connection:Disconnect()
 text.Text = value.Value
end

where would i place it still a bit confused.

i kinda need to see it in the full script

I would use a variable, upon unequipping set its value to false. You could try using a loop:

local Tool = script.Parent

local IsEquipped = false
local RELOADING = false

local function Reload()
    if IsEquipped and not RELOADING then
        RELOADING = true
        coroutine.wrap(function()
            while IsEquipped and RELOADING do
                -- code
            end
        end)()
    end
end

Tool.Equipped:Connect(function()
    IsEquipped = true
end)

Tool.Unequipped:Connect(function()
    IsEquipped = false
    RELOADING = false
end)
1 Like

Check if the item is under the player’s character after the reloading wait. I have no idea where the reloading code is in your post but here’s an example.

function Reload()
--start reload
wait(5) --reload duration
if Tool.Parent = Player.Character then --check if the gun is equipped
--reload the gun
end
end

i have no idea where to put that in my code?

it would be nice if you could show me the full code with that

Quote from another topic asking the same question: