How do I make a beam follow the players cursor?

I’m attempting to make a laser that will follow the players cursor, I know it has something to do with ray casting or something, but I have absolutely no idea how to use it.

1 Like

i’d take a look at this
https://developer.roblox.com/en-us/api-reference/function/WorldRoot/Raycast
you would fire a raycast either on local or server and use a remote event to give the info to the server

1 Like

or if a laser fires there constantly u would repeatedly give location to server so it can create a beam

I see. And how exactly do I like, actually do this? Because as I said I absolutely have no idea how to use a Raycast, and I don’t know how to bind it to my beam.

local parameters = RayCastParams.new()
parameters.FilterDescendantInstances = {--thing not to be touched by beam, for example u could put player.Character here}
parameters.FilterType = Enum.RayCastFilterType.Blacklist
local startpos = --starting position
local endpos = --endposition
local ray = workspace:RayCast(startpos, (endpos - startpos).Unit, parameters)
if ray then
     print(ray.Instance)
else
     print("Ray didnt hit anything")
end

sorry for the late reply

So do I just put this in the part with the beam in it?

Also there are 3 errors in the script, pardon me being a little dumb by the way, but I’m not sure how to fix these.
“W001: Unknown global ‘RayCastParams’.”
“Syntax error: Expected identifier when parsing expression, got ‘local’”
“Syntax error: Expected identifier when parsing expression, got ‘local’”

  1. mb, it should be RaycastParams instead of RayCastParams and
  2. when i wrote what the starting position and ending position were supposed to be, did u change them to what i wrote there?

Ah, right sorry. Must’ve slipped my mind, uh so what exactly do I do with the starting and ending position? Sorry once again I’m a little confused.

starting position would be wherever the beam starts, for example character.RightHand or parts of a tool, such as the handle, and ending would be the player’s mouse position, such as

local mouse = game.Players.LocalPlayer:GetMouse()
local endingpos = mouse.hit.p

you can only get the mouse on the client, i suggest making the actual beam on server and creating the raycasts on the client.

task.wait(1)
local parameters = RaycastParams.new()
parameters.FilterDescendantInstances = {--thing not to be touched by beam, for example u could put player.Character here}
parameters.FilterType = Enum.RayCastFilterType.Blacklist
local startpos = game.Players.LocalPlayer.Character.RightHand
local endpos = game.Players.LocalPlayer:GetMouse().hit.p
local ray = workspace:RayCast(startpos, (endpos - startpos).Unit, parameters)
if ray then
     print(ray.Instance)
else
     print("Ray didnt hit anything")
end

final script

Sorry for the probably stupid questions but where does the actual beam part come in?
And also it says in the console “‘FilterDescendantInstances’ is not a valid member of RaycastParams”

There is a good article on the wiki that explains how to use raycasting, I highly recommend you check it out Raycasting | Roblox Creator Documentation

2 Likes