Saving Multiple Values In Data Store

  1. *I want to save multiple Values in 1 Data Store

  2. When ever i leave with this outfit image and then join back i get this result – image

  3. ive looked up many things for Multiple saving tables ect

here is my code that im using so far

local datastore = game:GetService("DataStoreService")
local MyDataStore2 = datastore:GetDataStore("MyDataStore5")

game.Players.PlayerAdded:Connect(function(plr)
	
	
	
	print("please wait!")
	wait(1)
	print("STARTING!")
		local datafolder = Instance.new("Folder")
	datafolder.Name = "data"
	datafolder.Parent = plr
	
			local humanoiddescription = Instance.new("HumanoidDescription")
	humanoiddescription.Name = "PlayersOutFit"

	humanoiddescription.Parent = plr.data
	
	
	local Shirt = Instance.new("IntValue")
	Shirt.Name = "Shirt"
	Shirt.Parent = datafolder
	
		local Pants = Instance.new("IntValue")
	Pants.Name = "Pants"
	Pants.Parent = datafolder
	
			local Hat = Instance.new("IntValue")
	Hat.Name = "Hat"
	Hat.Parent = datafolder
	

	local PlayerUserId = "player_"..plr.UserId
	
	-- load data
	print("loading"..PlayerUserId.." Clothes Data!")
	local Data
	local suscess, errormessage = pcall(function()
	Data = MyDataStore2:GetAsync(PlayerUserId)
	end)
	
  if suscess then
print("puting the data on")
plr.data.Shirt.Value = Data
plr.data.Hat.Value = Data
plr.data.Pants.Value = Data


plr.data.PlayersOutFit.Shirt = plr.data.Shirt.Value
plr.data.PlayersOutFit.Pants = plr.data.Pants.Value
plr.data.PlayersOutFit.HatAccessory = plr.data.Hat.Value

wait(1)
print("changed")
plr:LoadCharacterWithHumanoidDescription(plr.data.PlayersOutFit)
else
	-- new player!
	print("new player")
humanoiddescription.HatAccessory = "14673292"
Hat.Value = humanoiddescription.HatAccessory
			
humanoiddescription.Shirt = "2254167941"
Shirt.Value = humanoiddescription.Shirt
			
humanoiddescription.Pants = "1673964451"
Pants.Value = humanoiddescription.Pants
	
humanoiddescription.HeadColor = Color3.fromRGB(255, 204, 153)
humanoiddescription.LeftArmColor = Color3.fromRGB(255, 204, 153)
humanoiddescription.LeftLegColor = Color3.fromRGB(255, 204, 153)
humanoiddescription.RightArmColor = Color3.fromRGB(255, 204, 153)
humanoiddescription.RightLegColor = Color3.fromRGB(255, 204, 153)
plr:LoadCharacterWithHumanoidDescription(plr.data.PlayersOutFit)
	

end

end)


game.Players.PlayerRemoving:Connect(function(player)
	print("Saving...")
	local PlayerUserId = "player_"..player.UserId



	local success, errormessage = pcall(function()
		print("saved all the values now leaving")
		local data = {}
      data.Hat = player.data.Hat.Value
      data.Shirt = player.data.Shirt.Value
      data.Pants = player.data.Pants.Value

	MyDataStore2:SetAsync(PlayerUserId, data)
	end)
	
	if success then
		print("clothes Data Saved Successfully!")
		
	else
		print("there was a error saving data")
		warn(errormessage)
		end
	
end)
2 Likes

Are you getting any errors when you are leaving the game?

plr.data.Shirt.Value = Data

At this point you set the Value to the Data dictionary when I believe you meant to do Data.Shirt.
Likewise, for the Hat and Pants.

Can’t you just save a table to a data store? That’s how my tool order editor works.

yeah i tried that but it wont saved properly im pretty sure

Can you provide a bare bones testing place for us to download? You can remove anything that is not required.

1 Like

Testing Data.rbxl (92.3 KB) here you go thanks!

1 Like

its also the PlayersLookData script in server script service

So, why don’t you use, the event Plauer.CharacterRemoving?

its saving on the player i don’t know why i need that

and its loading the values on the player when its saved and then it loads it on the HumanoidDescription

it doesn’t makes sense, like, to add values, just make something like, savin g character when it leaves or dies lemme make a script

1 Like
local data= game:GetService("DataStoreService")

game.Players.PlayerAdded:Connect(function(plr)
	
	plr.CharacterAdded:Connect(function(Character)
	LoadCharacter(Character, plr)
end)
end)

function LoadCharacter(Character, plr)
	local data
	local success = pcall(function()
		data = MyDataStore2:GetAsync(plr.UserId)
	end)
	
	if not success then
		warn("Error while catching "..Character.Name.." data.")
	end
