Datastore not working

I’ve made a simple datastore script, but it does not save when I rejoin my game. How can I fix this? I really new to datastore scripting.

local dss = game:GetService("DataStoreService")
local dr = dss:GetDataStore("DailyChest")

game.Players.PlayerAdded:Connect(function(p)
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"
	ls.Parent = p

	local t = Instance.new("NumberValue")
	t.Parent = ls
	t.Name = "t"
	
	local s, e = pcall(function()
		dr:GetAsync(p.UserId.."-daily")
	end)
	
	if e then
		warn(e)
	end
end)

game.Players.PlayerRemoving:Connect(function(p)
	local s, e = pcall(function()
		dr:SetAsync(p.UserId.."-daily", p:FindFirstChild("leaderstats"):FindFirstChild("t").Value)
	end)
	
	if e then
		warn(e)
	end
end)

Someone please help, I’m kind of on a time limit for this. Sorry if i’m bad at scripting, by the way.

in this script you never assign t.Value to the data that you’re returning

I use the console to do that when i’m testing.

I mean when you join into the game you load the player’s data in, but never do anything with it. Once you load the player’s data, you need to set the value to the data. Heres an example of what I mean:

local success, data= pcall(function() -- if it successfully loads then 'data' will be the data thats loaded in, otherwise data is the error.
    return dr:GetAsync(p.UserId.."-daily")
end)

if success == true then
   if data == nil then
      print("New player!")
   else
      t.Value = data
   end
else
   warn("Failed to load: ".. tostring(data))
end

Edit: for testing the data saving you should change the value on the server instead of the client, because if you change it on the client it won’t replicate and the server will save the old data.

1 Like

Thank you! This worked! This helps a lot.