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)
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.
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)
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?