Need Help with table datastore

Sorry Bad English

I need help with ‘Table’ Datastore

Script

Save Script :

    function Saved(plr)
    	local activated = {}
    	local Ds2
    	local function CheckSaved()
    		for i,v in pairs(plr.Stats:GetChildren()) do
    			if v:isA("NumberValue") then
    				Ds2 = DS:GetDataStore("DataSave123" .. v.Name)
    				table.insert(activated, v.Value)
    			end
    		end
    	end
    	CheckSaved()
    	local succes, errormessage = pcall(function()
    		Ds2:SetAsync(plr.UserId,activated)
    	end)
    end

when i try to print the table, the output only sends me 1 print from player.Stats, While in Player.Stats there are 16 numbervalue.
Capture

Load Script :

function GetSaved(plr)
	for i,v in pairs(script:GetChildren()) do
		local Ds2 = DS:GetDataStore("DataSave123" .. v.Name)
		local save = Ds2:GetAsync(plr.userId)
		local newStat = Instance.new("NumberValue",plr.Stats)
		newStat.Name = v.Name
		newStat.Value = save or v.Value
	end
end

The problem is my script doesn’t work on/doesn’t save player value

Anyone can help me with this script please.

What is the error first? We need to know it.

1 Like

Your code seen so very complicated. I suggest you to show us the full scripts.

There no error in output or console

but i think the error is here :

but i am not sure
i’ve also tried to print ‘errormessage’ but output send me nothing.

Obviously not because the DataStore doesn’t meet any error yet. Did you try testing the game in your actual game and not inside Roblox Studio?

This is my full script :

local DS = game:GetService("DataStoreService")

    function Saved(plr)
    	local activated = {}
    	local Ds2
    	local function CheckSaved()
    		for i,v in pairs(plr.Stats:GetChildren()) do
    			if v:isA("NumberValue") then
    				Ds2 = DS:GetDataStore("DataSave123" .. v.Name)
    				table.insert(activated, v.Value)
    			end
    		end
    	end
    	CheckSaved()
    	local succes, errormessage = pcall(function()
    		Ds2:SetAsync(plr.UserId,activated)
    	end)
    end

    function GetSaved(plr)
    	for i,v in pairs(script:GetChildren()) do
    		local Ds2 = DS:GetDataStore("DataSave123" .. v.Name)
    		local save = Ds2:GetAsync(plr.userId)
    		local newStat = Instance.new("NumberValue",plr.Stats)
    		newStat.Name = v.Name
    		newStat.Value = save or v.Value
    	end
    end

    game.Players.PlayerAdded:Connect(function(plr)
    	local Stats = Instance.new("Folder", plr)
    	Stats.Name = "Stats"
    	
    	local WorldBoost = Instance.new("NumberValue", plr)
    	WorldBoost.Name = "WorldBoost"
    	WorldBoost.Value = 1
    	
    	game.ReplicatedStorage.RemoteEvents.PlayerJoin:FireClient(plr, plr.Name)
    	
    	GetSaved(plr)
    	
    	game:BindToClose(function()
    		Saved(plr)
    	end)
end)

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

Yep i did,but still doesn’t load my data

Why are you setting the Parent to the player? Do you meant to set it to the folder?

May I know if this line have anything to do with your data store?

Nope,I purposely set the parent to player just to make it so its doesn’t save

it’s just to send a player join chat message
Nothing wrong there
Client :

local function send(msg)
	game.StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = msg;
		TextSize = 15,
		Color = Color3.fromRGB(255, 255, 255);
		Font = Enum.Font.FredokaOne
	})	
end

game.ReplicatedStorage.RemoteEvents.PlayerJoin.OnClientEvent:Connect(function(Plr)
	send(Plr .. " Joined The Game!")
end)

Use protective call function when getting a DataStore.

local success, result = pcall(function()
    return Ds2:GetAsync(plr.UserId)
end

if success then
    — load data here using result variable.
else
    — print error code using result variable
end

This event shouldn’t be in the PlayerAdded scope. It should be outside the scopes.

I think the error is not from the player load script, but from the player save script.

This is the problem. After the for loop completed one iteration, this DataStore variable will be changed to something else, for example you have a multiple IntValues, and I assume that Ds2’s DataStore name will be the last NumberValue’s name. So that means all of the NumberValues in the stat will be saved to that ONE DataStore.

The best solution I can give is save the data to their respective DataStore inside the for loop scope.

it will just make the output send me a warning ‘datastore request was added to queue’.

fun fact:
This is the script that I edited from my old script :

function Saved(plr)
	for i,v in pairs(script:GetChildren()) do
		local DSnew = DS:GetDataStore("DataSave123" .. v.Name)
		DSnew:SetAsync(plr.UserId, plr.Stats[v.name].Value)  
	end
end

So that’s why you shouldn’t make separate DataStores just to save one data. It’s like getting a lot of plastic bags but you just end up putting only one item in each plastic bags.

Fixed it for you, it had some unesessary looping and way too many :GetAsync() requests, cleaned up some of it too. If anything doesn’t work let me know:

local DS = game:GetService("DataStoreService")
local Ds2 = DS:GetDataStore("DataSave123")

function Saved(plr)
	local activated = {}
	local function CheckSaved()
		for i,v in pairs(plr.Stats:GetChildren()) do
			if v:isA("NumberValue") then
				activated[v.Name] = v.Value
			end
		end
	end
	CheckSaved()
	local succes, errormessage = pcall(function()
		Ds2:SetAsync(plr.UserId,activated)
	end)
end

function GetSaved(plr)
	local save = Ds2:GetAsync(plr.userId)
	
	for i,v in pairs(script:GetChildren()) do
		local newStat = Instance.new("NumberValue",plr.Stats)
		newStat.Name = v.Name
		
		if save ~= nil then
			newStat.Value = save[v.Name]
		else
			newStat.Value = v.Value
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	local Stats = Instance.new("Folder", plr)
	Stats.Name = "Stats"

	local WorldBoost = Instance.new("NumberValue", plr)
	WorldBoost.Name = "WorldBoost"
	WorldBoost.Value = 1

	game.ReplicatedStorage.RemoteEvents.PlayerJoin:FireClient(plr, plr.Name)

	GetSaved(plr)
end)

game:BindToClose(function()
	for i, plr in pairs(game.Players:GetPlayers()) do
		Saved(plr)
	end
end)

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

I recommend reading this article on saving data.

1 Like

so is there another way to solve this problem without changing the datastore key?

I don’t quite understand. But I can say you should just save all data’s in one DataStore.