local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore("TestDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local studs = Instance.new("IntValue")
studs.Name = "Studs"
studs.Parent = leaderstats
local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Parent = leaderstats
local PlrID = "Player_"..player.UserId
local getData
local success, errormessage = pcall(function()
getData = Data:GetAsync(PlrID)
end)
if success then
player.leaderstats.Studs.Value = data.Studs
player.leaderstats.Rebirths.Value = data.Rebirths
else
print("Error loading data..")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local PlrID = "Player_"..player.UserId
local data = {
Studs = player.leaderstats.Studs.Value;
Rebirths = player.leaderstats.Rebirths.Value;
}
local success, errormessage = pcall(function()
Data:SetAsync(PlrID, data)
end)
if success then
print("✅ Data saved!")
else
warn("⚠ Error when saving data. Contact Xelb")
end
end)
My issue: ServerScriptService.DataStore:28: attempt to index nil with ‘Studs’
I feel like it has something to do with saving the table, but I’m unsure. Sorry if my code is weird. This is my first time working with datastores, and I’m trying to get the basics down.
I found the problem with your code, specifically this line here:
Your loaded data table is called getData, not data. So, in this case, data.Studs is nil.
Rewritten version code here, which works perfectly fine:
local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore("TestDataStore")
-- I made the loading and saving into a function, and they get called when needed.
function LoadData(Player)
local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderstats"
local studs = Instance.new("IntValue", leaderstats)
studs.Name = "Studs"
local rebirths = Instance.new("IntValue", leaderstats)
rebirths.Name = "Rebirths"
local PlrID = "Player_"..Player.UserId
local getData
local success, errormessage = pcall(function()
getData = Data:GetAsync(PlrID)
end)
if success and getData then
Player.leaderstats.Studs.Value = getData.Studs
Player.leaderstats.Rebirths.Value = getData.Rebirths
elseif success and not Data then
-- If loading succeded, but no data is saved for the player
elseif not success then
print('⚠ Error loading data.')
end
end
function SaveData(Player)
local PlrID = "Player_"..Player.UserId
local data = {
Studs = Player.leaderstats.Studs.Value;
Rebirths = Player.leaderstats.Rebirths.Value;
}
local success, errormessage = pcall(function()
Data:SetAsync(PlrID, data)
end)
if success then
print("✅ Data saved!")
else
warn("⚠ Error when saving data. Contact Xelb")
end
end
game.Players.PlayerAdded:Connect(LoadData)
game.Players.PlayerRemoving:Connect(SaveData)
-- We have this here just in case the server closes for whatever reason
game:BindToClose(function()
for i, v in pairs(game.Players:GetPlayers()) do
SaveData(v)
end
end)