Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a gun stop firing after the tool is unequipped?

im making my first gun so my code isnt the best, but how can i prevent the gun from shooting after the player unequipped it?

Client Side Code

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
script.Parent.Parent.Equipped:Connect(function(eqp)
		print("eqp")
mouse.Button1Down:Connect(function(input)
	script.Parent.Parent.fire:FireServer(mouse.hit.p)
end)

end)

Server Side Code

local RS = game:GetService("ReplicatedStorage")
local beam = RS.beam
local tip = script.Parent
script.Parent.Parent.fire.OnServerEvent:Connect(function(player, mousePos)
	print("hello")
	local beamClone = beam:Clone()
	beamClone.Parent = game.Workspace
	beamClone.Position = mousePos
	beamClone.CanCollide = false
	beamClone.Size = Vector3.new(.3,.3,.3)
	beamClone.BrickColor = BrickColor.new("New Yeller")
	beamClone.Material = "Neon"
	wait()
	beamClone:Destroy()

end)
--end)

I dont have any errors it works as id like it to but when i put the tool away it still fires the bullets when i click ANY advice would be helpful even if it doesn't have to do with the question, thanks!!

1 comment

0

tool.Unequipped, tool.Unactivated, tool.Activated, etc etc. there's your answer <3

1 answer

✓ Accepted answer

Use tool.Activated instead of mouse.Button1Down. Your client side code should be changed to this:

local mouse = game.Players.LocalPlayer:GetMouse()
script.Parent.Parent.Activated:Connect(function()
	script.Parent.Parent.fire:FireServer(mouse.hit.p)
end)

You can replace your original code with this and I'm sure that it will pretty much fix your problem.

This discussion is preserved as it was. New answers, comments, and votes are closed.