Fix visual ray flickering?

Hello, I have an laser using rays and it seems to flicker (shown in video below)

As you can see from the output the position is changing even if I don’t move. (Second image below)

I have tried black listing the laser part but it didn’t really seem to work.

https://gyazo.com/0db1427b782a4fcb71cc36ff7c93099e

Client code:
(not all of it)

local LaserPart = Instance.new("Part")
LaserPart.Color = Color3.fromRGB(255, 0, 0)
LaserPart.Anchored = true
LaserPart.CanCollide = false
LaserPart.Parent = workspace

local RayCastParameters = RaycastParams.new()
RayCastParameters.FilterDescendantsInstances = game.Workspace.HappyHome.Home.EyeOfSauron.Eye.Parent:GetChildren()
RayCastParameters.FilterType = Enum.RaycastFilterType.Blacklist



local function castRay(rTarget)

	--print("Target in castRay is set to"..rTarget)

	local laser_Thickness = .5

	local rayOriginPart = Eye
	local rayTargetPart = rTarget



	local origin = rayOriginPart.CFrame.Position
	local rayTarget = rayTargetPart.HumanoidRootPart.CFrame.Position
	local direction = (rayTarget - origin)

	local results = workspace:Raycast(origin, direction, RayCastParameters)

	if not results then
		results = {Position = rayTarget}
	end

	local distance = (results.Position - origin).Magnitude

	LaserPart.Size = Vector3.new(laser_Thickness,laser_Thickness,distance)


	LaserPart.CFrame = CFrame.new(origin, rayTarget - rTarget.Humanoid.MoveDirection * 2) * CFrame.new(0,0, -distance / 2)
	

	--game.ReplicatedStorage.EyeEvent:FireAllClients(origin, rayTarget, distance, LaserPart)
end

local target

EyeEventRemote.OnClientEvent:Connect(function(Target)
	target = Target
end)

RunService.RenderStepped:Connect(function()
	if not target then return end
	castRay(target)
end)

What is “Eye”? Is it a part attached to the player’s head? Can you send a gyazo of the laser in use with the player model?

Also,

Every time this remote event gets fired, a new .RenderStepped event gets connected. If you :FireClient the same player 10 times, there will be 10 RenderStepped events firing every frame doing the same time.

I would recommend to change it to:

EyeEventRemote.OnClientEvent:Connect(function(Target)
	target = Target
end)

RunService.RenderStepped:Connect(function()
	if not target then return end
	castRay(target)
end)

Other than that, we don’t know how you’re currently setting up your model, so we can’t be sure of the issue.

Eye is a model in the workspace :smiley: also thanks let me try that out

It didn’t seem to work, here is what it looks like.

https://gyazo.com/9eb3a2dbf453053a3365d0cd76773a21

Weld an attachment to the player’s character and use a beam for better performance.

I can’t use .touched on a beam can I?

If you are using raycasting to generate the part, why bother with touched event?

I don’t understand what you are saying, to do with this maybe elaborate more?

If you were tracking the player with the raycast part then I recommend beams as replacement, otherwise what would you be using .touched event for?

Dealing damage to the player when they are hit and damaging blocks around it

What is the condition that the player is damaged when the part is raycasted? Must they be moving?

No, they don’t I have to use raycast for what I am doing

The difference is very minuscule which suggests that it may just be some floating point precision error. I would suggest rounding the position to, say, the nearest hundredth or something.

Beams will generate a more fluent laser effect than baseparts, and if you wish to use .touched as hitbox detection then set the part’s transparency to 1.

Um well you i see,why dont you raycast from both the client as visual and from the aerver as the real lazer,this would eliminate replication lag and also be exploiter safe