Learning datastore and finding no solutions

I have been trying to learn datastore for a little while now and have reached out to other devforum posts and third party resources on discord. Unfortunately have found no luck in solving my issue.

local DSS = game:GetService("DataStoreService")

function DataTable()
	local userData = {
		Stats = {
			Coins = 0;
			Emeralds = 0;
			Admin = false;
			Banned = false;
			};
		Inventory = {
			Seeds1 = 0;
			Seeds2 = 0;
		};
	}
	return userData
end

local Datastore = DSS:GetDataStore("PlayerData")
local data

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	local success, errormessage = pcall(function()
		data = Datastore:GetAsync(player.UserId) or DataTable()
		local Coins = Instance.new("IntValue")
		Coins.Parent = leaderstats
		Coins.Value = data.Stats.Coins
		Coins.Name = "Coins"
		
		local Emeralds = Instance.new("IntValue")
		Emeralds.Parent = leaderstats
		Emeralds.Value = data.Stats.Emeralds
		Emeralds.Name = "Emeralds"
		
		local Admin = Instance.new("BoolValue")
		Admin.Parent = leaderstats
		Admin.Value = data.Stats.Admin
		Admin.Name = "Admin"
		
		local Banned = Instance.new("BoolValue")
		Banned.Parent = leaderstats
		Banned.Value = data.Stats.Banned
		Banned.Name = "Banned"
	end)
	
	if success then
		print(player.Name.. "'s Data has successfully been loaded.")
	else
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		local Coins = player:WaitForChild("leaderstats").Coins
		local Emeralds = player:WaitForChild("leaderstats").Emeralds
		local Admin = player:WaitForChild("leaderstats").Admin
		local Banned = player:WaitForChild("leaderstats").Banned
		
		data.Stats.Coins = Coins.Value
		
		data.Stats.Emeralds = Emeralds.Value
		
		data.Stats.Admin = Admin.Value
		
		data.Stats.Banned = Banned.Value
		data = Datastore:SetAsync(player.UserId, DataTable())
	end)
	
	if success then
		print(player.Name.. "'s Data has successfully been saved.")
	else
		print("Erorr")
		warn(errormessage)
		print(data)
	end
end)

If anyone could direct me somewhere where I could learn the correct way to map data to its correct value on a table of datastore data then that would be very helpful. otherwise when I change data on the server then leave, and return in the studio the saved data isnt there.

1 Like

There are fixed code

This should be Server Script:

local DSS = game:GetService("DataStoreService"):GetDataStore("PlayerData")

function DataTable()
	local userData = {
		Stats = {
			Coins = 0;
			Emeralds = 0;
			Admin = false;
			Banned = false;
			};
		Inventory = {
			Seeds1 = 0;
			Seeds2 = 0;
		};
	}
	return userData
end

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	local data
	
	local success, errormessage = pcall(function()
		data = DSS:GetAsync(player.UserId) or DataTable()
		local Coins = Instance.new("IntValue")
		Coins.Parent = leaderstats
		Coins.Value = data.Stats.Coins
		Coins.Name = "Coins"
		
		local Emeralds = Instance.new("IntValue")
		Emeralds.Parent = leaderstats
		Emeralds.Value = data.Stats.Emeralds
		Emeralds.Name = "Emeralds"
		
		local Admin = Instance.new("BoolValue")
		Admin.Parent = leaderstats
		Admin.Value = data.Stats.Admin
		Admin.Name = "Admin"
		
		local Banned = Instance.new("BoolValue")
		Banned.Parent = leaderstats
		Banned.Value = data.Stats.Banned
		Banned.Name = "Banned"
	end)
	
	if success then
		print(player.Name.. "'s Data has successfully been loaded.")
	else
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local data
	
	local success, errormessage = pcall(function()
		local Coins = player:WaitForChild("leaderstats").Coins
		local Emeralds = player:WaitForChild("leaderstats").Emeralds
		local Admin = player:WaitForChild("leaderstats").Admin
		local Banned = player:WaitForChild("leaderstats").Banned
		
		data.Stats.Coins = Coins.Value
		
		data.Stats.Emeralds = Emeralds.Value
		
		data.Stats.Admin = Admin.Value
		
		data.Stats.Banned = Banned.Value
		data = DSS:SetAsync(player.UserId, DataTable())
	end)
	
	if success then
		print(player.Name.. "'s Data has successfully been saved.")
	else
		print("Erorr")
		warn(errormessage)
	end
end)

What was it you changed? The code looks the same other than the deleted print.

I changed some things with data, check if that works

Also check this tutorial:

I followed that video but he set up his stats differently

That’s learns how to use data stores…

1 Like

I have checked all of these resorces already and I am stuck. particularly with the Setasync

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		local Coins = player:WaitForChild("leaderstats").Coins
		local Emeralds = player:WaitForChild("leaderstats").Emeralds
		local Admin = player:WaitForChild("leaderstats").Admin
		local Banned = player:WaitForChild("leaderstats").Banned
		
		data.Stats.Coins = Coins.Value
		
		data.Stats.Emeralds = Emeralds.Value
		
		data.Stats.Admin = Admin.Value
		
		data.Stats.Banned = Banned.Value
		data = Datastore:SetAsync(player.UserId, data)
	end)
	
	if success then
		print(player.Name.. "'s Data has successfully been saved.")
	else
		print("Erorr")
		warn(errormessage)
	end
end)

What’s wrong?
Is this won’t save or load?

1 Like

It says it loads the data and it says it saves but when I go to change the data server-side and leave. Next time I check the data remains unchanged.

Not exactly the solution but its working now

Are you testing this in studio?
If you are you need to enable access to the DataStore API.

You can find this page by going into Game Settings in studio.

For some reason, that was on but it still won’t work