Error while loading datastore? [SOLVED]

So, I’m attempting to load my game which is a hub of sorts, which will lead the player into another place, the game itself serves as a start place and its supposed to save Kills and Wipe-outs through the games, the hub will convert to points and place you on a leader board depending on how many you have, but I have a small issue, when I try to load my points counter comes up as a “?” which is set this way by default in case anything goes wrong which it has.

I cannot travel through my games on roblox nor on studio, I’m trying to test out my KOs and WOs saving between games and I cant teleport, nor will any data show up at all.

I’ve looked all over the developer hub for this specific error and I’ve come to learn its because a certain variable or something doesn’t exist.

As for the error that is outputted, it says this:

Loaded data of XShockwaveX2335  -  Server - BackendMain:449
ServerScriptService.BackendMain:511: attempt to index nil with 'KOs'  -  Server - BackendMain:511
Stack Begin  -  Studio
Script 'ServerScriptService.BackendMain', Line 511 - function PlayerAdded  -  Studio - BackendMain:511
Stack End  -  Studio
Infinite yield possible on 'Players.XShockwaveX2335:WaitForChild("Points")'  -  Studio
Stack Begin  -  Studio
Script 'Players.XShockwaveX2335.PlayerGui.MainUI.UIHandler', Line 1351 - function Init  -  Studio - UIHandler:1351
Script 'Players.XShockwaveX2335.PlayerGui.MainUI.UIHandler', Line 1358  -  Studio - UIHandler:1358
Stack End  -  Studio

Here is a screenshot of the script in studio:

As for teleporting, no error is outputted and I have no clue why it wont let me switch between all of the games under the start place. I can only assume it has something to do with this I’m relatively new to scripting so this may be a challenge. I appreciate any effort towards helping <3

3 Likes

Could be an issue to do with the actual data itself. The error attempt to index nil with "KOs" might be to do with the data not saving properly, not loading properly, not having enough time to save before the game closes, or not using the DataStoreService properly. Are you:

  1. Using a BindToClose() function to ensure the game has enough time to save data
  2. Using the same key to save and load data, within the same DataStore
  3. Using a pcall() to access the DataStore
  4. Labelling all the data correctly within your data dictionary
1 Like

Hey thanks for the response, I am infact using BindToClose() functions and pcall() functions like in these examples from the same script here:
image


As for data keys and labelling the data correctly in a dictionary, this is my first proper attempt at using a datastore, so I’m unsure weather or not I had to set up something beforehand, I’m guessing by keys you mean what they are named?

No, I meant like the item you put as the first parameter to GetAsync() and SetAsync(), as well as UpdateAsync(). Anything within the DataStore for an individual player. An example might be:
"Key_"..player.UserId

1 Like

I see do you mean examples like these?
image


I used a print command to tell me weather they were failing or not… I’m totally stumped

The key used to access would be the line tostring(Player.UserId). Did the print() statement work?

Also, the FindFirstChild() part within the SetAsync() function needs to be changed. FindFirstChild() should be used when you are unsure whether something exists or not. Since it is a server-side script, change it to the following:

local playerPoints = player.Points.Value
local succ, err = pcall(function()
    Points:SetAsync(tostring(Player.UserId), playerPoints)
end)

Plus, if you are only testing this in Studio, your BindToClose() function is ignoring that which could still mean there’s not enough time for saving the data.

1 Like

Not at all, every time I have tried to test it and my print statements never print anything but "print("Loaded data of “…tostring(Player.Name))”, I have a few tostring lines for certain different datastores, such as banned players, play time, and points. The thing that gets printed says that the data is loaded, even when it is not. I publish after editing the script, and go on to the games page on the website and run it from there, and the same issue happens.

Try getting rid of the FindFirstChild() functions, and replace them with the code I sent above. Let me know how it goes.

1 Like

Alright will do ill get back to you in sec.

1 Like

That seems to be working great! Thank you! My data now loads, which is good progress, but I still seem to have issues while moving throughout places, my data doesn’t save withing place to place, I reset character to get a WO in the other place and it doesn’t save over, any idea how I can achieve this? I mean my KOs and WOs should move from the game under the starter place, and transfer the data over to the start place (the hub) so that it can calculate the KOs and WOs and convert them into points. Is it as simple as copying the script from serverscriptservice and placing it in the other places serverscriptservice?

1 Like

The data should transfer fine if:

  1. Each seperate place you want the data to be able to transfer to is under the same experience
  2. You are saving data on PlayerRemoving and loading it on PlayerAdded
  3. The game has enough time to save the data
  4. You are accessing the same DataStore within each place

I may have jumped the gun a bit asking that question so soon, so is that as simple as just copying over the main script from serverscriptservice where all the datahandlers are stored. Also it seems another error has popped up…
image

1 Like

Is that the same Points DataStore?

I’m fairly certain it is, i replaced all of the FindFirstChild() functions with what you provided. I’m starting to believe that I cant teleport while the data is this way

Would you mind sending a code snippet of the code you are using to get the data?

Sure thing, I assume this is what you’re asking about? I may be wrong

Cool. Can you add the PlayerAdded data loading function as well please?

1 Like

Absolutely that must’ve gone straight over my head, thank you for putting up with me works like a charm now :sob:

1 Like

Wouldn’t it be more efficient to store all the data in a dictionary instead of managing 50 trillion DataStores?