Help With saving some stuff

Why is the data not saving. When i leave the game it prints the saved data which is fine but when i enter the game back again it comes up as nil

		local data
		local success,errormessage = pcall(function()
			data = DataStore2:GetAsync(player.UserId)
			return data
		end)

	
		
    if success then
			print(data)
			if data == nil then
				data = ColorValue.Value
			end
			ColorValue.Value = data
		end

		game.Players.PlayerRemoving:Connect(function(player)
			local data = ColorValue.Value
			print(data)
			local sucess,errormessage = pcall(function()
				DataStore2:SetAsync(player.UserId,data)
			end)

			if success then
				print("saved")
			else
				print("anerror")
				warn(errormessage)
			end
		end)

Do you have errors ? pls send me the full script

no errors . why does the value save when i leave the game but come up as nil when i join?

did it print “saved” or “anerror”

it printed saved
//////////////

There is no

game.Players.PlayerAdded:connect(function(player)
end)

At the top.

i only showed a part of the script so its there

And the getAsync is only player.UserId and not player.UserId, data

umm i think you read it wrong its player.UserId

Oh sorry, I just saw that that is Datastore2.

what happened
//////////////////

I want to write a post but it said “Time Out”

whats your discord
/////////////////

Are you using the Datastore from Datastore2 or just normally using roblox datastore?

local DataStore2 = game:GetService(“DataStoreService”):GetDataStore(“Colour”)

The return is the problem, try to delete that.

Did you put the game.Players.PlayedRemoving inside the playeradded function?

Hey there, I had the same issue some days ago. Try to use at the saving point

local data = ColorValue.Value.Number

And at the at :GetAsync use

if data ~= nil then 
ColorValue.Value = BrickColor.new(data) 
end

The final script should look like this:

local DataStore2 = game:GetService(“DataStoreService”):GetDataStore(“Color”)
local ColorValue = script.Parent.Parent — change it

game.Players.PlayerAdded:connect(function(player)
local data
		local success,errormessage = pcall(function()
			data = DataStore2:GetAsync(player.UserId)
		end)

	
		
    if success then
			print(data)
			if data ~= nil then
			 ColorValue.Value = BrickColor.new(data)
            end
		end
 end)

		game.Players.PlayerRemoving:Connect(function(player)
			local data2save = ColorValue.Value.Number
			print(data2save)
			local sucess,errormessage = pcall(function()
				DataStore2:SetAsync(player.UserId,data2save)
			end)

			if success then
				print("saved")
			else
				print("anerror")
				warn(errormessage)
			end
		end)