Gun still shoots after unquipped (also starts lagging)

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

i wanna make the built trails stop after unquipped and also want to stop shoots going through walls

  1. What is the issue? Include screenshots / videos if possible!

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i haved seen people having solutions but i dont know how to add t my script

 local mouse = game.Players.LocalPlayer:GetMouse()
local isShooting = false

local RemoteEvent = script.Parent:WaitForChild("RemoteEvent")

mouse.Button1Up:Connect(function()
	if isShooting == true then
		isShooting = false
	end
	isShooting = false
end)

mouse.Button1Down:Connect(function()
	script.Parent.Equipped:Connect(function()
		isShooting = true
		while isShooting == true do
			print("IsShooting")
			RemoteEvent:FireServer(mouse.Hit.LookVector)
			task.wait(0.1)
			
		end
	end)
end)

local debounce = false

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, mouse)
	local bullet = Instance.new("Part",workspace)
	bullet.CFrame = script.Parent.ShootPart.CFrame
	bullet.CanCollide = false
	bullet.Size = Vector3.new(1, 0.1, 0.1)
	bullet.BrickColor = BrickColor.new("New Yeller")
	bullet.Material = "Neon"

	local velocity = Instance.new("BodyVelocity",bullet)
	velocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	velocity.Velocity = mouse * speed

	bullet.Touched:Connect(function(hit)
		if debounce == false then
			debounce = true
			print(hit.Parent.Name)
			if hit.Parent:FindFirstChild("Humanoid") then

				print("got through debounce")
				if hit.Parent.Name ~= player.Name then
					print("got through checking players name")
					hit.Parent.Humanoid:TakeDamage(5)
				end

			end
			task.wait(0.1)
			debounce = false
		end

	end)

	game.Debris:AddItem(bullet, 10)
end)
6 Likes

The reason why your gun keeps firing even if the gun is unequipped it simply because you didn’t tell the gun to set isShooting to false. This is an example script.

local tool = script.Parent
local isShooting = false

tool.Unequipped:Connect(function()
	isShooting = false
end)
1 Like

I’ve also forgot to mention by what you meant by lagging, do you mean by the bullets stay put for a brief moment before firing? If so, if I remember correctly, you are creating(?) the bullets in the server which isn’t a good idea.

1 Like

by laging is because of the bullets when shooting

1 Like

Please clarify, I didn’t know what you meant by that?

so the bullets that still shooting after unquipped it makes so many part that it start to lag my laptop and by the way i dont know where to place the script so i did it like this but it seems that after the first try it works after does the same thing

local mouse = game.Players.LocalPlayer:GetMouse()
local isShooting = false
--the script u gave me
local tool = script.Parent
local isShooting = false

tool.Unequipped:Connect(function()
	isShooting = false
end)
-- end of the script u gave me

local RemoteEvent = script.Parent:WaitForChild("RemoteEvent")

mouse.Button1Up:Connect(function()
	if isShooting == true then
		isShooting = false
	end
	isShooting = false
end)

mouse.Button1Down:Connect(function()
	script.Parent.Equipped:Connect(function()
		isShooting = true
		while isShooting == true do
			print("IsShooting")
			RemoteEvent:FireServer(mouse.Hit.LookVector)
			task.wait(0.1)
			
		end
	end)
end)

Oh so basically, your game lags while the gun is firing, correct?

ye when i uniqupped and continue to shoot

I don’t know why it’s causing your laptop to lag, although I’d assume it would be because of the server getting overwhelmed, I think?

does it matter where i put the little script you gave because on the first try it works but then it stops

Oh it works for the first time but stops working on multiple attempts? Weird.
Maybe try placing the tiny code on the bottom.

ima try that tomorrow i gotta go to sleep

Alright then, cya. Good luck on your project!

you’re binding your Equipped listener every time your mouse button is pressed

this causes possibly hundreds of connections, and hundreds of bullets being fired every shot.

instead try not to bind to events inside of other event connections

--var to keep track of if the gun is equipped
local Equipped = false

--bind a function to set Equipped to true when the gun is equipped
tool.Equipped:Connect(function()
	Equipped = true
end)

--bind a function to set Equipped to false when the gun is unequipped
tool.Unequipped:Connect(function()
	Equipped = false
end)

mouse.Button1Up:Connect(function()
	if isShooting == true then
		isShooting = false
	end
	isShooting = false
end)

mouse.Button1Down:Connect(function()
	--on click, if our gun isn't equipped dont shoot
	if not Equipped then return end

	isShooting = true
	--while loop checking if the gun is equipped and the gun is shooting
	while isShooting == true and Equipped do
		print("IsShooting")
		RemoteEvent:FireServer(mouse.Hit.LookVector)
		task.wait(0.1)
	end
end)

notice how in the modified script I provided, all of my event connections (when I call :Connect) are in the base level of the script, this ensures that they only bind once and subsequently only fire once

1 Like

i tryed it and it seems that it doesnt work
i tryed it all the script together and without it also seperate

the script wasn’t intended to be fully copy and pasted into yours, it’s intended to explain how you might go about doing something like what you’re doing. My points are still valid, you’re connecting to your gun.Equipped event every time you recieve a mouse.Button1Down event
image
that means that every time you click your mouse it will be binding a new completely seperate function to on equipped. then when you equip your tool it will run all of the functions binded even if they are the same function

lets say you click 100 times, now you would have binded to equipped 100 times, and whenever you equip the tool it will start 100 repeat loops, resulting in 100 bullets being fired every 0.1 seconds.

like I said in the outset, the easiest way to fix this is by keeping all of your event connections seperate from each other

mouse.Button1Down:Connect(function()

end)

script.Parent.Equipped:Connect(function()

end)

but this requires extra logic to ensure you aren’t shooting while the gun is unequipped.

the main things you need to take away from the script I gave earlier is that you need some way to keep track if the gun is equipped or not, which we can just listen to .Equipped and .Unequipped and update a global variable (in this case the variable “Equipped”)

now that we know if the gun is equipped or not we can incorperate it into our shoot logic, which should follow a simple pipeline
“Button click”
“Is our gun equipped”
-“No? then return, breaking out of our function and stopping it”
-“Yes? then continue running our function”
“Set our isShooting variable to true so we can break out of our shoot loop with mousebutton1up”
“while loop that only runs if we are shooting and our tool is equipped”

Event connections are tricky sometimes, but understanding how they work can help you become a better scripter.

nvm i gave up on that sorry for wasting your guys time