end

So basicall,y on load character, just make a dictionary for getting Player data for example

local Information = {
ShirtName = "Yes",
PantsName = "Yes",
HatName = "NoxD",
};
data:SetAsync(plr.UserId,Information)

And to get player data just do

	local data
	local success = pcall(function()
		data = MyDataStore2:GetAsync(plr.UserId)
	end)
print(data.ShirtName);
print(data.PantsName);
print(data.HatName);
1 Like

I believe I’ve solved your issue, I tried to stay consistent with your coding style, and not remove or add too much. Hopefully it helps. :+1:

Do note that because you initially add the “default skin” to the player and have a wait(3) it takes 3 seconds before the character’s updated avatar shows. I do not know if you intentionally wanted this or not but I’ve kept it there anyways.

The reason your code on your original post did not work is because you were saving the incorrect data and you were not loading the HumanoidDescription directly based on that data.

Here is the updated code:

local datastore = game:GetService("DataStoreService")
local MyDataStore2 = datastore:GetDataStore("MyDataStore5")

game.Players.PlayerAdded:Connect(function(plr)
	print("please wait!")
	wait(3)
	print("STARTING!")
	local datafolder = Instance.new("Folder")
	datafolder.Name = "data"
	datafolder.Parent = plr
	
	local Shirt = Instance.new("IntValue")
	Shirt.Name = "Shirt"
	Shirt.Parent = datafolder
	
	local Pants = Instance.new("IntValue")
	Pants.Name = "Pants"
	Pants.Parent = datafolder
	
	local Hat = Instance.new("IntValue")
	Hat.Name = "Hat"
	Hat.Parent = datafolder
	
	local humanoiddescription = Instance.new("HumanoidDescription")
	humanoiddescription.Name = "PlayersOutFit"
	humanoiddescription.Parent = plr.data
	
	humanoiddescription.HatAccessory = "14673292"
	Hat.Value = humanoiddescription.HatAccessory
			
	humanoiddescription.Shirt = "2254167941"
	Shirt.Value = humanoiddescription.Shirt
			
	humanoiddescription.Pants = "1673964451"
    Pants.Value = humanoiddescription.Pants
	
	humanoiddescription.HeadColor = Color3.fromRGB(255, 204, 153)
	humanoiddescription.LeftArmColor = Color3.fromRGB(255, 204, 153)
	humanoiddescription.LeftLegColor = Color3.fromRGB(255, 204, 153)
	humanoiddescription.RightArmColor = Color3.fromRGB(255, 204, 153)
	humanoiddescription.RightLegColor = Color3.fromRGB(255, 204, 153)
	
	plr:LoadCharacterWithHumanoidDescription(plr.data.PlayersOutFit)
	
	print("now loading shirt stuff to see if the player had other stuff on")
	local PlayerUserId = "player_"..plr.UserId
	
	-- load data
	print("loading"..PlayerUserId.." Cloths Data!")
	local Data
	local suscess, errormessage = pcall(function()
		Data = MyDataStore2:GetAsync(PlayerUserId)
	end)
	
	if suscess and Data then
		wait(2)
		print("put the data on")
		plr.data.Shirt.Value = Data[1]
		plr.data.Hat.Value = Data[2]
		plr.data.Pants.Value = Data[3]
		
		wait(1)
		plr.data.PlayersOutFit.HatAccessory = Data[1]
		plr.data.PlayersOutFit.Shirt = Data[2]
		plr.data.PlayersOutFit.Pants = Data[3]
		print("changed")
		plr:LoadCharacterWithHumanoidDescription(plr.data.PlayersOutFit)
	end
end)

local function SaveData(player)
	print("Saving...")
	local PlayerUserId = "player_"..player.UserId
	
	local shirtdata = player.data.Shirt.Value
	local pantdata = player.data.Pants.Value
	local hatdata = player.data.Hat.Value
	local shirtdata2 = player.data.PlayersOutFit.Shirt
	local pantdata2 = player.data.PlayersOutFit.Pants
	local hatdata2 = player.data.PlayersOutFit.HatAccessory
	
	local success, errormessage = pcall(function()
		print("saved all the values now leaving")
		print(shirtdata, pantdata, hatdata)
		MyDataStore2:SetAsync(PlayerUserId, {
			shirtdata, 
			pantdata, 
			hatdata
		})
	end)
	
	if success then
		print("clouths Data Saved Successfully!")	
	else
		print("there was a error saving data")
		warn(errormessage)
	end
end

game.Players.PlayerRemoving:Connect(function(player)
	SaveData(player)
end)	

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		SaveData(player)
	end
end)
8 Likes

oh okay.

i will test this out thanks.