local DataStoreService = game:GetService("DataStoreService") local MainDataStore = DataStoreService:GetDataStore("MainDataStore") local playerUserId = "Player_"..player.UserId local MainData local success, errormessage = pcall(function() MainData = MainDataStore:GetAsync(playerUserId) end) if success then print("Loaded in successfully") print(MainData) totalPeopleInfected.Value = MainData["TotalInfected"] peopleInfected.Value = MainData["Infected"] money.Value = MainData["Money"] survivors.Value = MainData["Survivors"] totalSurvivors.Value = MainData["TotalSurvivors"] else print("There was an error retrieving data") warn(errormessage) end
It does the success part of the if statement but then I get this error
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)
local DataStoreService = game:GetService("DataStoreService") local MainDataStore = DataStoreService:GetDataStore("MainDataStore") local playerUserId = "Player_"..player.UserId local success, mainData = pcall(function()--The second argument will be what is returned from the function return MainDataStore:GetAsync(playerUserId) end) if success and mainData then print("Loaded in successfully") print(MainData) totalPeopleInfected.Value = MainData["TotalInfected"] peopleInfected.Value = MainData["Infected"] money.Value = MainData["Money"] survivors.Value = MainData["Survivors"] totalSurvivors.Value = MainData["TotalSurvivors"] else print("There was an error retrieving data") warn(mainData) end
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).