Accesing and loading data from DataStores

Hi! So I wanna make it when the players join, the saved data will be loaded. Here’s my script

local DS = game:GetService("DataStoreService"):GetDataStore("Data")


game.Players.PlayerAdded:Connect(function(player)
	local playerUserId = player.UserId
	local folder = Instance.new("Folder",player)
	folder.Name = "Slot1"
	local c = game.StarterPlayer.StarterCharacter:Clone()
	c.Parent = folder

	DS:GetAsync("key", {
		mesh = player.Slot1.StarterCharacter.Hair.Mesh.MeshId,
		ofs = player.Slot1.StarterCharacter.Hair.OffsetValue.Value,
		s = player.Slot1.StarterCharacter.Hair.ScaleValue.Value,

		hcR = player.Slot1.StarterCharacter.Hair.Color.R,
		hcG = player.Slot1.StarterCharacter.Hair.Color.G,
		hcB = player.Slot1.StarterCharacter.Hair.Color.B,


		scR = player.Slot1.StarterCharacter["Body Colors"].HeadColor.r,
		scG = player.Slot1.StarterCharacter["Body Colors"].HeadColor.g,
		scB = player.Slot1.StarterCharacter["Body Colors"].HeadColor.b,

		shirt = player.Slot1.StarterCharacter.Clothing.ShirtTemplate,
		pants = player.Slot1.StarterCharacter.Pants.PantsTemplate,
		class = player.Slot1.StarterCharacter.Class.Value,
		code = player.Slot1.StarterCharacter.CodeName.Value,
	})
	local data = DS:GetAsync("key")
--	player.Character.Class.Value = data.class
--[[player.Slot1.StarterCharacter.Hair.Mesh.MeshId = data.mesh --This is the part I want to load
	player.Slot1.StarterCharacter.Hair.Mesh.Offset = data.ofs
	player.Slot1.StarterCharacter.Hair.Mesh.Scale = data.s
	player.Slot1.StarterCharacter.Hair.Color.R = data.hcR
	player.Slot1.StarterCharacter.Hair.Color.G = data.hcG
	player.Slot1.StarterCharacter.Hair.Color.B = data.hcB
	player.Slot1.StarterCharacter["Body Colors"].HeadColor.r = data.scR
	player.Slot1.StarterCharacter["Body Colors"].HeadColor.g = data.scG
	player.Slot1.StarterCharacter["Body Colors"].HeadColor.b = data.scB
	player.Slot1.StarterCharacter.Clothing.ShirtTemplate = data.shirt
	player.Slot1.StarterCharacter.Clothing.ShirtTemplate = data.pants
	player.Slot1.StarterCharacter.Class.Value = data.class
	player.Slot1.StarterCharacter.CodeName.Value = data.code ]]
end)

game.StarterGui.InsertedObjects.Slot1.MouseButton1Up:Connect(function(player)
	DS:SetAsync("key",player.UserId)
end)

local function onShutDown()
	local RunService = game:GetService("RunService")
	if RunService:IsStudio() then
		wait(2)
	else
		local finished = Instance.new("BindableEvent")
		local allPlayers = game:GetService("Players"):GetPlayers()
		local leftPlayers = #allPlayers

		for _,player in ipairs(allPlayers) do
			coroutine.wrap(function()
				DS:SetAsync("key",player.UserId)
				leftPlayers -= 1
				if leftPlayers == 0 then
					finished:Fire()
				end
			end)
		end
		finished.Event:Wait()
	end
end

game.Players.PlayerRemoving:Connect(function(player)
	DS:SetAsync("key",player.UserId)
	onShutDown()
end)

When I’m trying to load the data, I get an error, that these aren’t valid numbers, either if I do it with a string value.
Error:
“Attempt to index number with mesh”
Can someone help me? If I remove that lines, I get no error.

I’m pretty sure you can’t load meshes during runtime (after script execution)

that is the MeshId I tried to load.

Yea I don’t believe you can load mesh’s during runtime

But I can’t load the class + code values either.

Hello Halacs2008.

There are some smaller issues in your coding (from what I can see). I will list them below and also post an overworked version of your code.

1st:
DS:GetAsync() returns a tuple (read only table) when given a key. The :GetAsync() function takes in only one argument, the key. It’s good practise to set the key to the user’s id and add some random string to the end to secure it from exploiters trying to access data.

2nd:
This part of the code can be removed as it does nothing:
However you need it when saving the data and not when loading the user’s data

DS:GetAsync("key", {
		mesh = player.Slot1.StarterCharacter.Hair.Mesh.MeshId,
		ofs = player.Slot1.StarterCharacter.Hair.OffsetValue.Value,
		s = player.Slot1.StarterCharacter.Hair.ScaleValue.Value,

		hcR = player.Slot1.StarterCharacter.Hair.Color.R,
		hcG = player.Slot1.StarterCharacter.Hair.Color.G,
		hcB = player.Slot1.StarterCharacter.Hair.Color.B,


		scR = player.Slot1.StarterCharacter["Body Colors"].HeadColor.r,
		scG = player.Slot1.StarterCharacter["Body Colors"].HeadColor.g,
		scB = player.Slot1.StarterCharacter["Body Colors"].HeadColor.b,

		shirt = player.Slot1.StarterCharacter.Clothing.ShirtTemplate,
		pants = player.Slot1.StarterCharacter.Pants.PantsTemplate,
		class = player.Slot1.StarterCharacter.Class.Value,
		code = player.Slot1.StarterCharacter.CodeName.Value,
	})

