Datastore result always outputting nil

Hi, im having issues loading data from a datastore, ive checked and the items save correctly, its just the result in the pcall which always outputs nil nothing im doing is fixing it

script


local datastoreservice = game:GetService("DataStoreService"):GetDataStore("inventory")
local UserInputService = game:GetService("UserInputService")


local template = game.ReplicatedStorage:WaitForChild("Template")
local rStorage = game:GetService("ReplicatedStorage")
local dataFunc = rStorage:WaitForChild("GetData")

game.Players.PlayerAdded:Connect(function(player)
	local itemFolder = Instance.new("Folder"):Clone()
	local price = Instance.new("NumberValue"):Clone()
	local imageLabel = Instance.new("ImageLabel") :Clone()





	local data
	local success, result = pcall(function()
		data = datastoreservice:GetAsync("Player_"..player.UserId)
	end)
	if success then
		print(result)
		if result ~= nil then
		for i, v in pairs(result) do
			local item = game.ServerStorage.Items:FindFirstChild(v)
			if item then 
				local clone = item:Clone()
				clone.Parent = game.ReplicatedStorage:FindFirstChild(player.Name)
			end
		end
		else
			
		end
		
	end







end)
local players = game:GetService("Players")
local runService = game:GetService("RunService")

function saveData(plr, newData)
	local plrId = "Player_"..plr.UserId
	local success, err = pcall(function()
		datastoreservice:SetAsync(plrId, newData)
	end)
	if err then
		warn(err)
	end
end

players.PlayerRemoving:Connect(function(plr)
	local item = game.ReplicatedStorage:FindFirstChild(plr.Name)
	for i, v in pairs(item:GetChildren()) do
		saveData(plr, v.Name)
		print(v.Name)
	end
end)



game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() then return end
	for index, plr in pairs(players:GetPlayers()) do
		local item = game.ReplicatedStorage:FindFirstChild(plr.Name)
		for i, v in pairs(item:GetChildren()) do
			saveData(plr, v.Name)
			print(v.Name)
		end
	end
end)


1 Like

loop through the data variable since youre saving the data collected from the datastore to it, if you wanted to get the data like youre trying to you would need to do something like this:

local Success, Data = pcall(function()
   return DataStore:GetAsync()
end)
2 Likes

im now not getting nil, however, im getting an error in a for loop

can you print data and post what it prints

2 Likes

image

1 Like

it seems to be outputting just one of the items and not in table form

1 Like

It’s telling you the problem. It was expecting a table {} but it got a string “”.

1 Like

shouldnt it automatically put it in a table when saving since there is multiple values?

Data is not automatically sorted into a table, you need to do it manually.

local saveData = { --example
    ["Money"] = player.leaderstats.Money.Value,
    ["DataVersion"] = player.DataVersion.Value
}

local success, result = pcall(DataStore.UpdateAsync, DataStore, function(old)
    return saveData
end)
1 Like

Show us the data you’re saving … 12345koip has that formatted correctly as a table.

im saving the names of items then cloning them once the player joins

function saveData(plr)
local newData = {}
local plrId = “Player_”…plr.UserId
local success, err = pcall(function()
datastoreservice:SetAsync(plrId, newData)
end)
if err then
warn(err)
end
end

players.PlayerRemoving:Connect(function(plr)
local item = game.ReplicatedStorage:FindFirstChild(plr.Name)
for i, v in pairs(item:GetChildren()) do
saveData(plr, v.Name)
print(v.Name)
end
end)

im basically looking into the folder in replicated storage with the players name attatched, it checks the items inside of it and saves the names, once the player joins, it should go through the table of items and clone them from serverstorage

You are replacing the data on the data store with the name of an item, of every item on your game by the looks… That is not what you want, you probably want to make it into a table and then save it

Something like

local t = {}

for _, item in pairs(game:GetService("ReplicatedStorage").PlayerItems:GetChildren()) do
    table.insert(t, item.Name)
end

-- save t to DataStore instead.

Sorry if the code is eefy, I’m on phone.

1 Like

the folder refered to is where the players items are stored

local DataStoreService = game:GetService("DataStoreService")

local function saveData(plr)
    local plrId = "Player_" .. plr.UserId
    local newData = {} -- add your data here
    local success, err = pcall(function()
        DataStoreService:GetDataStore(plrId):SetAsync(plrId, newData)
    end)
    if not success then
        warn(err)
    end
end

game.Players.PlayerRemoving:Connect(function(plr)
    saveData(plr)
end)

1 Like

This works, but when you close the game you would have to put an artificial wait for it to be alright

2 Likes

Two problems here.

  1. Rapid-firing the data store. This can cause issues with data saving.
  2. Sending an arguement that is never used within the procedure. You are constantly overwriting the old data with a blank table, never actually saving any data.

also, please format your code using backticks (`) or the “</>” button in your post editor.

1 Like


i did this and entered my data but now im getting this error
script


local function saveData(plr)
    local plrId = "Player_" .. plr.UserId
    local newData = {game.ReplicatedStorage:FindFirstChild(plr.Name):GetChildren()} -- add your data here
    local success, err = pcall(function()
        DataStoreService:GetDataStore(plrId):SetAsync(plrId, newData)
    end)
    if not success then
        warn(err)
    end
end

if you are wondering, this is the folder it saves from
image

I saw you trying to save an array obtained using GetChildren().

You can only save arrays/dictionaries containing:

  • Strings
  • Numbers
  • Booleans

You cannot save instances.

1 Like