Datastore Set Panel not working

im trying to make a set leaderstats panel but it’s not working…

Datastore Saving Script:

local DataStoreService = game:GetService("DataStoreService")
local leaderstatsData = DataStoreService:GetDataStore("LeaderstatsData")

game.Players.PlayerAdded:Connect(function(player)
	local playerData = leaderstatsData:GetAsync(tostring(player.UserId))
	if not playerData then
		playerData = {
			emeralds = 0
		}
		leaderstatsData:SetAsync(tostring(player.UserId), playerData)
	end

	player.leaderstats.emeralds.Value = playerData.emeralds
	
end)

game.Players.PlayerAdded:Connect(function(player)
	player:WaitForChild("leaderstats").emeralds.Changed:Connect(function()
	local playerData = {
		emeralds = player.leaderstats.emeralds.Value
	}
	if game.ReplicatedStorage:WaitForChild("IsPrivateServer").Value == false then
	leaderstatsData:SetAsync(tostring(player.UserId), playerData)
		game.ServerStorage.emeraldsDataStoreChanged:Fire(player)
		end
	end)
end)




game.ServerStorage.emeraldsDataStoreChanged.Event:Connect(function(plr)
	
	local playerData = leaderstatsData:GetAsync(tostring(plr.UserId))
	if not playerData then
		playerData = {
			emeralds = 0
		}
	
	end

	plr.leaderstats.emeralds.Value = playerData.emeralds
	
end)

Data setting script:

script.Parent.set.OnServerEvent:Connect(function(plrInvoked, emeraldsSetAmount)
	local foundUserId = script.Parent.Parent.currentFoundUserId.Value
	--kick and ban player from joining while datastore is being updated. disabled in studio.
	--game.Players:BanAsync({
	--	UserIds = {foundUserId},
	--	ApplyToUniverse = true,
	--	Duration = 10,
	--	DisplayReason = "You data has been altered. Please wait 10 seconds and rejoin.",
	--	PrivateReason = "[EXECUTIVE CONSOLE] Data was set to "..emeraldsSetAmount,
	--	ExcludeAltAccounts = false
		
	--})
	
	
	local leaderstatsData = game:GetService("DataStoreService"):GetOrderedDataStore("LeaderstatsData")
	leaderstatsData:SetAsync(tostring(foundUserId), tonumber(emeraldsSetAmount))
end)

The foundUserId works, as does the event too.

Ask me if you need any more details.

Any help appreciated!

3 Likes

Can you explain exactly what isn’t working or what you’re expecting to happen?

Which part about it doesn’t work?

You may have forgotten to enable the first part of using DataStore in Studio. Enable the API in the game settings.

Edit1: Another thing, know how to use FindFirstChild and WaitForChild.
You used WaitForChild in a connection that is waiting for a player to join, and the instance is in ReplicatedStorage. This will run infinitely and will not continue with the code.

Edit2: I think that because you use 2 connections for PlayerAdded this may cause problems. There is no guarantee that the first function will be executed before or after. Leave only one connection and make it execute the two functions in the correct order.

i think what’s happening, is that its setting the data but the leaderstats is not updating when you join the game, ive tried mutliple times and that was the outcome. i think its something wrong with my datasaving script.

nope datastore is enabled and the system works perfectly fine. by edit1 and edit2 could you explain what you mean by this? im not a very good scripter.

There are some videos on YouTube that can explain it better than I can explain it, but usually the functions that execute at each “step” are not in a linear sequence, that is, your connection at the bottom of the script can be executed before the one at the top.
You can treat the connections as priority levels, where the connections involving Player and Data are of maximum priority, to avoid data loss, etc. The point is that both connections access the DataStore and both are PlayerAdded. Why not join and access the DataStore only once, besides guaranteeing the fidelity of the steps that the function will have?

