While reloading the gun, it breaks if you shoot for some reason.I need some help with debugging this so please if you can help thanks :)

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local debounce = false
local maxammo = script.Parent:WaitForChild("maxammo")
local clipammo = script.Parent:WaitForChild("clipammo")
local reloading = false

function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then
			if clipammo.Value == 9 then 
				maxammo.value = maxammo.value
				end
		print("reloading")
		reloading = true
		player.PlayerGui.Bullets.TextLabel.Text = "Reloading..."
		maxammo.Value = (maxammo.Value - (9 - clipammo.Value))
		clipammo.Value = 9
		wait(3)
		player.PlayerGui.Bullets.TextLabel.Text = clipammo.Value.." / "..maxammo.Value
		reloading = false
		print("reloading finished")
	end
end
 
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

tool.Equipped:connect(function(mouse)
	local BulletsGui = script.Parent.Bullets

		BulletsGui:Clone().Parent = player.PlayerGui
		BulletsGui.TextLabel.Text = clipammo.Value.." / "..maxammo.Value
	mouse.Button1Down:connect(function()
		
	if not debounce then
		
	
		debounce = true
if clipammo.Value <= 0 or maxammo.Value <= 0 then end
	if reloading == true then end
	if clipammo.Value > 0 and reloading == false then

		clipammo.Value = clipammo.Value - 1
		player.PlayerGui.Bullets.TextLabel.Text = clipammo.Value.." / "..maxammo.Value
		
		local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
		local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
		
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Bright red")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
		
		local distance = (tool.Handle.CFrame.p - position).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
		
		game:GetService("Debris"):AddItem(beam, 0.1)
		
		if part then
			local humanoid = part.Parent:FindFirstChild("Humanoid")
			
			if not humanoid then
				humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
			end
			
			if humanoid then
				humanoid:TakeDamage(7)
			end
		end
		
		wait(0.39)
		
		debounce = false
	end
	
tool.Unequipped:Connect(function()
	player.PlayerGui.Bullets:Destroy()
end)
	end
	end)
end)

2 comments

0

How does it break? Does it fall? Does it get removed? Please be more specific on how it breaks.

0

Any errors in your console?