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)
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)