When I unequip my gun, it still shoots from the last place the shootPart was. Also, when spamming 1 (or whatever backpack slot its in) it shoots way faster than intended. This is my script:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent
local mouseDown = false
local canPlay = true
local sound = script.Parent.shoot
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Button1Down:Connect(function()
mouseDown = true
end)
mouse.Button1Up:Connect(function()
mouseDown = false
end)
tool.Equipped:Connect(function()
while wait(0.2) do
if mouseDown then
if canPlay then
canPlay = false
local ray = Ray.new(tool.shootPart.CFrame.p, (mouse.Hit.p - tool.shootPart.CFrame.p).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Brick yellow")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.5
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (tool.shootPart.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.1, 0.1, distance)
beam.CFrame = CFrame.new(tool.shootPart.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
game:GetService("Debris"):AddItem(beam, 0.1)
if part then
local humanoid = part.Parent:FindFirstChild("Humanoid")
if not humanoid then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid then
humanoid:TakeDamage(49)
end
end
sound:Play()
canPlay = true
end
end
end
end)
I'm a fairly new scripter so any help is appreciated.
