Clothing datastore wont work

Hello, so im trying to make a clothing datastore that saves my clothes so i dont have to buy them in-game again. Here’s my script (its suppost to print “idk something wont work lol if it doesn’t save”)

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")


game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local shirtId = character:WaitForChild("Shirt").ShirtTemplate
	local pantsId = character:WaitForChild("Pants").PantsTemplate

	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId, {shirtId, pantsId})
	end)
	if success then
		shirtId = data[1]
		pantsId = data[2]
	else
		print('idk something wont work lol')
	end

end)


game.Players.PlayerRemoving:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local shirtId = character:WaitForChild("Shirt").ShirtTemplate
	local pantsId = character:WaitForChild("Pants").PantsTemplate

	local success, errormessage = pcall(function()
		myDataStore = myDataStore:GetAsync(player.UserId)
	end)
	if success then
		print ("Player Data successfully saved")
	else
		print ("Error saving data")
		warn(errormessage)
	end
end)

everytime i enter the game it prints out ‘idk something wont work lol’, can someone please help?

You should probably be using SetAsync() instead of GetAsync() when writing data.

Also, this might have become apparent to you if you warned errormessage instead of printing something random.

you kinda switched line 12 and 30

still doesn’t work, it doesn’t warn or anything…

here’s what i meant


local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")


game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local shirtId = character:WaitForChild("Shirt").ShirtTemplate
	local pantsId = character:WaitForChild("Pants").PantsTemplate

	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId)
	end)
	if success then
		shirtId = data[1]
		pantsId = data[2]
	else
		print('idk something wont work lol')
	end

end)


game.Players.PlayerRemoving:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local shirtId = character:WaitForChild("Shirt").ShirtTemplate
	local pantsId = character:WaitForChild("Pants").PantsTemplate

	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId, {shirtId, pantsId})
	end)
	if success then
		print ("Player Data successfully saved")
	else
		print ("Error saving data")
		warn(errormessage)
	end
end)

it gives me no warnings but still doesn’t work.

What exactly are you attempting to do? Save what information and retrieve it when?

clothing datastore that saves my clothes so i dont have to buy them in-game again after leaving/dying.

What do you want to do with the saved data? Because I can assure you that it has been saved.

is Enable Studio Access to API Services enabled?

It would tell him if it wasn’t. I don’t think he has a clear idea of what he is doing with the data he has stored.

yeah bro, i dont have a clear idea. i just started making games a month ago.

it’s probably because when the player has no saved data, data is being defined as nil (GetAsync returns nil if player has no data) and data is being indexed even thought it’s nil and not a table

so to fix this, you check if data exists (not nil) when setting the shirt template and pants template

i also noticed you were setting the variable instead of the ShirtTemplate property of Shirt (if you set a property of an instance as a variable, and you change that variable, the variable will change but not the property of the instance)

to also fix that, just set the property directly

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local shirt = character:WaitForChild("Shirt")
	local pants = character:WaitForChild("Pants")
	
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId)
	end)
	
	if success then
		if data ~= nil then
			shirt.ShirtTemplate = data[1]
			pants.PantsTemplate = data[2]
		end
	else
		print('idk something wont work lol')
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local shirtId = character:WaitForChild("Shirt").ShirtTemplate
	local pantsId = character:WaitForChild("Pants").PantsTemplate
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId, {shirtId, pantsId})
	end)
	
	if success then
		print ("Player Data successfully saved")
	else
		print ("Error saving data")
		warn(errormessage)
	end
end)