01 | local DataStoreService = game:GetService( "DataStoreService" ) |
02 | local MainDataStore = DataStoreService:GetDataStore( "MainDataStore" ) |
03 |
04 | local 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" ] |
It does the success part of the if statement but then I get this error
1 | 09 : 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)
01 | local DataStoreService = game:GetService( "DataStoreService" ) |
02 | local MainDataStore = DataStoreService:GetDataStore( "MainDataStore" ) |
03 |
04 | local 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" ] |
Try this
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).