Strange that I’ve been getting this error. Here are my 2 functions.
local function SetData(player)
local key = string.format("-Player_Key: %s", player.UserId)
local attempts = 0
if not player.Data.Loaded.Value then
return false
end
local data = {
Level = player.leaderstats.Level.Value,
}
repeat wait()
local success, errormessage = pcall(function()
PlayerDataStores:SetAsync(key, data)
end)
if success then
print(string.format("Data successfully saved for %s", player.Name))
else
attempts += 1
end
until success or attempts == 3
end
local function GetData(player)
local key = string.format("-Player_Key: %s", player.UserId)
local data
local attempts = 0
repeat wait()
local success, errormessage = pcall(function()
data = PlayerDataStores:GetAsync(key)
end)
if success then
player.leaderstats.Level.Value = data.Level
player.Data.Loaded.Value = true
else
attempts += 1
end
until success or attempts >= 3
end
player.leaderstats.Level.Value = data.Level – Line where it errors
13:20:32.355 - ServerScriptService.Silent Data:75: attempt to index nil with 'Level'
local function SetData(player)
local key = string.format("-Player_Key: %s", player.UserId)
local attempts = 0
if not player.Data.Loaded.Value then
return false
end
local data = {
Level = player.leaderstats.Level.Value,
}
repeat wait()
local success, errormessage = pcall(function()
PlayerDataStores:SetAsync(key, data)
end)
if success then
print(string.format("Data successfully saved for %s", player.Name))
else
attempts += 1
end
until success or attempts == 3
end
local function GetData(player)
local key = string.format("-Player_Key: %s", player.UserId)
local data
local attempts = 0
repeat wait()
local success, errormessage = pcall(function()
data = PlayerDataStores:GetAsync(key)
end)
if success then
player:WaitForChild("leaderstats").Level.Value = data.Level
player.Data.Loaded.Value = true
else
attempts += 1
end
until success or attempts >= 3
end
local function SetData(player)
local key = string.format("-Player_Key: %s", player.UserId)
local attempts = 0
if not player.Data.Loaded.Value then
return false
end
local data = {
Level = player.leaderstats.Level.Value,
}
repeat wait()
local success, errormessage = pcall(function()
PlayerDataStores:SetAsync(key, data)
end)
if success then
print(string.format("Data successfully saved for %s", player.Name))
else
attempts += 1
end
until success or attempts == 3
end
local function GetData(player)
local key = string.format("-Player_Key: %s", player.UserId)
local data
local attempts = 0
repeat wait()
local success, errormessage = pcall(function()
data = PlayerDataStores:GetAsync(key) or 0
end)
if success then
player:WaitForChild("leaderstats").Level.Value = data.Level
player.Data.Loaded.Value = true
else
attempts += 1
end
until success or attempts >= 3
end
In the GetData() function, you are checking if the pcall was a success, not if the data exists. If it’s a player’s first time playing, the data of course does not exist. Change: