Need help on how to make a ray visible

How do I make a ray visible?

I’m unsure of as to how to make a ray visible as I’m pretty new to raycasting, help would be appreciated.

My code:

local gun = script.Parent.Parent
local localPlayer = game.Players.LocalPlayer
local mouse = localPlayer:GetMouse()
local camera = workspace.CurrentCamera
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("DamageEvent")


gun.Activated:Connect(function(shoot)
    local n = 10000
    local rayOrigin = gun.Handle.Position
    local rayDirection =  mouse.Hit.Position

    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {gun.Parent}
    raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
    local raycastResult = workspace:Raycast(rayOrigin, (rayDirection - rayOrigin) * n,  raycastParams)
    
    if raycastResult.Instance.Parent:FindFirstChildOfClass("Humanoid") then
        remoteEvent:FireServer(raycastResult.Instance.Parent:FindFirstChildOfClass("Humanoid"))
        print("Detected something:", raycastResult.Instance:GetFullName())
        print("Material Hit:", raycastResult.Material.Name)
    else
        print(raycastResult)
    end
end)
1 Like

Try the Search tool up top with the search term “make a ray visible”.
I did and got 5 solved posts.

So, you cant exactly “make a raycast visible”
However, you can create one of these two options:

Option 1: A part that has a trail and move it from the raycast origin to the raycast.Position using Tweenservice
Option2: Create 2 parts, 2 attachments and create a beam, and then make one of the parts positions set to the raycast Origin, and the others position to the raycast.Position

Option 1 will result in a more bullet looking object, as it will move, but Option 2 is faster to script.

I have already done this. I wouldn’t have created this post if I found results on what I need help with.

This has been previously solved on the dev forum

2 Likes