DatStore saving but not saving?

Hello! I was trying to make a DataStore for a game of mine and for some reason it doesn’t want to save when I exit the game. It prints(“Data Loaded”) like I want it too but the leaderstats are not there so I am confused if it’s actually saving because it throws no errors at all and doesn’t print “Saved Data” when you leave the game but if anyone could help me I would appreciate it Thank You!

Script I have now

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestDataStore = DataStoreService:GetDataStore("DataStore3")

Players.PlayerAdded:Connect(function(Player)

local DataStore = nil


local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player

local strength = Instance.new("NumberValue")
strength.Name = "Strength"
strength.Value = 0
strength.Parent = leaderstats

local ClassName = Instance.new("StringValue")
ClassName.Name = "Class"
ClassName.Value = "Noob"
ClassName.Parent = leaderstats

local Coins = Instance.new("NumberValue")
Coins.Name = "Coins"
Coins.Value = 0
Coins.Parent = leaderstats
local ClassName = Instance.new("StringValue")


local success, er = pcall(function()
	TestDataStore:GetAsync("uid1-" .. Player.UserId)
end)

if success then
	if DataStore then
		for name1, value1 in pairs(DataStore) do
			Player.leaderstats[name1].Value = value1
		end
	end

	print("Data Loaded for: " .. Player.Name)
else
	print("Data Created for: " .. Player.Name)
end
end)


Players.PlayerRemoving:Connect(function(Player)

local DataStore = {}
for name1, value1 in pairs(Player.leaderstats:GetChildren()) do
	DataStore[value1.Name] = value1.Value
end


local success1 = pcall(TestDataStore.SetAsync, TestDataStore, "uid1-" .. Player.UserId, 
DataStore)

if success1 then
	print("Saved!"..Player.Name)
end
end)

Try this:

local success, er = pcall(function()
       DataStore = TestDataStore:GetAsync("uid1-"..Player.UserId)
end)

I don’t think you’re actually doing anything with the data you retrieved other than seeing if it was successfully retrieved or not.

2 Likes

Ahhh I see alright I’ll try it thank you

Thank you very much it worked I guess it only retrieves from actual game