How can I lower or stop raycast 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

…?

This just seems like a problem within a problem within a problem. That’s going to be on you to figure out at the end of the day. If your question at hand is how you can make the visual effects as well as the ‘eye’ properly face the player, the answer is to handle it on the client, as all visual effects, etc… should be.

If damage then seems inaccurate then you can have the server handle damage (as well as cast its own rays), then have the client send its raycast results to the server, compare the servers raycast to the clients raycast, if it’s within a sane range then you can apply damage.

1 Like

This is actually a pretty fair thought. I was thinking in terms of my own use case where we literally predict the player’s position but I guess my use case is different because it’s an archer and a projectile, not a ray. Doing two comparative rays between the server and the client as well as divorcing the visuals from the function is actually a more appropriate solution.

My bad. :sweat_smile:

1 Like