I am trying to make a fps game and I just found out that when I unequip my gun it still fires after I unequipped it
after I unequipped the tool the autofire also stopped working.
this is the code:
local tool = script.Parent
local remote = tool:WaitForChild("OnShoot")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local shootDelay = 1 / 12
local LastFired = 0
local held = false
local FireKey = Enum.UserInputType.MouseButton1
local players= game:GetService("Players")
local client = players.LocalPlayer
local cursor = client:GetMouse()
local function fire()
local now = tick()
if now - LastFired >= shootDelay then
LastFired = now
remote:FireServer(cursor.Hit.Position)
end
end
script.Parent.Equipped:Connect(function()
UserInputService.InputBegan:Connect(function(i, gp)
if not gp and i.UserInputType == Enum.UserInputType.MouseButton1 then
fire()
held = true
end
end)
UserInputService.InputEnded:Connect(function(i, gp)
if not gp and i.UserInputType == Enum.UserInputType.MouseButton1 then
held = false
end
end)
end)
connection = RunService.RenderStepped:Connect(function()
if held then
fire()
end
end)
script.Parent.Unequipped:Connect(function()
held = false
connection:Disconnect()
end)
Does anyone know what is wrong?