Hello there, So basically Im making a shop system where the player is buying something it goes to their inventory. What I had in plan is to make a click detector on an NPC’s torso and then a script detects it, opening up a shop GUI, [the script is also in the NPC’s torso] and handles everything you buy from that same script, deducting the money and cloning the item from ServerStorage to their inventory.
What I wanted to know, since I’m kind of bad at scripting, is doing this a good idea, or should I use remote events to handle buying and cloning?
If you want the system to be secure, you will need to handle the interface on the client, and perform checks on the server, which requires two scripts: One on the server, and one on the client.
Don’t trust the client.
Two is all that you need, but you could split it up into more manageable pieces by using module scripts. Definitely check them out if you haven’t already.
( Less scripts is not necessarily better. Putting all of your code into one script usually affects readability. )
I try and keep all relative stuff to 2 scripts. Client side, and server side.
Let’s say it’s a simulator
So shop could be 2 scripts.
Skill shop another 2 scripts.
So on…
It keeps your workspace from cluttering with scripts.
As mentioned, you can also use module scripts for repeated functions such as GetPlayerFunds() a function youd make, and through all scripts can have the same function.
Ehh, not necessarily. The only thing you change is how much you need to access. You can still have the same amount of code across several scripts versus a single script. Topics like this are typically preferential though, you’re right. It depends on your needs and wants.
There’s no real answer to this thread as a result. Naturally though you will need client-server communication for a shop so it can’t strictly speaking work off of only one script.
^ So basically this happens all from one script inside the players Torso, from that same script, it handles what the player chooses as an option, the NPC’s speech etc.
If I do it like this would it be harder for someone to exploit or easier for someone to exploit?
Yes, if you can keep things to one script that’s almost always a good idea. The main strong reason to have multiple scripts is if you’re trying to make something sharable / usable by non-technical users.
If you’re making your own self-contained game it’s a much better idea to have one client and one server script, and do the rest of your organization through ModuleScripts which are required by that one client and one server script. The big advantage of doing things that way is being able to exactly specify the order that everything loads in, so that you never have to worry about some script using stuff before it’s been initialized by another script.
I would add that having your entire game run on one loop that performs multiple tasks is a very bad idea. A single error can cause the loop to stop. With this in mind, systems like shops should be based on events which fire every time even if the previous fire resulted in an error, and main loops should be placed in different scripts with the ability to gracefully handle errors.