How to datastore2 more than 1 variable?

A couple small mistakes here.

Making the Datastore2 instances global doesn’t help. Why? Because they’re declared in Players.PlayerAdded:Connect(), which means they’re only defined when a player joins. So without a player joining yet, since this is a server Script, line 90 will run with terrainpositionStore as nil because it’s not declared yet.

If you intend to do this for every player, as I said earlier, even if you’re just testing, you ought to put it in the same scope as you declared the variables, where Players.PlayerAdded:Connect() is if you’re planning to do this for every player. That way you can actually wait until the player actually joins and the variables are actually declared before you use them.

Basic Debugging Tips

What the error message says and what line it is on really helps. This isn’t only for when letting your helpers on DevForum find your error, but they also help when you find your error yourself.

Keyword: index. To index something is to get something out of a table using a key. When I say table, you should already know that a table is a one-in-all, all-in-one data structure for Lua. This means that they aren’t only dictionaries, but they are also instances such as Datastore2 instances.

Keyword instance. An instance is an individual object of a class made with Object-Oriented Programming (OOP). I recommend you check out the OOP paradigm in Lua. Datastore2 uses it and it’s very useful to you, too. (not necessarily for this particular issue you’re working on)

As for your actual error, calling a function on an instance is also getting something out of a table. Tables can store functions, just like Datastore2’s class. So when calling a function on the Datastore2 instance, you’re indexing it with the function. You’re indexing nil with the function, Get(). This is how I knew that the datastore was nil and this is how you’ll know in the future, as well. If you look a little further to see where you declared them, you’ll find the code where you declared the variables does not run until someone joins.

Another note, since you made them global variables, then keep in mind that all players share the same variables so if you plan on using those Datastore2 instances outside of the PlayerAdded:Connect() (which I don’t recommend), then you’ll have to remember this.

A Note About Serialization

I said in one of those first posts I wrote for you on this issue that you need to serialize and deserialize certain objects, remember? I also wrote some functions as a demonstration to you how you might serialize and deserialize some bunker objects. If you forgot, please reread my old posts about serialization and deserialization as you can’t store anything with CFrames or Vector3s inside datastores - you need to serialize them, first, as I showed you in my demonstration.