The :GetAsync() returns data. If you do not set a variable for example data = DS:GetData() then that part of the code does nothing.

3rd:
DS:SetAsync() Works by giving any key (e.g. userid as string) and the data you want to save.
Example:

DS:SetAsync("1234", {
  name = player.Name,
  joined = os.date(),
  accAge = player.AccountAge
})

Here is a overworked version of your code:

local DS = game:GetService("DataStoreService"):GetDataStore("Data")

game.Players.PlayerAdded:Connect(function(player)
	local playerUserId = player.UserId
	local folder = Instance.new("Folder", player)
	folder.Name = "Slot1"

    -- c = clone of the starter character
	local c = game.StarterPlayer.StarterCharacter:Clone()
	c.Parent = folder

    -- this is the key for the user's datastore
    local key = tostring(player.UserId).."_RANDOM-STRING"
    
    -- fetching data
	local data = nil -- nil is standart
    
    -- when the user has no data in their datastore ROBLOX likes to throw out an error. pcall stops your script from breaking because of the error
    pcall(function() 
       data = DS:GetAsync(KEY)
    end)

    if data ~= nil then -- if nil -> User has no data -> no loading
	   player.Character.Class.Value = data.class
       player.Slot1.StarterCharacter.Hair.Mesh.MeshId = data.mesh --This is the part I want to load
	   player.Slot1.StarterCharacter.Hair.Mesh.Offset = data.ofs
	   player.Slot1.StarterCharacter.Hair.Mesh.Scale = data.s
	   player.Slot1.StarterCharacter.Hair.Color.R = data.hcR
	   player.Slot1.StarterCharacter.Hair.Color.G = data.hcG
	   player.Slot1.StarterCharacter.Hair.Color.B = data.hcB
	   player.Slot1.StarterCharacter["Body Colors"].HeadColor.r = data.scR
	   player.Slot1.StarterCharacter["Body Colors"].HeadColor.g = data.scG
	   player.Slot1.StarterCharacter["Body Colors"].HeadColor.b = data.scB
 	   player.Slot1.StarterCharacter.Clothing.ShirtTemplate = data.shirt
	   player.Slot1.StarterCharacter.Clothing.ShirtTemplate = data.pants
	   player.Slot1.StarterCharacter.Class.Value = data.class
  	   player.Slot1.StarterCharacter.CodeName.Value = data.code 
    end
end)

game.StarterGui.InsertedObjects.Slot1.MouseButton1Up:Connect(function(player)
	DS:SetAsync(tostring(player.UserId).."_RANDOM-STRING") -- make sure that the random string for the user is the same when using the :GetAsync() / :SetAsync() functions
end)

local function onShutDown()
	local RunService = game:GetService("RunService")
	if not RunService:IsStudio() then -- checking if the user is NOT in Studio. Cancles the "else" statement
	-- 	wait(2)
	-- else
		local finished = Instance.new("BindableEvent")
		local allPlayers = game:GetService("Players"):GetPlayers()
		local leftPlayers = #allPlayers

		for _,player in ipairs(allPlayers) do
			coroutine.wrap(function()
				DS:SetAsync("key",player.UserId)
				leftPlayers -= 1
				if leftPlayers == 0 then
					finished:Fire()
				end
			end)
		end
		finished.Event:Wait()
	end
end

game.Players.PlayerRemoving:Connect(function(player)
    local data = {
		mesh = player.Slot1.StarterCharacter.Hair.Mesh.MeshId,
		ofs = player.Slot1.StarterCharacter.Hair.OffsetValue.Value,
		s = player.Slot1.StarterCharacter.Hair.ScaleValue.Value,

		hcR = player.Slot1.StarterCharacter.Hair.Color.R,
		hcG = player.Slot1.StarterCharacter.Hair.Color.G,
		hcB = player.Slot1.StarterCharacter.Hair.Color.B,


		scR = player.Slot1.StarterCharacter["Body Colors"].HeadColor.r,
		scG = player.Slot1.StarterCharacter["Body Colors"].HeadColor.g,
		scB = player.Slot1.StarterCharacter["Body Colors"].HeadColor.b,

		shirt = player.Slot1.StarterCharacter.Clothing.ShirtTemplate,
		pants = player.Slot1.StarterCharacter.Pants.PantsTemplate,
		class = player.Slot1.StarterCharacter.Class.Value,
		code = player.Slot1.StarterCharacter.CodeName.Value,
	}

	DS:SetAsync(tostring(player.UserId).."_RANDOM-STRING", data)
	onShutDown()
end) 

I hope this could help you.
If you have any questions reply to this post.

-DosenSuppe2_0

Hi! Thank you so much, it’s extremely good, it worked. Thank you!!! :smiley:

A pleasure to hear!

I wish you the best for your game.
Have a nice day and stay healthy.

Thank you! I wish you the same :smiley: