What is the best way for inserting instance or variable for your game?

  1. What do you want to achieve? Keep it simple and clear!

  2. What is the issue? Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Here is my answer:

  1. What I want to achieve is the best way or efficient way of inserting instance or variable for my game.

  2. The problem is still unsure, the script is quite long and changing the foundation is surely can mess the whole thing up.

  3. I tried using local variable = instance.new("IntValue", playerStat) or something like that, and it seems it’ll require a lot of local usage and I’m afraid it’ll reach the limit (around 255 if I’m not mistaken) and so I tried other method such as using the table instance instead of object such as IntValue or StringValue.

Using the local variables may not be really efficient especially for a game that may require a lot of local usage and possibly reaches its limit, and using the table instance may have one problem, I can’t share it with other script. So I store it in ModuleScript so it can be shared between scripts. And here another problem arise, I couldn’t share the same value within the ModuleScript (if I’m not mistaken, it’ll be divided into two, server and client) so I need another way to share the value between server and client.

However… Since my script may be too complex and big, I sometimes get a “nonsense” bug or error that may seems not making sense… Yet I probably need to rewrite all of it from scratch again, but, I need to understand it before doing such thing.

I may share the code/script but I’d like to hear your suggestion first. The script may be too long if I copy paste the entire thing, so far, the script isn’t working (still have some error such as value of type nil cannot be converted to a number) yet it’s strange why value of type nil while I clearly put local instance = instance.new("NumberValue", variables), it’s clearly a variable I created right inside the script. I’ve been searching for an answer but seems it can’t help me much, from search engine, to AI (Artificial Intelligence).

If your script is big enough where you’re encountering issues with reaching the limit on how many variables you can have, then the best way to fix this problem is to split the script into seperate modules, or separate scripts if possible considering what the large script is trying to do

By the way, it’s not good practice to use Instance.new’s second argument to set the parent, unless you don’t intend to modify the newly created Instance’s properties


It’s much harder to debug one script that has tones of lines of code rather than a bunch of scripts or modules that have a reasonable amount of code, especially if you aren’t careful with keeping the large script organized

Sorry for the slow reply.
Does that mean I shouldn’t use the Instance.new’s second argument (set parent) if I constantly update the specific Instance?

I see, I also been splitting scripts into many, however, about a game that is big enough to surpass maximum local limit, I’m not sure about that as I intended to create Instance for the specific number of item (used for something like object within an inventory that can be stacked), but using table may be more efficient. Even though I may not fully understand the concept of table array or something like that, does it has limit or not…

I’d like to ask if any of you know this value of type nil cannot be converted to a number message like the image below means?
image

It was a completely same Instance just like I made, but for some reason there are some Instances that result like this, even though it was created using the same method and updated using the same method (Instance.new) within the same script, and updated from the table within the Module Script as well.

And after giving each line that occur error/warning a -- (disabling it) it seems to work perfectly fine, only I can’t use the specific Instance

I keep wondering whether I should use Instance.new (Object) or Table (Non-object/ within a script). Which one is better and more efficient? Or it depend on the case?
After contemplating for a moment, I think I won’t use too much of an Instance for the main variables (such as currency, main stats as health, attack damage, etc.) however… How about the item/objects like something you store within the backpack? Surely it’ll be a lot of different items and object that more likely will surpass the limit of Local usage within a script, right? So I wonder if should I use both instead…
And so I ended using both, the Table for the Instance to across the Server-side while the Instance as the Variable or Object to be constantly updated & only for the specific Instances for the Instance to share the data with Client-side. If I’m not mistaken, I couldn’t share the value of a Table within the Module Script with the Client-side (As it’ll only be updated in the Server-side while the Client-side is different, this is most likely to avoid such collision between server and client)…

there’s no such thing as a local limit btw

What it means is that you should set an Instance’s parent after you set its properties, like this example:

local part = Instance.new("Part")
part.Name = "ExamplePart"
part.Color = Color3.new(0, 1, 0)
part.CFrame = CFrame.new(4, 16, -8)
part.Parent = workspace

Since the maximum local variable limit is 200 per scope (scope, not script), it’s quite hard to hit even if you have a large quantity of variables in a single script. If your intention is to store player’s data, then using a table would be more efficient than using Instances. If you make sure to remove a value from the table once the value is no longer needed, then there’s no need to worry about hitting any limits since tables can store a good amount of data if kept organized

That warning explains what happened quite well: something was expecting a number value, but got no value whatsoever. What this means is that one of the values you’re using to set that Instance’s properties doesn’t actually contain a number value, or isn’t able to be converted to a number

Using a table that’s stored in a module is preferable when you don’t need the value to replicate from server to client or client to server, but for values you’d like both the server and the client to be able to see, then you can either use an Instance, attributes, or send the information using RemoteEvents or RemoteFunction (do be careful if sending data from the client to the server though, as some clients are able to change the data that’s sent to try to cheat or break the server’s script)

1 Like