local DataStoreService = game:GetService("DataStoreService")
local leaderstatsData = DataStoreService:GetDataStore("LeaderstatsData")
game.Players.PlayerAdded:Connect(function(player)
local success, data = pcall(function()
return leaderstatsData:GetAsync(player.UserId)
end)
if not success then print(`Failed to get data: {data}`) return end
if not data then
data = {
emeralds = 0
}
end
player:WaitForChild("leaderstats"):WaitForChild("emeralds").Value = data.emeralds
player.leaderstats.emeralds.Changed:Connect(function(value)
local NewData = {
emeralds = value
}
if game:GetService("ReplicatedStorage"):WaitForChild("IsPrivateServer").Value == false then
local success2, result = pcall(function()
leaderstatsData:SetAsync(player.UserId, NewData)
end)
if not success2 then
print(`Failed to save data: {result}`)
end
end
end)
end)
and
script.Parent.set.OnServerEvent:Connect(function(plrInvoked, emeraldsSetAmount)
local foundUserId = script.Parent.Parent.currentFoundUserId
local dataToSave = {
emeralds = tonumber(emeraldsSetAmount)
}
local leaderstatsData = game:GetService("DataStoreService"):GetDataStore("LeaderstatsData")
local success, result = pcall(function()
leaderstatsData:SetAsync(foundUserId.Value, dataToSave)
end)
if not success then
print(`Failed to save data: {result}`)
end
end)
try these scripts and let me know