Datastore Not Loading Data

What do you want to achieve?
I want to have stats in a players leaderstats folder save when they leave and load the stored data when they join.

What is the issue?
The players data seems to save, (their userid is printed, which means it successfully saved) but does not load, and instead just defaults to 0.

What solutions have you tried so far?
I have looked for a very long time on the developer forums and the dev hub, but none of the solutions fixed my problem. (Switching the script, publishing to roblox, enabling and disabling access to datastore, etc).

I have already enabled access to datastores, and I am not getting any errors in console. Anything is appreciated. Please help! :frowning:

local datastores = game:GetService("DataStoreService")
local datastore = datastores:GetDataStore("DataStore")
local players = game:GetService("Players")

local function deserializeData(player, data)
	local leaderstats = player.leaderstats
	for statName, statValue in next, data do
		local stat = leaderstats:FindFirstChild(statName)
		if statName then
			stat.Value = statValue
		end
	end
end

local function serializeData(player)
	local data = {}
	local leaderstats = player.leaderstats
	for _, stat in ipairs(leaderstats:GetChildren()) do
		data[stat.Name] = stat.Value
	end
	return data
end

local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local robux = Instance.new("IntValue")
	robux.Name = "Robux"
	robux.Parent = leaderstats

	local multiplier = Instance.new("IntValue")
	multiplier.Name = "Multiplier"
	multiplier.Parent = leaderstats

	local success, result = pcall(function()
		return datastore:GetAsync("Stats_"..player.UserId)
	end)

	if success then
		if result then
			deserializeData(player, result)
		end
	else
		warn(result)
	end
end

local function onPlayerRemoving(player)
	local data = serializeData(player)

	local success, result = pcall(function()
		return datastore:SetAsync("Stats_"..player.UserId, data)
	end)

	if success then
		if result then
			print(result)
		end
	else
		warn(result)
	end
end

local function onServerShutdown()
	for _, player in ipairs(players:GetPlayers()) do
		local data = serializeData(player)

		local success, result = pcall(function()
			return datastore:SetAsync("Stats_"..player.UserId, data)
		end)

		if success then
			if result then
				print(result)
			end
		else
			warn(result)
		end
	end
end

players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)
game:BindToClose(onServerShutdown)

Edit: I also have a second script that only works in one game, but not the other.

Edit 2: I fixed it with a plugin called Datastores!

1 Like

so ur making a normal datastore when a player joins their leaderstats value loads and and when they leave it saves the value right?

Yes, at least I’m trying to do that

local DataStore = game:GetService("DataStoreService")
local SaveDataStore = DataStore:GetDataStore("SaveGame")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local Folder = Instance.new("Folder")
	Folder.Name = "leaderstats"
	Folder.Parent = Player
	
	local ValueNameHere = Instance.new("NumberValue")
	ValueNameHere.Name = "TestValue"
	ValueNameHere.Parent = Folder
	ValueNameHere.Value = SaveDataStore:GetAsync(Player.UserId) or 0
end)

Players.PlayerRemoving:Connect(function(Player)
	local leaderstats = Player.leaderstats:FindFirstChild("TestValue").Value
	
	SaveDataStore:SetAsync(Player.UserId, leaderstats)
end)

Thats how i do it.

Tell me if it works or not alr?

no pcall

(limit hahahhahsssss)

That’s not working for me, and I think it may be the script of the button since I tried with a different datastore I used for a separate game that uses a tool instead (the tool works with saving data) and it didn’t work. The script for the button is attached below

local gui = script.Parent
local btn1 = gui.RobuxBtn
local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local rbx = leaderstats:WaitForChild("Robux")
local multiplier = leaderstats:WaitForChild("Multiplier")
local enabled = true

btn1.MouseButton1Click:Connect(function()
	if enabled == true then
		if multiplier.Value > 1 then
			rbx.Value = rbx.Value + (1 * multiplier.Value)
		else
			rbx.Value = rbx.Value + 1
		end
		enabled = false
		wait(0.1)
		enabled = true
	else
		wait(0.0001)
	end
end)

i think the actually problem if when u check: “if result then”, result really means that if exist already a key so the data never will load

Do you think I should remove if result then? I was using it to make sure data wouldn’t be wiped from loading too fast.

yeah of course‎ ‏‏‎‎ ‏‏‎‎ ‏‏‎‎ ‏‏‎‎ ‏‏‎

I’ll try that and tell you what happens.

ok‎ ‏‏‎‎ ‏‏‎‎ ‏‏‎‎ ‏‏‎‎ ‏‏‎‎ ‏‏‎‎ ‏‏‎

Tried that and still not loading data
Here’s the changed script

local datastores = game:GetService("DataStoreService")
local datastore = datastores:GetDataStore("DataStore")
local players = game:GetService("Players")

local function deserializeData(player, data)
	local leaderstats = player.leaderstats
	for statName, statValue in next, data do
		local stat = leaderstats:FindFirstChild(statName)
		if statName then
			stat.Value = statValue
		end
	end
end

local function serializeData(player)
	local data = {}
	local leaderstats = player.leaderstats
	for _, stat in ipairs(leaderstats:GetChildren()) do
		data[stat.Name] = stat.Value
	end
	return data
end

local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local robux = Instance.new("IntValue")
	robux.Name = "Robux"
	robux.Parent = leaderstats

	local multiplier = Instance.new("IntValue")
	multiplier.Name = "Multiplier"
	multiplier.Parent = leaderstats

	local success, result = pcall(function()
		return datastore:GetAsync("Stats_"..player.UserId)
	end)

	if success then
		deserializeData(player, result)
	else
		warn(result)
	end
end

local function onPlayerRemoving(player)
	local data = serializeData(player)
	local success, result = pcall(function()
		return datastore:SetAsync("Stats_"..player.UserId, data)
	end)

	if success then
			print(result)
	else
		warn(result)
	end
end

local function onServerShutdown()
	for _, player in ipairs(players:GetPlayers()) do
		local data = serializeData(player)

		local success, result = pcall(function()
			return datastore:SetAsync("Stats_"..player.UserId, data)
		end)

		if success then
				print(result)
		else
			warn(result)
		end
	end
end

players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)
game:BindToClose(onServerShutdown)

u could try:

if result then
        --load some data
        else
        --load default data
end

I will try that tomorrow, it is late where I am.

ok‎ ‏‏‎‎ ‏‏‎‎ ‏‏‎‎ ‏‏‎‎ ‏‏‎‎ ‏‏‎‎ ‏‏‎

I fixed this with a plugin called Datastores, which helped me make a fairly simple yet efficient one.

2 Likes

Could you send me the link to this? I can’t find it…

DataStorer - Roblox – plugin
How To Make A Simple Data Store! – documentation
Sorry I replied so late.

Don’t worry, I found it before. Thanks for replying anyway.

1 Like