Gun shooting even if the gun is not equipped

Heya, I got a problem.

You see when the gun is reloading i did

	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)

I did this so the gun cannot be unequipped when reloading, the Gui which lets you unequip and equip tools is disabled, once the reloading is done the Gui is again visible, But when you unequip the gun you can actually shoot even if the gun is not equipped. I need this fixed but i have no idea how.

So like how to fix doc

Here’s the local script which does pretty much everything

local tool = script.Parent
local player = game.Players.LocalPlayer
local shooting = false
local mouse = player:GetMouse()
local raycastfilter = RaycastParams.new()
raycastfilter.FilterDescendantsInstances = {player.Character}
raycastfilter.FilterType = Enum.RaycastFilterType.Blacklist
local ammo = 6
local max_ammo = 6
local cooldown = 0.5
local reloading = false
local mouse_icon = "http://www.roblox.com/asset/?id=9720078100"
local UIS = game:GetService("UserInputService")
local StarterGui = game:GetService("StarterGui")

local function reload()
	player.PlayerGui.RevolverGUI.TextLabel.Text = "Reloading!"
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
	tool.Handle.Reload:Play()
	reloading = true
	task.wait(2)
	ammo = max_ammo
	player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(max_ammo)
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,true)
	reloading  = false
end

tool.Equipped:Connect(function()
	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.R then
			reload()
		end
	end)
	player.PlayerGui.RevolverGUI.Enabled = true
	player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(ammo)
	mouse.Icon = mouse_icon
	UIS.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			if not shooting and not reloading then
				if ammo == 0 then
					reload()
					return
				end
				ammo -= 1
				tool.Handle.Fire:Play()
				player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(ammo)
				local raycast = workspace:Raycast(tool.Handle.Position,(mouse.Hit.Position - tool.Handle.Position)*300, raycastfilter)
				if raycast then
					local instance = raycast.Instance
					local instanceParent = instance.Parent
					local findHumanoid = instanceParent:FindFirstChildOfClass("Humanoid")

					if findHumanoid then
						game.ReplicatedStorage.RemoteEvent:FireServer(findHumanoid)
					end
				end
				shooting = true
				task.wait(cooldown)
				shooting = false
			end
		end
	end)
end)

tool.Unequipped:Connect(function()
	mouse.Icon = ""
	player.PlayerGui.RevolverGUI.Enabled = false
	script.Parent.Handle.Reload:Stop()
end)

Make sure you set shooting to false when the weapon is unequipped

tool.Unequipped:Connect(function()
shooting = false
	mouse.Icon = ""
	player.PlayerGui.RevolverGUI.Enabled = false
	script.Parent.Handle.Reload:Stop()
end)

This does nothing sadly

I Really really hate the 30 letter thing

Well, there are a couple of problems here. You have the inputbegans’ inside of the Tool.Equipped, meaning they will continue to stack each time you reequip the gun, and you never check IF it’s equipped.

Here’s a fixed(?) script:

local tool = script.Parent
local player = game.Players.LocalPlayer
local shooting = false
local mouse = player:GetMouse()
local raycastfilter = RaycastParams.new()
raycastfilter.FilterDescendantsInstances = {player.Character}
raycastfilter.FilterType = Enum.RaycastFilterType.Blacklist
local ammo = 6
local max_ammo = 6
local cooldown = 0.5
local reloading = false
local mouse_icon = "http://www.roblox.com/asset/?id=9720078100"
local UIS = game:GetService("UserInputService")
local StarterGui = game:GetService("StarterGui")
local equipped = false

local function reload()
	player.PlayerGui.RevolverGUI.TextLabel.Text = "Reloading!"
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
	tool.Handle.Reload:Play()
	reloading = true
	task.wait(2)
	ammo = max_ammo
	player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(max_ammo)
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,true)
	reloading  = false
end

tool.Equipped:Connect(function()
	equipped = true
	player.PlayerGui.RevolverGUI.Enabled = true
	player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(ammo)
	mouse.Icon = mouse_icon
end)

tool.Unequipped:Connect(function()
	equipped = false
	mouse.Icon = ""
	player.PlayerGui.RevolverGUI.Enabled = false
	script.Parent.Handle.Reload:Stop()
end)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R and equipped then
		reload()
	end
end)

tool.Activated:Connect(function() --friendlier for all devices and only while equipped
	if not shooting and not reloading then
		if ammo == 0 then
			reload()
			return
		end
		ammo -= 1
		tool.Handle.Fire:Play()
		player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(ammo)
		local raycast = workspace:Raycast(tool.Handle.Position,(mouse.Hit.Position - tool.Handle.Position)*300, raycastfilter)
		if raycast then
			local instance = raycast.Instance
			local instanceParent = instance.Parent
			local findHumanoid = instanceParent:FindFirstChildOfClass("Humanoid")

			if findHumanoid then
				game.ReplicatedStorage.RemoteEvent:FireServer(findHumanoid)
			end
		end
		shooting = true
		task.wait(cooldown)
		shooting = false
	end
end)
1 Like

Ok i’ll try that

I super super hate the 30 letter thing

It works doc, Thx so much

But why does it happen tho.
Explain if you want to

Sure. In the original script, the tool.Equipped ran then handled all of the inputs. Those events never disconnect unless you do it yourself; meaning they would stack. You also never checked whether or not it was equipped, in which I did. tool.Activated only runs when the player clicks with the tool equipped/activated it

which allowed me to handle the shooting without any extra variables. Also, for the reloading, I just added the equipped variable which I changed in the equipped and unequipped functions.

Ok thx

Roblox pls remove the damn 30 letter thing

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