Clothing datastore won't work

Hello, so i got this script here that is suppost to just save clothing so everytime i die or leave the game i don’t have to buy the clothing again.

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


	game.Players.PlayerAdded:Connect(function(player)
		local shirtId = player.Character:WaitForChild("Shirt").ShirtTemplate
		local pantsId = player.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
			warn(errormessage)
		end

	end)


	game.Players.PlayerRemoving:Connect(function(player)
		local shirtId = player.Character:WaitForChild("Shirt").ShirtTemplate
		local pantsId = player.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)

can someone please help?

1 Like

Remove the {shirtId, pantsId} from the :GetAsync() line. Also, what is it printing?

local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(player.UserId)
end)
1 Like

i get “attempt to index nil with waitforchild”

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


game.Players.PlayerAdded:Connect(function(player)
	local shirtId = player.Character:WaitForChild("Shirt").ShirtTemplate
	local pantsId = player.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
		warn(errormessage)
	end

end)


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

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

i changed the script a bit and now i get “idk something wont work lol”

because i scripted it print that if spomething wrong.

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)

Replace the :GetAsync() inside the PlayerRemoving event:

game.Players.PlayerRemoving:Connect(function(player)
   -- ...
   local success, errormessage = pcall(function()
      myDataStore:SetAsync(tostring(player.UserId), {shirtId, pantsId})
   end)
   -- ...
end)

i get

" Unable to cast value to Object"

Try this script. Mark @Korate168’s post as the solution if this works.

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()
		print("Saving")
		myDataStore:SetAsync(player.UserId, {shirtId, pantsId})
	end)
	if success then
		print ("Player Data successfully saved")
	else
		print ("Error saving data")
		warn(errormessage)
	end
end)

Also, where does the Unable to cast value to Object error occur?

the unable to cast value to object error stopped occuring but like, it literally printed “saving” but later it didn’t print “Player Data successfully saved” or “Error saving data” or warn me.

Remove the pcall statement for a second and then see what error it gives. The pcall statement is stopping any errors from showing but they are still occurring in the background

it just says “nil” after i deleted

local success, errormessage = pcall(function()