Game freezes when clicking while reloading

So in my gun with this local script, the entire game freezes when I click while the gun is reloading and forces me to open task manager to force close the game.

I have already tried changing the reload wait time, removing the reload sound and even adding a TextButton Gui that covers the whole screen, which worked mostly but I couldn’t right click and it just seemed very impractical.

If anyone can help me out and see if there’s any fix in the script, that’d be greatly appreciated

local gun = script.Parent
local re = script.Parent:WaitForChild("RemoteEvent")
local player = nil
local mouse = nil
local connection = nil
local disconnection = nil
local canShoot = true
local coolDown = 0

local rs = game:GetService("ReplicatedStorage")
local ammoGuiCloning = script.Parent:WaitForChild("AmmoGui")
local ammoGui = nil
local ammoLabel = nil

local blockGuiCloning = script.Parent:WaitForChild("BlockClick")
local blockGui = nil

local config = script.Parent:WaitForChild("Configuration")
local ammo = 30
local maxAmmo = 30

local click = script.Parent.Gun:WaitForChild("GunClick")

local reloadSound = script.Parent.Gun:WaitForChild("GunReload")
local isReloading = false
local uis = game:GetService("UserInputService")local reloadKey = Enum.KeyCode.R

local isShooting = nil

local function reload()
	isReloading = true
	reloadSound:Play()
	wait(3.3)
	ammo = maxAmmo
	ammoLabel.Text = "" .. ammo .. "/" .. maxAmmo
	isReloading = false
end

local function shoot()
	while isShooting do
		if canShoot and ammo > 0 and not isReloading then
			canShoot = false
			re:FireServer(mouse.Target)
			ammo -= 1
			ammoLabel.Text = "" .. ammo .. "/" .. maxAmmo
			wait(0.15)
			canShoot = true
		elseif ammo == 0 and not isReloading then
			-- click:Play()
			reload()
		end
	end
end

local function makeAmmoGui()
	local playerGui = player:WaitForChild("PlayerGui")
	ammoGui = playerGui:FindFirstChild("AmmoGui")
	if not ammoGui then
		ammoGui = ammoGuiCloning:Clone()
		ammoGui.Parent = playerGui
	end
	ammoGui.Enabled = true
	ammoLabel = ammoGui:FindFirstChild("AmmoLabel")
	ammoLabel.Text = "" .. ammo .. "/" .. maxAmmo
end

local function onActivated()
	isShooting = true
	shoot()
end

local function onDeactivated()
	isShooting = false
end

gun.Equipped:Connect(function()
	player = game.Players.LocalPlayer
	mouse = player:GetMouse()
	connection = gun.Activated:Connect(onActivated)
	disconnection = gun.Deactivated:Connect(onDeactivated)
	local char = player.Character or player.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid")
	makeAmmoGui()
end)

gun.Unequipped:Connect(function()
	player = nil
	mouse = nil
	connection:Disconnect()
	disconnection:Disconnect()
	if ammoGui then
		ammoGui.Enabled = false
	end
end)

uis.InputBegan:Connect(function(input, gameProcess)
	if input.KeyCode == reloadKey and
		ammo ~= maxAmmo and not isReloading then
		reload()
	end
end)

does it do the same thing if you comment out the wait?

I’m sorry, what do you mean by that?

Sorry, I searched it up, it doesn’t freeze without the wait

Mostly just curious what happens if reload is called without the wait(3.3)

cool

try this

local function doAmmoReload()
	wait(3.3)
	ammo = maxAmmo
	ammoLabel.Text = "" .. ammo .. "/" .. maxAmmo
	isReloading = false
end

local function reload()
	isReloading = true
	reloadSound:Play()
	local runner = coroutine.wrap(doAmmoReload)
	runner()
end

Sadly, it still freezes

Unless I replaced the wrong part

local reloadKey = Enum.KeyCode.R

local isShooting = nil

local function doAmmoReload()
	wait(3.3)
	ammo = maxAmmo
	ammoLabel.Text = "" .. ammo .. "/" .. maxAmmo
	isReloading = false
end

local function reload()
	isReloading = true
	reloadSound:Play()
	local runner = coroutine.wrap(doAmmoReload)
	runner()
end

local function shoot()
	while isShooting do
		if canShoot and ammo > 0 and not isReloading then
		canShoot = false
		re:FireServer(mouse.Target)
		ammo -= 1
		ammoLabel.Text = "" .. ammo .. "/" .. maxAmmo
			wait(coolDown)
			canShoot = true
		elseif ammo == 0 and not isReloading then
		-- click:Play()
		reload()
		end
	end
end

I got it working, thank you very much for the help!

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