Basically, I have a gun that shoots a laser when the player clicks and holds. I want the laser to come out of the gun and end where the mouse is positioned (or as close to the mouse as the max length allows).
The issue is I can’t get the laser to rotate or position correctly. It resizes fine, but any movement throws the player off the map, despite the entire tool being cancollide = false
. Here is the code I currently have.
local Players = game:GetService("Players")
local tool = script.Parent
local Configuration = tool.Configuration
local Chamber = tool.Chamber
local Handle = tool.Handle
local Laser = tool.Laser
local function RenderLaser()
local mouse = Players.LocalPlayer:GetMouse()
local startPos = Chamber.Position
local endPos = mouse.Hit.Position
local length = math.min(Configuration.StudRange.Value, (startPos - endPos).Magnitude)
local midPoint = (startPos + endPos) / 2
Laser.Transparency = 0.5
Laser.Size = Vector3.new(length, Laser.Size.Y, Laser.Size.Z)
Laser.Position = midPoint
Laser.CFrame = CFrame.new(midPoint, endPos)
end
local function ResetLaser()
Laser.Size = Vector3.new(0.05, 0.05, 0.05)
Laser.Transparency = 1
end
local render = false
tool.Activated:Connect(function()
render = true
end)
tool.Deactivated:Connect(function()
render = false
end)
while true do
if render then
RenderLaser()
else
ResetLaser()
end
task.wait()
end