How do I make the beam land on the exact location of the mouse? It ranges from a bit of inaccuracy to wholly messed up…
local UIS = game:GetService("UserInputService")
local Debris = game:GetService("Debris")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local humrp = char:WaitForChild("HumanoidRootPart")
local root = humrp:WaitForChild("RootRigAttachment")
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local e = Instance.new("Attachment")
e.Position = mouse.Hit.Position
e.Parent = humrp
local beam = Instance.new("Beam")
beam.Attachment0 = root
beam.Attachment1 = e
beam.Parent = humrp
Debris:AddItem(e,1)
Debris:AddItem(beam,1)
end
end)
The position of the beam on the mouse would be accurate if you raycasted to the player’s mouse position instead. Start the raycast at the root, then end the raycast at game.Players.LocalPlayer:GetMouse().hit.p. Create the second attachment for the beam at the result of the raycast, and it will be accurate to the mouse position.
I also noticed that the created attachment from the mouse hit position in the video is moving with the character, don’t parent it to the character unless this is intended.