Having trouble saving player's shirt and pants

So I have a shop that changes your outfit when you click on it. I am trying to make a datastore that will save the character’s last outfit they had on. My attempt at doing it wasn’t successful. What did I do wrong?

ServerScript:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Outfit")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		
		local success, errormessage = pcall(function()
			local information = {}
			
			information.OutfitTop = player.Character:WaitForChild("Shirt").ShirtTemplate
			information.OutfitButtom = player.Character:WaitForChild("Pants").PantsTemplate
			
			local PlayerShirt = player.Character:FindFirstChild("Shirt").ShirtTemplate
			local PlayerPants =player.Character:FindFirstChild("Pants").PantsTemplate
			
			PlayerShirt = information.OutfitTop
			PlayerPants = information.OutfitButtom
			DataStore:SetAsync(player.UserId, information)
		end)
		if success then
			print("Successfuly Saved!")

		elseif errormessage then
			warn(errormessage)
		end
		
	end)
	
end)


game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	local success, errormessege = pcall(function()
		return DataStore:GetAsync(playerUserId)
	end)
	
	if success then
		print(player.Name.."Outfit Successfuly Stored!")
	end
	
end)

Try this:

  1. Add HttpService = game:GetService("HttpService") at the top of your script
  2. Change DataStore:SetAsync(player.UserId, information) to DataStore:SetAsync(player.UserId, HttpService:JSONEncode(information))

Explanation:

The SetAsync function requires you to input a string as a value AFAIK, and entering a table without first turning it into a string does not satisfy these parameters.

It doesn’t, it can take a non-mixed-key table, a string, integer or boolean. It’s JSON encoded when you go to call :SetAsync on the backend, and JSON decoded when you call :GetAsync

1 Like

I reckon it’s saving correctly (with the exception of a few useless variables) but you didn’t load the data anywhere. Use the GetAsync method to get the data from the datastore.

Is this better?

local  HttpService = game:GetService("HttpService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("datastore")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		
		local success, errormessage = pcall(function()
			local information = {}
			
			information.OutfitTop = player.Character:WaitForChild("Shirt").ShirtTemplate
			information.OutfitButtom = player.Character:WaitForChild("Pants").PantsTemplate
			
			
			DataStore:SetAsync(player.UserId, HttpService:JSONEncode(information))
		end)
		if success then
			print("Successfuly Saved!")

		elseif errormessage then
			warn(errormessage)
		end
		
	end)
	
end)


game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	local success, errormessege = pcall(function()
		return DataStore:GetAsync(playerUserId)
	end)
	
	if success then
		print(player.Name.."Outfit Successfuly Stored!")
	end
	
end)

It didn’t work. The outfit i bought still didn’t save when i rejoin

Taking a second look at this, it would seem you have your Removing and Added events swapped as well. You’re loading the data in whenever the player leaves and saving it whenever they first join. (In addition to not changing the player’s shirt/pants whenever the data is loaded in)

Also to further iterate on what @7z99 said previously, SetAsync is able to take tables as a data value.

I did load the data. at the bottom of the script i did return Get:Async. Even though i dont knwo what return does lol

Isn’t that how its suppose to be it saves when player join?

This article may be able to help

Example of return being used:

function Scream()
 return "AAAAA"
end
print( Scream() ) -- Prints "AAAAA"

You would want to save whenever the player changes their clothing or when they’re leaving the game, otherwise, you’re saving nothing.

The process that’s happening right now is as follows:

  1. Player joins game.
  2. Player’s current clothes are saved to a datastore.
  3. Player leaves game.
  4. Data is obtained from datastore and not applied.
    (loop)

How you want it to happen is as follows:

  1. Player joins game.
  2. If a datastore for that player exists, apply data obtained.
  3. Player leaves game.
  4. Player’s current clothes are saved to a datastore.
    (loop)

So to save the player’s current stuff is it GetAsync or SetAsync? I’m pretty sure I did it right but it still didn’t work…

local  HttpService = game:GetService("HttpService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("datastore")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		
		local success, errormessage = pcall(function()
			local information = {}
			
			information.OutfitTop = player.Character:WaitForChild("Shirt").ShirtTemplate
			information.OutfitButtom = player.Character:WaitForChild("Pants").PantsTemplate
			
			
			DataStore:GetAsync(player.UserId, HttpService:JSONEncode(information))
		end)
		if success then
			print("Successfuly Saved!")

		elseif errormessage then
			warn(errormessage)
		end
		
	end)
	
end)


game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	local success, errormessege = pcall(function()
		return DataStore:SetAsync(playerUserId)
	end)
	
	if success then
		print(player.Name.."Outfit Successfuly Stored!")
	end
	
end)




Saving is SetAsync, loading is GetAsync. Think of it as you’re setting the datastore and getting the datastore.

Strange then why isn’t it working /: it should save the player’s last otufit they had on

So I rewrote some things but it gave me an error

local  HttpService = game:GetService("HttpService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("datastore")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		
		local success, errormessage = pcall(function()
			local information = {}
			
			information.OutfitTop = player.Character:WaitForChild("Shirt").ShirtTemplate
			information.OutfitButtom = player.Character:WaitForChild("Pants").PantsTemplate
			
			
			DataStore:GetAsync(player.UserId, HttpService:JSONEncode(information))
		end)
		if success then
			print("Data Retrieved")

		elseif errormessage then
			warn(errormessage)
		end
		
	end)
	
end)


game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()

			local information = {}

			information.OutfitTop = player.Character:WaitForChild("Shirt").ShirtTemplate
		information.OutfitButtom = player.Character:WaitForChild("Pants").PantsTemplate
		
		DataStore:SetAsync(player.UserId, HttpService:JSONDecode((information)))
		
	end)
	if success then
		print("Data Succesfully Saved!")
		
	elseif errormessage then
		warn(errormessage) -- Error here
	end

end)

image

Its saying attempt to index nill with Character:FindFirstChild("Shirt):WaitForChild(“ShirtTemplate”)

I’d dial back a bit, try to save and read a coin … Then move on to items, then the JSONEncode
You do have http service turned on right.

No I dont have it turned on yet. Do I have to turn it on?

And I really wanna know how to save shirts and pants for the game im making

Sounds good, but 1st you need to get anything working … try not using HttpService or JSONEncode just to see if you can get that part going.