You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
Fire a simple raycast without lag spikes
What is the issue? Include screenshots / videos if possible!
when i add parts into the game , each time i start firing a raycast the game freezes for a sec
3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried changing the parts to non collidable disable the canQuery bool, anchoring them and still , it seems the raycast founds a way to include those parts into the method, i also use ignoreDescendants of the parts that i want to use which are rlly few…
Im firing a raycast every 0.25 sec , the lag spike happens only when i increase the partcount above 15000
i have another raycast that fires at 100 lenght every 0.25 secs but still doesnt explain why if i increase the part count the raycast starts to freeze the game…
I also made sure i didnt have any code iterating trough the whole workspace, i came to the conclusion is the raycast thanks to the script profiler
I’ll give it a test myself later, one thing that’s notable though is that you are putting a task wait inside of a heartbeat. This doesn’t make sense because heartbeats get called every frame, so you’re not doing a raycast every 0.25 seconds, you’re doing it every frame, but delaying it first.
It’s not a great idea.
See every call of the function as it’s own independant one, because that’s essentially what it is. You’re connected to the Heartbeat, which means everytime the heartbeat happens, this function gets executed anew. The wait function doesn’t block the next call.
It’s not whats causing your issue, but you should fix it.
I just tested it, and I couldn’t reproduce your issue at all. Created over 15000 parts, and did raycasts of random lengths every frame. No issue at all.
Unless you have anything else, I’d wager it’s just a processing issue and that your computer can’t handle it. Oh yeah, and you also shouldn’t use pcalls like that, just check for potential errors before you do the action instead of wrapping it up in a pcall.
Yea i also tried to reproduce it on an empty baseplate with 40k partd and couldnt found any issues, what i found is that there is a hugh renderjob block
When you start getting in to high part counts, I find optimisations like removing Cast Shadow, disable CanCollide and CanTouch from purely simple scenic parts can make a huge difference to performance.