oh its outside, its firing the function only once and never again
From the code that I wrote it shoots only once?
well i added one end)
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
local LaserGun = script.Parent
LaserGun.Parent.Equipped:Connect(function(Mouse)
connection = Mouse.Button1Down:Connect(function()
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)
Mouse.Button1Up:Connect(function()
connection:Disconnect()
end)
end)
And it shoots only once per click?
no it shot when i click for ONCE and it does nothing after
im pretty sure you need loop for an autorifile but i might be wrong
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)
If you’re using a while, I think you need to use a break
.
@foxnoobkite But, since your gun system is very obsolete and you should try the new fast cast system instead.
hmm nope, dosn’t do anything, i don’t think the things run behind loops
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
I’m not fully sure, but this may fix your issue
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)
You should take a look in the output maybe it would display some errors.
surprising…no errors at all and nothing is happening
it works but when i spam click the tool and hold it goes super fast without cooldown for some reason
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)
i think cooldown does nothing with this, i assume its because mutiple loops are running when i spam click
It may be because of how short I made the cooldown, try making it longer and trying again
anyway it seems like i had to wait more than .1 second for the cooldown when i was clicking
Well if that’s all would you mind marking one of my comments with the code as the solution so people can find it easier?