What do you want to achieve? Hello, i have a script that save table into a datastore for a leveling system, and i have an error. (The script was working perfectly until i added the test data in dataToSave, now when i launch the game it says : ServerScriptService.Main.Server.PlayerLevelHandler:37: attempt to index nil with ‘Level’)
What is the issue? The issue is when i create another data in the table i get an error like : " attempt to index nil with ‘Level’ "
Here is the script :
game.Players.PlayerAdded:Connect(function(player)
local EmployeeLevel = Instance.new("NumberValue")
EmployeeLevel.Name = "EmployeeLevel"
EmployeeLevel.Parent = player
local EmployeeXP = Instance.new("NumberValue")
EmployeeXP.Name = "CurrentXP"
EmployeeXP.Parent = EmployeeLevel
local MaxEmployeeXP = Instance.new("NumberValue")
MaxEmployeeXP.Name = "MaximumXP"
MaxEmployeeXP.Parent = EmployeeLevel
local TestLevel = Instance.new("NumberValue")
TestLevel.Name = "TestLevel"
TestLevel.Parent = player
local TestXP = Instance.new("NumberValue")
TestXP.Name = "CurrentXP"
TestXP.Parent = TestLevel
local MaxTestXP = Instance.new("NumberValue")
MaxTestXP.Name = "MaximumXP"
MaxTestXP.Parent = TestLevel
local data = nil
local success, err = pcall(function()
data = LevelsData:GetAsync(player.UserId)
end)
if success and data ~= nil then
EmployeeLevel.Value = data.Employee.Level
EmployeeXP.Value = data.Employee["Current XP"]
MaxEmployeeXP.Value = data.Employee["Maximum XP"]
TestLevel.Value = data.Test.Level
TestXP.Value = data.Test["Current XP"]
MaxTestXP.Value = data.Test["Maximum XP"]
else
EmployeeLevel.Value = 1
EmployeeXP.Value = 0
MaxEmployeeXP.Value = 20
TestLevel.Value = 1
TestXP.Value = 0
MaxTestXP.Value = 20
end
EmployeeXP.Changed:Connect(function(value)
if EmployeeXP.Value >= MaxEmployeeXP.Value then
MaxEmployeeXP.Value = math.floor(MaxEmployeeXP.Value * 1.4)
EmployeeLevel.Value = EmployeeLevel.Value + 1
EmployeeXP.Value = 0
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local dataToSave = {
["Employee"] = {
["Level"] = player.EmployeeLevel.Value,
["Current XP"] = player.EmployeeLevel.CurrentXP.Value,
["Maximum XP"] = player.EmployeeLevel.MaximumXP.Value
},
["Test"] = {
["Level"] = player.TestLevel.Value,
["Current XP"] = player.TestLevel.CurrentXP.Value,
["Maximum XP"] = player.TestLevel.MaximumXP.Value
}
}
local Success, err = pcall(function()
LevelsData:SetAsync(player.UserId, dataToSave)
end)
if Success then
print("data saved")
end
end)
What solutions have you tried so far? I tried to fix it myself and looked on forums.
The issue is that the player already has data, but not the new format. Which will cause an error only the first time they join without the newest data format.
Replace with this:
if data.Employee then
EmployeeLevel.Value = data.Employee.Level
EmployeeXP.Value = data.Employee["Current XP"]
MaxEmployeeXP.Value = data.Employee["Maximum XP"]
end
if data.Test then
TestLevel.Value = data.Test.Level
TestXP.Value = data.Test["Current XP"]
MaxTestXP.Value = data.Test["Maximum XP"]
end
if data.Testing then
TestingLevel.Value = data.Testing.Level
TestingXP.Value = data.Testing["Current XP"]
MaxTestingXP.Value = data.Testing["Maximum XP"]
end