Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Saving table giving nil value when loading the data, how to fix it?

Here's my data save and load script:

local DataStoreService = game:GetService("DataStoreService");
local PD = DataStoreService:GetDataStore("PlayerData");

function SaveData(Player)
    local TableSave = {};
    TableSave.Level = Player.PlayerData.Stats.Level.Value;
    local TableItems = {};
    local ItemNumber = 0;
    for i,v in pairs(Player.PlayerData.Items:GetChildren()) do
        ItemNumber = ItemNumber + 1;
        TableItems["Item"..ItemNumber]={};
        local TI = TableItems["Item"..ItemNumber];
        TI.ItemName = v.Name;
        TI.Level = v.Level.Value;
        TI.Damage = v.Damage.Value;
    end
    table.insert(TableSave,TableItems);
    TableSave.ItemNumber = ItemNumber;
    PD:SetAsync(Player.userId,TableSave);
end

function LoadData(Player)
    local success, err = pcall(function()
        PLD = PD:GetAsync(Player.userId);
    end)
    if success then
        Player.PlayerData.Stats.Level.Value = PLD.Level;
        for i = 1,PLD.ItemNumber do
			local CurrentItem = Instance.new("Folder");
            CurrentItem.Name = PLD[1]["Item"..i].ItemName;
            local Lv = Instance.new("IntValue");
            Lv.Name = "Level";
            Lv.Value = PLD[1]["Item"..i].Level;
            Lv.Parent = CurrentItem;
            local Dm = Instance.new("IntValue");
            Dm.Name = "Damage";
            Dm.Value = PLD[1]["Item"..i].Damage;
            Dm.Parent = CurrentItem;
            CurrentItem.Parent=Player.PlayerData.Items;
        end
	else
		print("ERROR IN GET ASYNC");
    end
end

game.Players.PlayerRemoving:Connect(function(plr)
    repeat wait() until plr.PlayerData;
    SaveData(plr);
end)

game.Players.PlayerAdded:Connect(function(plr)
    repeat wait() until plr.PlayerData;
    LoadData(plr);
end)

What causes this nil problem and how to fix the problem?

3 comments

0

Could you provide more information? Which line does the error occur in? What is the full error?

0

wait sorry, i think i wrote wrong name.. edited it.. so, when loading the table from datastore, it gives me nil value.. what causes this? and how to fix the problem?

0

I know it gives nil because i tried to print the datas and instead of giving number, it gives nil value

1 answer

GetAsync returns nil if the player does not have any data stored. You have to check if the player has any data in the first place, if not, then set their data to a default data.

Here's how it would look like:

local success, err = pcall(function()
    PLD = PD:GetAsync(Player.userId);
end)
if success then
    if not PLD then
        PLD = {
            Level = 1,
            ItemNumber = 0
        }
    end
    -- rest of your code here
end

2 comments

0

But if the load system always gives nil value, then everytime we rejoin, our data will reset to default data, right?

0

When you leave, your player's data is being set using SetAsync, so the next time the player joins, GetAsync will NOT return nil and instead return the previous data saved using SetAsync.

This discussion is preserved as it was. New answers, comments, and votes are closed.