Good script structure tips?

Does anybody have any good tutorials or tips about script structure?

  • How should my scripts be organized and ran so they can communicate with eachother efficiently?

  • Where should I put them in my DataModel?

I cannot find any good resources from the devforum or YouTube, so if anybody has any tips or tutorials it would greatly help as this is my biggest issue with creating games.
Thanks!

1 Like

I’m a little bit confused with this but what exactly do you mean by script structure? Is it like the structure of a book that have introduction, climax, etc?

And for the DataModel, you should put a script to where it belongs. Example: a LocalScript can only run in Client-Sided DataModel such as StarterCharacterScript. And server scripts game should be parented in Server-Sided DataModel such as ServerScriptService or Workspace. Keep in mind that Client-Sided scripts can only be used or seen by a player, like GUIs. And Server-Sided scripts can be used or seen by any player, like a cooldown system. Feel free to ask and correct me if I’m wrong.

1 Like

Communication
Scripts on the same computer can communicate with each other by using BindableEvents. However, BindableEvents have limitations and I consider these quite significant limitations. You can read more about them in the documentation.
https://create.roblox.com/docs/scripting/events/custom#argument-limitations.
To avoid these limitations, I would recommend using ModuleScripts, and if you need events/signals, you can either write your own event class or use a public module. Custom events/signals don’t have the limitations of BindableEvents.

Script structure
For script structure, the luau style guide may be useful.
https://roblox.github.io/lua-style-guide/

Location in instance hierarchy
I would recommend having server-only code (both modules and scripts) in ServerScriptService. However, if a server-only module is part of a larger system that isn’t server only, I think it would be more logical to keep it with the other modules of that system unless it’s important that the client can’t access it’s code.

Client-only modules and modules that can be used by both client and server should be in ReplicatedStorage.

I’d recommend having LocalScripts in StarterPlayerScripts so that they won’t run every time the player respawns. Alternatively, you could have scripts with RunContext set to client in ReplicatedStorage.

1 Like

The best way to structure code in my personal opinion and work is using Module scripts for the functional aspect of things and information like tables.

You can learn about modules from this video which I watched when I was getting started with them: Module Script Breakdown - Gnome Code

1 Like