Good and bad scripting practices?

I use a bit of spawn function, and try to use as little loops as I can, but sometimes it seems like there aren’t many other choices.
Like for instance, if I wanted to control a bunch of npcs in one script, I use spawn function for each individual npc and have it looped. I am not sure of a better way to do that though.
I’ve found some uses by waiting for the players move velocity to increase as a work around sometimes but it also seems weird to use constantly.

I also have never used pcall function and I actually have no clue what it does. Same with courotine wrap, and a few others i cant remember. For most of my scripts i also don’t use modules, it seems easier for me to combine everything into one “main” script and handle it all there. I am also use to using a lot of the older deprecated methods, like BodyVelocity, Gyro, etc. It just seems pointless to transition since the new stuff is essentially the same but in different words.

So I’m just wondering, in general, what are some good practices to learn and bad ones to forget?

1 Like

Creating NPC’s are tricky so I don’t blame you in being frustrated however here are some tips over the years of programming on ROBLOX.

  1. Use task.wait(): Use task.wait() over wait() - Task.wait() uses the task scheduler which enables multi-threading whcih increases the processing power by thresholds… Wait() is deprecated and I highly suggest not using it anymore.

  2. To answer your own question, courotine.wrap() basically just runs code seperately so if you were to call a coroutine function twice at the same time it’ll run seperately and independently. It’s somewhat the same for the spawn function.

  3. Pcall function is used to catch errors, you can essentially attach a pcall function to two variables like succ,err (which is commonly used). To do this you would do

local succ,err = pcall(function()  
    code here 
end) 

if succ then 
     code here 
else 
     code here 
end

It basically catches any error and outputs that if needed.

These are just a few to list and only answered most of your questions about Pcall and coroutine mostly. I’ll post more later when I have the time however hopefully this helped you a bit :slight_smile:

2 Likes

I’ll also help you with your NPC problem later

1 Like

If you’re controlling a bunch of NPC’s, i’d highly suggest you to use parallel programming to split up tasks. Running all the heavy computation serially could cause performance issues.

1 Like

Thank you id love the help. Right now i have 2 different types of NPCs im using for testing purposes;humanoid and humanoidless. They both work basically the exact same, but yeah i need to figure out a better way of controlling them as a whole.

Also thank you for explaining the others. I will definitely be using them from now on, they just seem much more organized.

Don’t nest your code if you don’t have to. If possible, use guard clauses. They inverse the if statement and return if it’s met.

1 Like

this one’s obvious but, deprecated methods.

1 Like

I use return as often as i can, and sometimes use break too for loops. Dont see break often in other people’s code though

Going to take me a bit to transition from the methods ive known and worked with for years but i hope ill understand the new methods soon!

Well thinking you should avoid loops at all cost isn’t good. Nothing wrong with loops used well.
There are many ways to do things, sometimes you don’t need a loop. pcalls are great when you know what is being used may run into errors. Like a go head to try and don’t stop a script because it hit an error. Avoiding deprecated methods is a good call … the entire call could just be removed at one point. Then you’re going back to debug something that is now broken.

1 Like