Reloading gun doesn't work if miss

I’m making a gun, but the only part of the script that doesn’t work is when you miss and it’s supposed to reload. It doesn’t print anything or give any errors. Here’s my code:

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 .. "!"
						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 .. "!"
						end
					elseif not hum then
						print("Missed")
						ui.Text = "You missed!"
						bullets -= 1
					end
				elseif part == nil then
					print("Missed")
					ui.Text = "You missed!"
					bullets -= 1
				end
		elseif 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")
								ui.Text = "Hit ".. part.Parent.Name .. "!"
								reload = true
								print("Reloading...")
								wait(5)
								reload = false
								print("Done Reloading!")
								bullets = 5
						elseif part.Name ~= "Head" then
								hum.Health -= config.NormalShot.Value
								game.ReplicatedStorage.GunGUI:FireServer(part.Parent, true, 1, config.NormalShot.Value)
								print("Successful Shot")
								ui.Text = "Hit ".. part.Parent.Name .. "!"
								reload = true
								print("Reloading...")
								wait(5)
								reload = false
								print("Done Reloading!")
								bullets = 5
						end
					elseif not hum then --Script stops functioning here
							print("Missed")
							ui.Text = "You missed!"
							reload = true
							print("Reloading...")
							wait(5)
							reload = false
							print("Done Reloading!")
							bullets = 5
							print(tostring(bullets))
					end
				elseif part == nil then
					print("Missed")
						ui.Text = "You missed!"
						reload = true
						print("Reloading...")
						wait(5)
						reload = false
						print("Done Reloading!")
						bullets = 5
					end
				end
			end
		end
	end)
end)

Please help me fix this. Thanks for your help. :slight_smile:

You didn’t add any else statement to handle with the bullring is lower than 1.

1 Like

That’s the point, the reloading should start when the bullet count is 1. It will add a debounce simulating 0 bullets reloading, then it will regain 5 more bullets. And I’m pretty sure it’s impossible to get a lower-than-1 bullet count with this code (unless your device is laggy and you have an autoclicker)

Instead of

not hum

Try

hum ~= nil

~= means not so

hum ~= nil

means hum is not nil, so it’s basically the

if hum then

Then can you try printing out what hum is when you finished all the bullets?

hum is an instance. Also, the gun script completely stops in the reloading part, even when I successfully hit the target.

Why don’t you just do the reloading logic when there is 0 bullets? You’re only limiting to 1 bullet.

The reloading part comes AFTER the damage so it seems like reloading automatically when it hits 0. But I guess ill try it.

Which of the print statements started working and which stopped printing?

Every print statement stops working inside the reload part of the code.

Just try this method, also are you really sure the variable part is not nil?

Both methods stop printing at the reloading part. Also, part is an Instance.

I’m editing my code myself and it’s a pain to run through all of that 97 lines of code to check the error. The whole gun doesn’t work because I added a pcall to check the error and I’m so confused about the ends. Can you please help?

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 --More than 1 bullet
			if reload == false then --Not reloading
				local part = mouse.Target
				print(typeof(part))
				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 .. "!"
							print(typeof(hum))
						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 .. "!"
						end
					elseif hum == nil then
						print("Missed")
						ui.Text = "You missed!"
						bullets -= 1
					end
				elseif part == nil then
					print("Missed")
					ui.Text = "You missed!"
					bullets -= 1
				else
					print("Missed")
					ui.Text = "You missed!"
					bullets -= 1
	   			end
		elseif bullets <= 1 then --If 1 bullet or less is left
			if reload == false then
				local part = mouse.Target
					local success, ret = pcall(function()
						if part 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")
									ui.Text = "Hit ".. part.Parent.Name .. "!"
									print("Reloading...")
									reload = true
									wait(5)
									reload = false
									print("Done Reloading!")
									print(typeof(hum))
									bullets = 5
								elseif part.Name ~= "Head" then
									hum.Health -= config.NormalShot.Value
									game.ReplicatedStorage.GunGUI:FireServer(part.Parent, true, 1, config.NormalShot.Value)
									print("Successful Shot")
									ui.Text = "Hit ".. part.Parent.Name .. "!"
									reload = true
									print("Reloading...")
									wait(5)
									reload = false
									print("Done Reloading!")
									bullets = 5
								end
							elseif hum == nil then
								print("Missed")
								ui.Text = "You missed!"
								reload = true
								print("Reloading...")
								wait(5)
								reload = false
								print("Done Reloading!")
								bullets = 5
							end
						elseif part == nil then
							print("Missed")
							ui.Text = "You missed!"
							reload = true
							print("Reloading...")
							wait(5)
							reload = false
							print("Done Reloading!")
							bullets = 5
						end
				end)
			end
		end		
	end)
end)