EDIT: Massive lag for first raycast during a frame (read first comment for the new details)

Hello, I am currently looking into performance issues with a new feature I’m adding to a game and I have stumbled upon something rather unusual and want to know if anyone else knows what is going on here.

In the game each player/npc has a script set in client mode that handles visual effects, an important thing to note is players and npcs both use the exact same script with 0 changes (npcs have them cloned from starter player scripts). In these scripts they do may things but the important one is they have a few raycasts each frame to handle effects that stick to the floor as well as the custom state machine.

The issue I seem to be running into is the raycast for npcs is taking upwards of 2 ms each frame!!! it only happens when effects are present, which doesn’t make sense as well npcs are using the same script as players.
here is a screenshot of the micro profiler (popper is the npc its just miss named)

I have looked into the code for the raycast and well it has given me 0 ideas


all that its doing is a simple floor check for the custom state machine for animations, for more context here is that raycast function

for more context of effects in this game, they are 3D part based vfx stored in a folder in workspace, they all have CanCollide, CanQuery, and CanTouch disabled any mesh parts have box CollisionFidelity as well. I’m not sure why this would interfere with only the raycasts of npcs but it somehow does.

If anyone has any ideas or questions about this feel free to let me know. If I end up finding the solution I’ll edit this post incase anyone somehow has a similar issue.

new discovery


image
it seems that its not the npc rather the first raycast that is run that has this problem, I’m starting to think this is more a roblox issue and less an issue on my end.

Raycasting is very fast, I fired 73+ raycast per frame and it has little performance reduction.
Check something else in your code. Maybe because of the collision of your effect in your NPC? Maybe because it kept setting it on the ground, colliding with the ground, two stuff fighting caused lag.

similar to what Kute said above, Raycasting is very fast and nearly nevers causes any actual performance drops. Do you have some similar thing happening when the raycast does start like creating the effects attached to it through instance.new.

the raycasts are totally separate from effects, which also use object pooling instead of instance.new().

I would think its maybe something to do with the npc if not for the fact of it happens to the player as well.