About the Edit1 part, the IsPrivateServer instance is probably included in the default instances (judging by the name), that is, they are already created in the base game. The connection says :WaitForChild(), but what will it wait for if the instance is already created? Unless you constantly create the IsPrivateServer instance, your code may be infinitely waiting for something to be created that no code will create.

Are you testing it in studio or in game? Data does not save in studio unless you add this in a server script somewhere


game:BindToClose(function()
  wait
end)

i added this, heres my new datasaving script.
still not working though

local DataStoreService = game:GetService("DataStoreService")
local leaderstatsData = DataStoreService:GetDataStore("LeaderstatsData")

game.Players.PlayerAdded:Connect(function(player)
	local playerData = leaderstatsData:GetAsync(tostring(player.UserId))
	if not playerData then
		playerData = {
			emeralds = 0
		}
		leaderstatsData:SetAsync(tostring(player.UserId), playerData)
	end

	player.leaderstats.emeralds.Value = playerData.emeralds
	
	--EXPERIMENTAL
	player:WaitForChild("leaderstats").emeralds.Changed:Connect(function()
		local playerData = {
			emeralds = player.leaderstats.emeralds.Value
		}
		if game.ReplicatedStorage:WaitForChild("IsPrivateServer").Value == false then
			leaderstatsData:SetAsync(tostring(player.UserId), playerData)
			game.ServerStorage.emeraldsDataStoreChanged:Fire(player)
		end
	end)
	--END OF EXPERIMENT
	
end)

--game.Players.PlayerAdded:Connect(function(player)
	--player:WaitForChild("leaderstats").emeralds.Changed:Connect(function()
	--local playerData = {
	--	emeralds = player.leaderstats.emeralds.Value
--	}
--	if game.ReplicatedStorage:WaitForChild("IsPrivateServer").Value == false then
--	leaderstatsData:SetAsync(tostring(player.UserId), playerData)
--		game.ServerStorage.emeraldsDataStoreChanged:Fire(player)
--		end
--	end)
--end)




game.ServerStorage.emeraldsDataStoreChanged.Event:Connect(function(plr)
	
	local playerData = leaderstatsData:GetAsync(tostring(plr.UserId))
	if not playerData then
		playerData = {
			emeralds = 0
		}
	
	end

	plr.leaderstats.emeralds.Value = playerData.emeralds
	
end)


--@BlueBloxBlast said to add this

game:BindToClose(function()
	wait()
end)

I said it would be better to remove WaitForChild.
Your code is broken because of this.

IsPrivateServer is a bool value inserted by a server script 0.5 seconds after the server is created. the script that handles it in serverscriptservice:

wait(0.5)
local privCheck = Instance.new("BoolValue")
privCheck.Name = "IsPrivateServer"
privCheck.Parent = game.ReplicatedStorage
if game.PrivateServerId ~=  "" and game.PrivateServerOwnerId ~= 0 then
	print("private server")
	privCheck.Value = true
	local aID = Instance.new("NumberValue")
	aID.Name = "PrivateServerOwnerId"
	aID.Value = game.PrivateServerOwnerId
	aID.Parent = game.ReplicatedStorage
			
else
	privCheck.Value = false
	print("non-private server")
end
	


i used waitforchild because wouldn’t findfirstchild just look for it once and if it’s not there then it would not work?

did you get any errors in output?

This will continue to crash your code because the instance was already created before the player joined.

You were using regular DataStore in one script and OrderedDataStore in another for the same data.

Edit: You also have two separate PlayerAdded connections that could potentially conflict.

no i didn’t. it all usually works it’s just when editing it

when i tried to use GetDataStore() instead of GetOrderedDataStore() in the setting script, it just broke and reset my emeralds

no i don’t have 2 playeradded connections… i changed it here

That’s probably because you were trying to access player.leaderstats before it’s loaded.

at least, you need to use :GetDataStore to access your LeaderstatsData.

i don’t get what you’re saying. i tried using GetDataStore to access the leaderstats data, but all it did was reset my stats.

where is this script located in?