How to visualize a raycast?

I used one of alvinblox’s tutorials to make myself a gun. He showed how to raycast and all of that, the thing he didn’t show was how to visualize it. Sort of “see” the raycast. How would I do that? Here’s the code.

script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local raycastResult = workspace:Raycast(script.Parent.Handle.Position, (mousePos - script.Parent.Handle.Position)*300, raycastParams)

if raycastResult then
local hitPart = raycastResult.Instance
local model = hitPart:FindFirstAncestorOfClass(“Model”)

  if model then
  	if model:FindFirstChild("Humanoid") then
  		model.Humanoid.Health -= 30 
  	end
  end

end
end)

4 Likes

Something like this? Because it isn’t working.

script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local raycastResult = workspace:Raycast(script.Parent.Handle.Position, (mousePos - script.Parent.Handle.Position)*300, raycastParams)

if raycastResult then

  local distance = (script.Parent.Handle.Position - raycastResult.Position).Magnitude
  local p = Instance.new("Part")
  p.Anchored = true
  p.CanCollide = false
  p.Size = Vector3.new(0.1, 0.1, distance)
  p.CFrame = CFrame.new(script.Parent.Handle.Position,script.Parent.Handle.Position*300)*CFrame.new(0, 0, -distance/2)
  
  local hitPart = raycastResult.Instance
  local model = hitPart:FindFirstAncestorOfClass("Model")
  
  if model then
  	if model:FindFirstChild("Humanoid") then
  		model.Humanoid.Health -= 30 
  		wait(.5)
  		p:Destroy()
  	end
  end

end
end)

No, in fact I will leave you my tutorial, since the video you watched has some questionable practices.

I explain it further in there as well

2 Likes

Crazy there’s no built in method for this yet, in UE4 it’s a toggle. Oh and uh - happy birthday!

I tried the tutorial, for some reason the bullet part is bugged, dont know why, i followed the whole tutorial.