Gun Glitch: Can shoot and reload when gun not equipped

Hello Devforum! Recently, I have been developing a raycasting gun with a bullet and reload system. I have the gun down, but now one of the only issues is the player can still shoot and reload even though the gun isn’t even equipped. I am very confused on why this happens.

Here is an example video:

As you can see, I can shoot and reload even though the gun isn’t equipped. I have been checking my code trying to add some values to indicate whether the gun could shoot or reload, but it didn’t work. I just went back to my original code.

Here is the code I have:

Local script:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local gunhold = character:WaitForChild("Humanoid"):LoadAnimation(game.ReplicatedStorage.GunAnimations.Pistol.Hold)
local reload = character:WaitForChild("Humanoid"):LoadAnimation(game.ReplicatedStorage.GunAnimations.Pistol.Reload)
local mouse = player:GetMouse()
local bulletsleft = script.Parent.Bullets -- value in gun about the amount of bullets if you are confused
local inputservice = game:GetService("UserInputService")
local reloading = false

script.Parent.Equipped:Connect(function()
	gunhold:Play()
	local cloneofgui = script.Parent.PistolGui:Clone()
	cloneofgui.Parent = player.PlayerGui
	local textlabel = cloneofgui.TextLabel
	textlabel.Text = "Bullets: "..bulletsleft.Value
	
	bulletsleft.Changed:Connect(function()
		textlabel.Text = "Bullets: "..bulletsleft.Value
	end)
	inputservice.InputBegan:Connect(function(input, gameprocessedevent)
		if bulletsleft.Value < 10 then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				if input.KeyCode == Enum.KeyCode.R then
					if not reloading then
						reloading = true
						textlabel.Text = "Reloading..."
						reload:Play()
						wait(2)
						bulletsleft.Value = 10
						textlabel.Text = "Bullets: "..bulletsleft.Value
						print("Reloaded")	
						reloading = false
					end
				end
			end			
		end
	end)	
	
	mouse.Button1Down:Connect(function()
		if bulletsleft.Value > 0 then
			script.Parent.Fire:FireServer(mouse.Hit.p)
			bulletsleft.Value = bulletsleft.Value - 1
			print(bulletsleft.Value)
		elseif bulletsleft.Value == 0 then
			print("No more bulelts left")
			textlabel.Text = "No more bullets! Press R to reload"
			inputservice.InputBegan:Connect(function(input, gameprocessedevent)
				if input.UserInputType == Enum.UserInputType.Keyboard then
					if input.KeyCode == Enum.KeyCode.R then
						if not reloading then
							reloading = true
							textlabel.Text = "Reloading..."
							reload:Play()
							wait(2)
							bulletsleft.Value = 10
							textlabel.Text = "Bullets: "..bulletsleft.Value
							print("Reloaded")	
							reloading = false
						end
					end
				end
			end)
		end
	end)
end)	
	
script.Parent.Unequipped:Connect(function()
	player.PlayerGui:FindFirstChild("PistolGui"):Destroy()
	gunhold:Stop()
end)

Server script handling the shooting aspect:

local canshoot = true

script.Parent.Fire.OnServerEvent:Connect(function(player, mousepos)
	
	if canshoot then 
		
		canshoot = false
		
		local raycastparams = RaycastParams.new()
		raycastparams.FilterDescendantsInstances = {player.Character}
		raycastparams.FilterType = Enum.RaycastFilterType.Blacklist

		local direction = (mousepos - script.Parent.Shoot.Position).Unit*100
		local raycastresult = game.Workspace:Raycast(script.Parent.Shoot.Position, direction)
		local intersection = raycastresult and raycastresult.Position or script.Parent.Shoot.Position + direction
		local distance = (script.Parent.Shoot.Position - intersection).magnitude

		local bullet = game.ServerStorage.Bullet:Clone()
		bullet.Size = Vector3.new(0.1,0.1,distance)
		bullet.CFrame = CFrame.new(script.Parent.Shoot.Position, intersection)*CFrame.new(0,0,-distance/2)
		bullet.Parent = game.Workspace

		if raycastresult then
			local hitpart = raycastresult.Instance
			local model = hitpart:FindFirstAncestorOfClass("Model")

			if model then
				if model:FindFirstChild("Zombie") then
					model.Zombie:TakeDamage(20)
				end
			end
		end

		wait(0.25)
		bullet:Destroy()
		canshoot = true
	end
end)

