Reloading Gun broken

I’m trying to fix my gun reload but it just stops working at the reload part. I’ve tried shortening my code with a function. Here’s my code:

local function reloadfunc(bulletcount, reloadbool)
	if bulletcount == 0 then
		reloadbool = true
		print("Reloading...")
		task.wait(5)
		reloadbool = false
		print("Done reloading!")
	end
end
local ui = game.Players.LocalPlayer.PlayerGui:WaitForChild("Missed/Hit").TextLabel
local bullets = 5
local reload = false
local config = script.Parent.Configuration
script.Parent.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if bullets > 1 then
			if reload == false then
				local part = mouse.Target
				if part ~= nil then
					local hum = part.Parent:FindFirstChild("Humanoid")
					if hum then
						if part.Name == "Head" then
							hum.Health -= config.Headshot.Value
							game.ReplicatedStorage.GunGUI:FireServer(part.Parent, true, 1, config.Headshot.Value)
							print("HEADSHOT")
							bullets -= 1
							ui.Text = "Hit ".. part.Parent.Name .. "!"
							reloadfunc(bullets, reload)
						elseif part.Name ~= "Head" then
							hum.Health -= config.NormalShot.Value
							game.ReplicatedStorage.GunGUI:FireServer(part.Parent, true, 1, config.NormalShot.Value)
							print("Successful Shot")
							bullets -= 1
							ui.Text = "Hit ".. part.Parent.Name .. "!"
							reloadfunc(bullets, reload)
						end
					elseif hum == nil then
						print("Missed")
						ui.Text = "You missed!"
						bullets -= 1
						reloadfunc(bullets, reload)
					end
				elseif part == nil then
					print("Missed")
					ui.Text = "You missed!"
					bullets -= 1
					reloadfunc(bullets, reload)
				else
					print("Missed")
					ui.Text = "You missed!"
					bullets -= 1
					reloadfunc(bullets, reload)
				end
			end		
		end
	end)
end)

Please help me. Thanks.

Can you please help me? I’ve been finding a solution but there’s no error.

I think you forgot to reset the bullets variable to 5 in the reloading function.

Well, yeah, but that’s not the problem. The problem is that the function doesn’t work. No print, nothing.

Have you tried calling the reloadFunc function after you have created it? So, for example;

local function reloadFunc()
    -- Code
end

reloadFunc()

If it still doesn’t print out anything let me know!

I called it right there in my code.

Sorry I accidentally replied to the original post instead of you

No no what I’m telling is just after creating the function. Not in any other functions. Just see if the function prints out anything or not.

1 Like

Maybe try replacing bulletcount == 0 to bulletcount < 1, unsure if you’ve printed bulletcount before but it could be like -1

Yes, it prints just fine outside any other functions. I don’t understand, because defining a function directly in the script would make it accessible anywhere within the script.

1 Like