Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Getting a nil value from a datastore?

Asked by 5 years ago
Edited 5 years ago
01local DataStoreService = game:GetService("DataStoreService")
02local MainDataStore = DataStoreService:GetDataStore("MainDataStore")
03 
04local playerUserId = "Player_"..player.UserId
05 
06    local MainData
07    local success, errormessage = pcall(function()
08        MainData = MainDataStore:GetAsync(playerUserId)
09    end)
10 
11    if success then
12        print("Loaded in successfully")
13        print(MainData)
14        totalPeopleInfected.Value = MainData["TotalInfected"]
15        peopleInfected.Value = MainData["Infected"]
View all 22 lines...

It does the success part of the if statement but then I get this error

109:27:11.667 - ServerScriptService.Leaderstats:91: attempt to index nil with 'TotalInfected'

Any ideas? (This is in a player added function, I only copied the neccessey parts)

0
On your line "print(MainData)" does it print "nil"? Spjureeedd 385 — 5y
0
yes Barty200 85 — 5y
0
i would assume that it didn't save correctly, because it looks like the loading works fine sheepposu 561 — 5y
0
Yeah maybe the table is not saved as the way it get received. LinavolicaDev 570 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
01local DataStoreService = game:GetService("DataStoreService")
02local MainDataStore = DataStoreService:GetDataStore("MainDataStore")
03 
04local playerUserId = "Player_"..player.UserId
05 
06    local success, mainData = pcall(function()--The second argument will be what is returned from the function
07        return MainDataStore:GetAsync(playerUserId)
08    end)
09 
10    if success and mainData then
11        print("Loaded in successfully")
12        print(MainData)
13        totalPeopleInfected.Value = MainData["TotalInfected"]
14        peopleInfected.Value = MainData["Infected"]
15        money.Value = MainData["Money"]
View all 21 lines...

Try this

Ad
Log in to vote
0
Answered by 5 years ago

Datastore will only work if your game has "Enable Studio Access to API Services" enabled. If you try to use datastore in ROBLOX Studio, that is. Now if you test your script in the ROBLOX Player, then your script should work. That is, unless your script is incorrect (which is doesn't look like it).

0
I have enabled access to studio API services because if I didn't I would get error 403 Barty200 85 — 5y
0
Ok, try this tutorial: https://youtu.be/DkYupSBUpes I'm saying this because I can't find anything wrong with your code IAmNotTheReal_MePipe 418 — 5y

Answer this question