How can I lower or stop raycast lag?

Hello developers, I am working on something here and I seem to be getting a delay, it should be tracking the user but it is just behind the user. First off, the main issue is the delay. Also should I use a loop or .stepped?

This is on the server, I don’t think I have an option to use the client

Background on the system: it is finding the nearest person and tracking them to try to kill them.

Lag example: https://gyazo.com/e4799b6a5778d056a766408b464883a0

function startEvent()
	
	local Target = FindNearest()
	
	local TargetPrimaryPart = Target or Target.PrimaryPart
	
	local laser_Thickness = .5


	local rayOriginPart = Eye
	local rayTargetPart = Target.HumanoidRootPart
	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 = Eye.Parent:GetChildren()
	RayCastParameters.FilterType = Enum.RaycastFilterType.Blacklist
	
	
	--local rayTargetPart = rootPart
	local function castRay()
		local orgin = rayOriginPart.CFrame.Position
		local target = rayTargetPart.CFrame.Position
		local direction = (target - orgin)

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

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

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

		LaserPart.CFrame = CFrame.new(orgin, target) * CFrame.new(0,0, -distance / 2)

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

	--RunService.Stepped:Connect(castRay)
	
	while true do
		task.wait()
		castRay()
	end
	
	
end

Use .Stepped it runs every frame so there shouldn’t be lag.

Thanks, that is what I was thinking but people told me otherwise, the lag still happens using that

Change .Stepped to .RenderStepped I think that might fix it.

Can’t this is a server sided script therefore I can’t use that.

Perform the raycasting on the server, and send the results of the raycast to all clients with FireAllClients.

What is the raycast for? (needs 30 ch)

It is for tracking the player, it is an big tower that is tracking the nearest player and dealing damage

First of all if you want to reduce lag you should reduce the amount of raycasts you perform, you seem to cast rays forever with the only delay being waiting for task.wait() to complete, this is very unnecessary!

You should optimize it to only cast rays every x seconds or milliseconds, and determining the closest player can be done by just looping through all the players and returning the one who has the lowest distance, this is ALOT more performant.

but it should be following the player constantly

Can you give me more information about the tower and what you are trying to achieve exactly? It’s difficult to figure this out with the little amount i have to go off of!

Basically, it is eye of sauron it is going to find a player, track it, if it detects a block, it will explode it and go to try to kill the players

Sorry for the slow reply! Can we have the rest of the script, other than the constant raycasting we can’t see much else!

On the topic of that script however, you should reduce the amount of raycasting you do! It is unnecessary to do it as often as it does. Try adding a small delay in the while true loop, even .05 seconds will make a huge difference, you should also only raycast when it is necessary, keep that in mind!

Raycasts are only expensive if you’re casting long rays frequently. Developers can perform several thousand raycasts in one frame with no performance hits. I am fairly certain it is not the raycasts causing lag. OP can still optimise but raycasts alone are fairly computationally inexpensive.

Checking the GIF, it seems that what OP is experiencing isn’t real “lag” but rather the natural desync experienced by the server trying to track the character’s position. The client naturally sees their character in a newer place faster than the server. I would at least use Stepped instead of a while wait loop (those are bad, anyhow). As for the ray, something OP could do is have it predict where the character moves and “position” the cast accordingly.

5 Likes

Oh god i am so sorry, i didn’t see the link! I can see that you are right, it isn’t lag but rather it not being synced up. I thought rays were expensive, apologies for the waste of time.

As for the ray, something OP could do is have it predict where the character moves and “position” the cast accordingly.

And yes i fully concur, this would probably look better as well!

Also @Socks347, you should upload directly to the forum whenever possible, having the video embed is alot simpler and makes it significantly harder to miss.

1 Like

hmm… so add a offset in the direction the player is facing?

Rather than where they’re facing, think in terms of where they’re moving. Ideally you don’t want to be moving your ray when the player isn’t moving either but if they are moving then you want to move the ray a bit ahead of the player so it manages to catch them.

You can add the target’s velocity to their current position but clamp it down to about a stud or so in front of the character, play around with some values to get something that looks nice.

1 Like

Wha…

Just synchronize your visual effect across all clients? Handle damage on the server, handle the visuals of the ‘sauron’ facing the player on the client?

There’s no real need for predicting the players location; that’s just overcomplicating things.

Edit: Obviously you can predict the players location on the client if you do for whatever reason want to shoot infront of the player, in which case either way the visuals should still be handled on the client rather then server.

the issue here is both roblox replication and lag
roblox’s replication will add extra delay to prevent packet loss (which is why you still die behind walls even with low ping)

the only thing you really can do is do it for each client

Showing the visuals on client would make the damage not accurate, because the server sees it behind the player, therefore it won’t even hit them