I thought that whatever code is in the equipped function only happens in the equipped function, but when the gun is unequipped, the code from the equipped function still runs. What did I do wrong?

1 Like

You included the firing function inside of the .Equipped event. The .Equipped event fires whenever the player equips the tool, so you are connecting the gun firing event every single time the tool is pulled out.

Move the firing function outside of the .Equipped event and instead, use a debounce (bool value) for .Equipped and .Unequipped to indicate whether the tool is pulled out or not.

Then, have the firing function check to see if the debounce is true (gun is out) or false (gun is in backpack).

Soooo like this?

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local gunhold = character:WaitForChild("Humanoid"):LoadAnimation(game.ReplicatedStorage.GunAnimations.Pistol.Hold)
local reload = character:WaitForChild("Humanoid"):LoadAnimation(game.ReplicatedStorage.GunAnimations.Pistol.Reload)
local mouse = player:GetMouse()
local bulletsleft = script.Parent.Bullets
local inputservice = game:GetService("UserInputService")
local reloading = false
local equipped = false

script.Parent.Equipped:Connect(function()
	equipped = true
	print("True")
	gunhold:Play()
	local cloneofgui = script.Parent.PistolGui:Clone()
	cloneofgui.Parent = player.PlayerGui
	local textlabel = cloneofgui.TextLabel
	textlabel.Text = "Bullets: "..bulletsleft.Value
	
	bulletsleft.Changed:Connect(function()
		textlabel.Text = "Bullets: "..bulletsleft.Value
	end)
end)	

if equipped == true and player.Backpack:FindFirstChild("Pistol") then
	print("True 2")
	local cloneofgui = player.PlayerGui:FindFirstChild("PistolGui")
	local textlabel = cloneofgui.TextLabel
	inputservice.InputBegan:Connect(function(input, gameprocessedevent)
		if bulletsleft.Value < 10 then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				if input.KeyCode == Enum.KeyCode.R then
					if not reloading then
						reloading = true
						textlabel.Text = "Reloading..."
						reload:Play()
						wait(2)
						bulletsleft.Value = 10
						textlabel.Text = "Bullets: "..bulletsleft.Value
						print("Reloaded")	
						reloading = false
					end
				end
			end			
		end
	end)	

	mouse.Button1Down:Connect(function()
		if bulletsleft.Value > 0 then
			script.Parent.Fire:FireServer(mouse.Hit.p)
			bulletsleft.Value = bulletsleft.Value - 1
			print(bulletsleft.Value)
		elseif bulletsleft.Value == 0 then
			print("No more bulelts left")
			textlabel.Text = "No more bullets! Press R to reload"
			inputservice.InputBegan:Connect(function(input, gameprocessedevent)
				if input.UserInputType == Enum.UserInputType.Keyboard then
					if input.KeyCode == Enum.KeyCode.R then
						if not reloading then
							reloading = true
							textlabel.Text = "Reloading..."
							reload:Play()
							wait(2)
							bulletsleft.Value = 10
							textlabel.Text = "Bullets: "..bulletsleft.Value
							print("Reloaded")	
							reloading = false
						end
					end
				end
			end)
		end
	end)
end
	
script.Parent.Unequipped:Connect(function()
	equipped = false
	player.PlayerGui:FindFirstChild("PistolGui"):Destroy()
	gunhold:Stop()
end)

Almost.

if equipped == true and player.Backpack:FindFirstChild("Pistol") then
	print("True 2")

This section isn’t needed. Instead, the check should be inside of the firing function.

inputservice.InputBegan:Connect(function(input, gameprocessedevent)
		if equipped and bulletsleft.Value < 10 then

Same with the button1down

mouse.Button1Down:Connect(function()
if not equipped then return end
if bulletsleft.Value > 0 then
1 Like

Thank you! It works now :D. Once again thank you very much!!!