Is there any way to do this better? server sided

repeat task.wait() until script.Parent:IsA("Model")

this line of code is used in a script which is placed in every character when they join, and i need it to be server sided. so i made this and put it in server storage and a script from serverscript service distributes it to characters. is this bad for the server? will it cause any issues?

This isn’t necessary.
When a script is run, its ancestry is already fully loaded and doesn’t need to be checked.
Instead, disable this script until it is in the correct location then enable it.

I believe understanding what you’re trying to do would be a better pointer towards any potential “better” methods to achieve this. What exactly are you trying to achieve with this? It seems that you’re simply yielding the code until the parent is a model, which if is the player’s character, it always will be a model. Additionally, scripts do not run when parented under ServerStorage, so if that’s your purpose behind this recursive checking, I believe that would be a redundant approach in that case.

If you’re simply trying to parent a script to a character, while not hook that functionality to CharacterAdded?

oh, thats right, i didnt think about that, i made the script a long time ago when i had way less knowledge in scripting and simply didnt think about it. so scripts dont run in server storage? so i could just enable them and move them to the character?

@ZEDDxR’s reply already has everything you need to know. Try to avoid any kind of loop.
Also, to add on top of this, using Run Service is much better and recommended when you need to have code recursively updating every single game frame (tick).

Also, you can change a script’s run context from Legacy to Server/Client if you want it to run regardless of where it is. You need to make a new script and in the properties you will see run context. That’s all.

Correct. Scripts do not run when parented to ServerStorage. It is nothing more than a server-sided storage container. You can simply parent them directly into the player’s Character (without even needing to enable them, unless they’re already disabled by default in ServerStorage).

Hope that helps, good luck.

funny enough, the script was already tied to a character added event, but the repeat was still in there, but now i used what i saw in the replies and it works flawlessly, thanks everyone

BTW, one last thing: try to make use of the collection service and more module scripts with metatable optimizations. It will save you memory, trust me. If you have, for example, 100 players using a new script instance, it might lag, but if you use a module script to control it, you will save a lot of memory. You can see a comparison here.

This or many other tutorials might help you. Just try to learn it, it’s worth it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.