Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago

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!!

0
tool.Unequipped, tool.Unactivated, tool.Activated, etc etc. there's your answer <3 Antelear 185 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

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.

Ad

Answer this question