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

Why do my save load script not work? It gives me errors when trying to load the data

I leveled up my character and got some items, when i rejoined, it didn't load my previous data, It gave me level 0 and didn't load any items (it didn't give any output for level problem, but it gave this output for item problem: 'for' limit must be a number), but PLD.ItemNumber is a number, because It is from "ItemNumber" variable in the saving function. How to fix it?

Edit: Actually, when loading the table from datastore, it gives nil value.. how to fix the nil loading?

Code:

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
	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)

2 answers

local DataStoreService = game:GetService("DataStoreService");
local PD = DataStoreService:GetDataStore("PlayerData");
ItemNumber = 1
function SaveData(Player)
    local TableSave = {};
    TableSave.Level = Player.PD.Stats.Level.Value;
    local TableItems = {};
    local ItemNumber = 0;
    for i,v in pairs(Player.PlayerData.Items:GetChildren()) do
if ItemNumber then
        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
    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)

1 comment

0

Still not working

Oh. You got the error because you were giving it a table as a limit in your for in pairs at line 9. If you were to print it you would get:

object,object,object

The reason was becuase you were not giving the length of the array, but the array itself. To get the length, do this:

local items = Player.PlayerData.Items:GetChildren()

for i,v in pairs (#items) do
	--Code
end

I hope this helped!

1 comment

0

Never mind, this answer is incorrect

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