Datastore not working

Hello guys !
I’m having a problem to load a lot of values but those values are not all the same I have some string and Intvalue and idk how can I load them when the player join

this is my load im sorry but I have really no idea to

local data

	local success, er = pcall(function()
		data = PlayerData:GetAsync("playerdata"..player.UserId)
	end)
	if data and success then
		--print("Loaded")
	else
		--print(player.Name.." Has joined for the first time")
	end

	if success then
		if data then
			local QuestSettingsBlackBearQuest = BlackBearQuest:FindFirstChild("QuestSettingsBlackBearQuest") or Instance.new("Folder", BlackBearQuest)
			QuestSettingsBlackBearQuest.Name = "QuestSettingsBlackBearQuest"
			
			for i, v in pairs(data) do
				--Here how can I know what was the value ?
			end
			
			

		else
			print("Failed to load data")
		end
	else
		warn(er)
	end

this is my save values I know this is working

game.Players.PlayerRemoving:Connect(function(player)
	local quest = player:WaitForChild("Quest")
	
	local data = {}
	
	for i, v in pairs(player.Quest.BlackBearQuest.QuestSettingsBlackBearQuest:GetChildren()) do
		if v.Name == "QuestTask" then
			table.insert(data, i, v.Value, "StringValue")
		elseif v.Name == "QuestReward" then
			table.insert(data, i, v.Value, "IntValue")
		elseif v.Name == "QuestRewardBoost" then
			table.insert(data, i, v.Value, "StringValue")
		elseif v.Name == "QuestRewardBoostFolder" then
			table.insert(data, i, v.Value, "StringValue")
		elseif v.Name == "QuestGoal" then
			table.insert(data, i, v.Value, "IntValue")	
		elseif v.Name == "QuestAuraName" then
			table.insert(data, i, v.Value, "StringValue")	
		elseif v.Name == "QuestTitle" then
			table.insert(data, i, v.Value, "StringValue")
		end
		print(v, i, data)
	end

	local success, err = pcall(function()
		PlayerData:SetAsync("playerdata"..player.UserId, data)
	end)

	if success then
		--print("data saved")
	else
		player:Kick("There was an error loaded your data")
		warn(err)
	end
end)

im sorry to ask you this but idk how to do it, im a beginner and I’ve found nothing on the web. Thanks :slight_smile:

1 Like

Why are you passing a fourth parameter to table.insert? It doesn’t take one, so it’s useless.

It looks like you need to set data keys so you know what data belonged to what.
Try to avoid a ton of elseif statements as well, maybe make a table to hold each.

Instead of this:

if value == "Something" then
    table.insert(--[[blah blah]])
end

assign it a key:

if value == "Something" then
    data["Something"] = value
end

so then come retrieving the data:

print(data.Something)

It has something to reference to. You can then know based on what the key was what value instance to use.

I made like you said

local data

	local success, er = pcall(function()
		data = PlayerData:GetAsync("playerdata"..player.UserId)
	end)
	if data and success then
		--print("Loaded")
	else
		--print(player.Name.." Has joined for the first time")
	end

	if success then
		if data then
			print(data)
			local QuestSettingsBlackBearQuest = BlackBearQuest:FindFirstChild("QuestSettingsBlackBearQuest") or Instance.new("Folder", BlackBearQuest)
			QuestSettingsBlackBearQuest.Name = "QuestSettingsBlackBearQuest"
			
			for i, v in pairs(data) do
				print(i, v, data)
				if i.Name == "QuestAuraName" then
					local QuestAuraName = Instance.new("StringValue", QuestSettingsBlackBearQuest)
					QuestAuraName.Name = "QuestAuraName"
				end
			end
			
		else
			print("Failed to load data")
		end
	else
		warn(er)
	end

but now in my load code what should I do because i tried to put

if i.Name == "QuestAuraName" then
					local QuestAuraName = Instance.new("StringValue", QuestSettingsBlackBearQuest)
					QuestAuraName.Name = "QuestAuraName"
				end

but thats not working

and this is my save code

game.Players.PlayerRemoving:Connect(function(player)
	local quest = player:WaitForChild("Quest")
	
	print(player)
	
	local data = {}
	
	for _, v in pairs(player.Quest.BlackBearQuest.QuestSettingsBlackBearQuest:GetChildren()) do
		if v.Name == "QuestTask" then
			data["QuestTask"] = v.Value
		end	
		if v.Name == "QuestReward" then
			data["QuestReward"] = v.Value
		end	
		if v.Name == "QuestRewardBoost" then
			data["QuestRewardBoost"] = v.Value
		end	
		if v.Name == "QuestRewardBoostFolder" then
			data["QuestRewardBoostFolder"] = v.Value
		end	
		if v.Name == "QuestGoal" then
			data["QuestGoal"] = v.Value
		end	
		if v.Name == "QuestAuraName" then
			data["QuestAuraName"] = v.Value	
		end		
		if v.Name == "QuestTitle" then
			data["QuestTitle"] = v.Value
		end
		print(data)
	end

	local success, err = pcall(function()
		PlayerData:SetAsync("playerdata"..player.UserId, data)
	end)

	if success then
		--print("data saved")
	else
		player:Kick("There was an error loaded your data")
		warn(err)
	end
end)

actually this is working I had to erase .Name thanks ! :slight_smile:

1 Like

Just a quick note, I would recommend using UpdateAsync instead of SetAsync. Set should only be used to override completely old data, and should be used with caution. Update allows you to insert extra code to compare data to help prevent data loss, and even if you don’t insert this code, it still has certain safety features that SetAsync does not.

--Avoid creating an anonymous function
local success, err = pcall(PlayerData.UpdateAsync, PlayerData, "playerdata"..player.UserId, function(old)
    return data
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.