Raycast Beam Lagging

Hello, I am trying to learn raycasting but I seem to be running into an issue
for some reason my raycast is getting nil if you look at the output (bottom left)
but then it randomly works at the same time?
Im currently trying to make a Beam but this issue is making it lag about

Here is the script

Thanks.

Use task.wait(), and parent the beam after all properties are set. Printing also causes lag.

Parent it after properties are set
use task.wait
and printing causes a slight lag / yield

Aren’t like when you getting direction you must normalize it before using?

local Distance = 1000 -- How far ray will go.
local Direction = (mouse.Hit.p - origin.Position).Unit -- We getting here direction and property Unit returns normalized vector.

local ray = workspace:Raycast(origin.Position, Direction * Distance, params)

(Edited string of code so you could just copy it)

Use mouse.Move instead so you don’t need a loop. You should still keep the hold variable working though, as there’s no easy way to tell if the mouse buttons are being held down, without a variable and connections like this.

mouse.Move:Connect(function()
    if hold then
        -- code
    end
end)

I also don’t know why you need to raycast? You can just set the beam start and end if mouse.Hit exists.

its works!, I decided to rewrite the script and not use raycast and it works very smoothly.
honestly a very simple script dont know why i overcomplicated it so much lol. thanks.

image
heres the updated code for anyone wondering

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.