After awhile I was able to create a single beam rather than several projectiles.
Local Script
local tool = script.Parent
local toggle = tool:WaitForChild("Toggle")
local part = tool:WaitForChild("Handle")
local pos = tool:WaitForChild("pos")
local m
local u
local run = game:GetService("RunService").RenderStepped
local on = false
local equipped = true
local storepos = coroutine.create(function(mouse)
while equipped do
pos.Value = mouse.Hit.p
run:wait()
end
end)
tool.Equipped:connect(function()
local player = game:GetService("Players").LocalPlayer
repeat run:wait() until player.Character
local character = player.Character
local mouse = player:GetMouse()
coroutine.resume(storepos,mouse)
m = mouse.Button1Down:connect(function()
on = true
toggle:FireServer(true)
end)
u = mouse.Button1Up:connect(function()
if not on then return end
on = false
toggle:FireServer(false)
end)
end)
tool.Unequipped:connect(function()
m:disconnect()
u:disconnect()
equipped = false
if not on then return end
on = false
toggle:FireServer(false)
end)
Server Script
local tool = script.Parent
local toggle = script.Parent:WaitForChild("Toggle")
local part = script.Parent:WaitForChild("Handle")
local mousepos = script.Parent:WaitForChild("pos")
local on = false
local beam = Instance.new("Part")
beam.BrickColor = BrickColor.new("Really red")
beam.FormFactor = "Custom"
beam.Size = Vector3.new(0,0,0)
beam.TopSurface = "Smooth"
beam.BottomSurface = "Smooth"
beam.CanCollide = false
beam.Anchored = true
beam.Transparency = 0.5
beam.Material = "Neon"
toggle.OnServerEvent:connect(function(player,bool)
if bool then
on = true
local character = player.Character
local clone = beam:Clone()
clone.Parent = tool
while on do
local pos = part.Position
local ray = Ray.new(part.Position,(mousepos.Value - pos).unit*300)
local hit,endpos = workspace:FindPartOnRay(ray,character,false,true)
local distance = (pos - endpos).magnitude
clone.Size = Vector3.new(0,0,distance)
clone.CFrame = CFrame.new(pos, endpos)*CFrame.new(0,0,-distance/2)
if hit then
local human = hit.Parent:FindFirstChild("Humanoid")
if human then
human:TakeDamage(1)
end
end
wait()
end
clone:Destroy()
else
on = false
end
end)
Note that these two scrips should be inside the tool.
There should also be a remote event called "Toggle" and a Vector3Value called "pos" inside the tool.
This script would have been a lot easier had filtering enabled been switched off, but filtering enabled is important in keeping games secure.
The main problem posed by filtering enabled was creating the laser beam to be server-side instead of client-side, so that all players could see it. If that doesn't matter to you, then it's much easier to make the laser beam client-side.
To put this script simply, all I did was use the MouseButton1Down, MouseButton1Up, and Unequipped events to toggle whether the beam should be on or off, and I used a loop to manipulate the beam and check if that condition was true. If it wasn't, then I'd end the loop and delete the laser.
Seeing as it is you probably don't understand this script very well, I can't explain it to you very well until you learn the rest of the terminology. I made this script so you could use it as an example or guide to put this idea into perspective.
Good Luck!