I’m currently making a gun system, and I want to make the player cancel a reload function by leftclicking during the reload. How do I detect if the player leftclicks during the reload and cancel it?
local function reload()
reloading = true
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
if ammo == 0 then
script.Parent.Reload:FireServer()
previousammo = ammo
wait(script.Parent.Handle.rs.TimeLength)
if ammoleft.Value >= maxammo then
ammo = maxammo
ammoleft.Value = ammoleft.Value - maxammo
else
ammo = previousammo + ammoleft.Value
ammoleft.Value = 0
end
else
script.Parent.Reload2:FireServer()
previousammo = ammo
wait(script.Parent.Handle.rs.TimeLength - 0.444)
if ammoleft.Value >= maxammo then
ammo = maxammo + 1
ammoleft.Value = ammoleft.Value - maxammo + previousammo - 1
elseif previousammo + ammoleft.Value >= 31 then
ammo = 31
ammoleft.Value = ammoleft.Value - maxammo + previousammo - 1
else
ammo = previousammo + ammoleft.Value
ammoleft.Value = 0
end
end
plr.PlayerGui.GunStatus.Frame.AmmoLeft.Text = ammoleft.Value
plr.PlayerGui.GunStatus.Frame.MagAmmo.Text = ammo
plr.PlayerGui.GunStatus.Frame.MagAmmo.TextColor3 = Color3.fromRGB(255, 255, 255)
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
reloading = false
end
local Player = Game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local function reload()
reloading = true
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
if ammo == 0 and reloading == true then
script.Parent.Reload:FireServer()
previousammo = ammo
wait(script.Parent.Handle.rs.TimeLength)
if ammoleft.Value >= maxammo then
ammo = maxammo
ammoleft.Value = ammoleft.Value - maxammo
else
ammo = previousammo + ammoleft.Value
ammoleft.Value = 0
end
else
if reloading == true then
script.Parent.Reload2:FireServer()
previousammo = ammo
wait(script.Parent.Handle.rs.TimeLength - 0.444)
if ammoleft.Value >= maxammo then
ammo = maxammo + 1
ammoleft.Value = ammoleft.Value - maxammo + previousammo - 1
elseif previousammo + ammoleft.Value >= 31 then
ammo = 31
ammoleft.Value = ammoleft.Value - maxammo + previousammo - 1
else
ammo = previousammo + ammoleft.Value
ammoleft.Value = 0
end
end
end
plr.PlayerGui.GunStatus.Frame.AmmoLeft.Text = ammoleft.Value
plr.PlayerGui.GunStatus.Frame.MagAmmo.Text = ammo
plr.PlayerGui.GunStatus.Frame.MagAmmo.TextColor3 = Color3.fromRGB(255, 255, 255)
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
reloading = false
end
Mouse.Button1Down:Connect(function()
reloading = false
end)
You can try this, it checks if reloading is still true before every event, I recommend you check it before playing animations.
After reading your post, I see that the your version of the script checks if the player is reloading before the reload action, and I don’t think that it can cancel the reload while the function runs. The script I provided does not reload instantly, but waits for a little bit of time before letting the player complete the reload.
I wonder if there’s a way to check if the player clicked during the time the script waits?