DataStore Not loading data

I’ve tested the script in studio and in a Roblox game and the data will save and load but the values will still be zero(base values). I have a part that changes the values of the leaderstats and I’ve done it manually through the console yet the data does not load when I rejoin the game.

Here is the code:

local datastore = game:GetService("DataStoreService")

local playerdata = datastore:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(player)
	task.wait()
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local purchases = Instance.new("IntValue")
	purchases.Name = "Purchases"
	purchases.Parent = leaderstats

	local rbxspent = Instance.new("IntValue")
	rbxspent.Name = "Robux Spent"
	rbxspent.Parent = leaderstats
	
	local data
	local data2
	local success, errormessage = pcall(function()
		print("test")
		data = playerdata:GetAsync(player.UserId.."-purchases")
		data2 = playerdata:GetAsync(player.UserId.."-robuxspent")
	end)
	
	if success then 
		purchases.Value = data
		rbxspent.Value = data2
		print("data success")
	else
		print("no data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage = pcall(function()
		playerdata:GetAsync(player.UserId.."-purchases", player.leaderstats.Purchases.Value)
		playerdata:GetAsync(player.UserId.."-robuxspent", player.leaderstats["Robux Spent"].Value)
	end)

	if success then
		print("plr data saved")
	else
		print("data not saved")
		warn(errormessage)
	end
end)

u made an mistake in PlayerRemoving event. u should use the SetAsync method instead

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage = pcall(function()
		playerdata:SetAsync(player.UserId.."-purchases", player.leaderstats.Purchases.Value)
		playerdata:SetAsync(player.UserId.."-robuxspent", player.leaderstats["Robux Spent"].Value)
	end)

	if success then
		print("plr data saved")
	else
		print("data not saved")
		warn(errormessage)
	end
end)
2 Likes

Thank you very much I guess I overlooked that when I was making it. :sweat_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.