hello i made an if loop for an auto gun, but when i test it out and clicked, it kept firing the event even though my mouse is not down, heres the script:
local Debris = game:GetService("Debris")
local LaserGun = script.Parent
local Tip = LaserGun:WaitForChild("Tip")
local ray = game.ReplicatedStorage.Toolevents:WaitForChild("Ray")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local GunDamage = 30
equipped = false
shooting = false
local connection
LaserGun.Parent.Equipped:Connect(function(Mouse)
equipped = true
connection = Mouse.Button1Down:Connect(function()
shooting = true
while shooting and equipped do
local Laser = Ray.new(Tip.CFrame.p, (Mouse.Hit.p - Tip.CFrame.p).unit * 300)
local HitPart, HitPosition = game.Workspace:FindPartOnRay(Laser, Character, false, true)
LaserGun.Position += LaserGun.CFrame.RightVector * 0.1;
wait(0.1)
LaserGun.Position += LaserGun.CFrame.RightVector * -0.1;
ray:FireServer(HitPart,HitPosition,Tip)
wait(0.1)
end
end)
Player.Mouse.Button1Up:Connect(function()
shooting = false
connection:Disconnect()
end)
end)
LaserGun.Parent.Unequipped:Connect(function(Mouse)
equipped = false
end)
the gun is not moving meaning the event itside is stuck in a loop of some sort
Maybe try this? This might work without using ‘connection’ and used new task api.
local Debris = game:GetService("Debris")
local LaserGun = script.Parent
local Tip = LaserGun:WaitForChild("Tip")
local ray = game.ReplicatedStorage.Toolevents:WaitForChild("Ray")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local GunDamage = 30
local equipped = false
local shooting = false
local mouseheld = false
local Mouse = nil
Mouse.Button1Down:Connect(function()
mouseheld = true
while mouseheld and equipped do
if not mouseheld or not equipped then shooting = false break end
shooting = true
local Laser = Ray.new(Tip.CFrame.p, (Mouse.Hit.p - Tip.CFrame.p).unit * 300)
local HitPart, HitPosition = game.Workspace:FindPartOnRay(Laser, Character, false, true)
LaserGun.Position += LaserGun.CFrame.RightVector * 0.1;
task.wait(0.1)
LaserGun.Position += LaserGun.CFrame.RightVector * -0.1;
ray:FireServer(HitPart,HitPosition,Tip)
task.wait(0.1)
end
end)
Player.Mouse.Button1Up:Connect(function()
mouseheld = false
end)
LaserGun.Parent.Equipped:Connect(function(NewMouse)
Mouse = NewMouse
equipped = true
end)
LaserGun.Parent.Unequipped:Connect(function()
Mouse = nil
equipped = false
end)
i don’t think you can do a mousebuttonup function inside a loop can you? because i was thinking of like looping and break until it detects the mousebutton1 is up manully every loop
local Debris = game:GetService("Debris")
local LaserGun = script.Parent
local Tip = LaserGun:WaitForChild("Tip")
local ray = game.ReplicatedStorage.Toolevents:WaitForChild("Ray")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local GunDamage = 30
local LaserGun = script.Parent
local Active = false
local Mouse = Player:GetMouse()
LaserGun.Parent.Activated:Connect(function()
Active = true
while Active == true do
local Laser = Ray.new(Tip.CFrame.p, (Mouse.Hit.p - Tip.CFrame.p).unit * 300)
local HitPart, HitPosition = game.Workspace:FindPartOnRay(Laser, Character, false, true)
LaserGun.Position += LaserGun.CFrame.RightVector * 0.1;
wait(0.1)
LaserGun.Position += LaserGun.CFrame.RightVector * -0.1;
ray:FireServer(HitPart,HitPosition,Tip)
wait(0.1)
end
end)
LaserGun.Parent.Deactivated:Connect(function()
Active = false
end)
Try this I added a short cooldown what you can change
local LaserGun = script.Parent
local Active = false
local CanShoot = true
local CooldownTime = .1 -- How much time to "recharge" after deactivating
local Mouse = Player:GetMouse()
LaserGun.Parent.Activated:Connect(function()
if CanShoot == true then
Active = true
while Active == true do
local Laser = Ray.new(Tip.CFrame.p, (Mouse.Hit.p - Tip.CFrame.p).unit * 300)
local HitPart, HitPosition = game.Workspace:FindPartOnRay(Laser, Character, false, true)
LaserGun.Position += LaserGun.CFrame.RightVector * 0.1;
wait(0.1)
LaserGun.Position += LaserGun.CFrame.RightVector * -0.1;
ray:FireServer(HitPart,HitPosition,Tip)
wait(0.1)
end
end
end)
LaserGun.Parent.Deactivated:Connect(function()
Active = false
CanShoot = false
wait(CooldownTime)
CanShoot = true
end)