Leaderstats script with abbreviations with saving

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    So i have a leaderstats script with abbreviations without datastore saving , i have a second script that is supposed to save it but it doesent. I have tried multiple ways but i cannot seem to get it to work.
  2. What is the issue? Include screenshots / videos if possible!
    It doesn’t save .
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve looked everywhere cannot find a solution for the problem.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder", player)
	stats.Name = "leaderstats"
	
	local lSouls = Instance.new("StringValue", stats)
	lSouls.Name = "Souls"
	lSouls.Value = "0"
	
	local Souls = Instance.new("NumberValue", player)
	Souls.Name = "nSouls"
	Souls.Value = 0
	
	local lGold = Instance.new("StringValue", stats)
	lGold.Name = "Gold"
	lGold.Value = "0"

	local Gold = Instance.new("NumberValue", player)
	Gold.Name = "nGold"
	Gold.Value = 0
	
	local SuffixList = {"", "K", "M", "B", "T", "Qa", "Qi"}
	
	local function Format(value, idp)
		local exp = math.floor(math.log(math.max(1, math.abs(value)),1000))
		local suffix = SuffixList[1 + exp] or ("e+" .. exp)
		local norm = math.floor(value * ((10 ^ idp) / (1000 ^ exp))) / (10 ^ idp)
		
		return("%.".. idp .. "f%s"):format(norm, suffix)
	end
	
	wait(1)
	while wait() do
		player:WaitForChild("leaderstats"):FindFirstChild("Souls").Value = Format(player.nSouls.Value,1)
		player:WaitForChild("leaderstats"):FindFirstChild("Gold").Value = Format(player.nGold.Value,1)
	end

end)

local dataStoreService = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait(0.1)
	local plr_key = "id_"..plr.userId
	local soulsSave = plr.nSouls
	local goldSave = plr.nGold
	
	local Get_Saved = dataStoreService:GetAsync(plr_key)
	if Get_Saved then
		soulsSave.Value = Get_Saved[1]
		goldSave.Value = Get_Saved[2]
	else
		local NFS = {soulsSave.Value, goldSave.Value}
		dataStoreService:GetAsync(plr_key, NFS)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	dataStoreService:SetAsync("id_"..plr.userId, {plr.nSouls.Value, plr.nGold.Value,})
end)

Youre saying plr.userId its plr.UserId

Also the gold and souls aren’t directly parented to the player

local soulsSave = plr.leaderstats.nSouls
local goldSave = plr.leaderstats.nGold

i changed it but it still doesn’t save , also nSouls and nGold are inside the player and not inside the leaderstats inside leaderstats are Gold and Souls which are string values. U can test the scripts for yourself if u want to you can see how it works.

Pretty sure setting an instances parent to a player instance when it’s not a folder deletes it

so if its not in a folder i cannot save the data ? So i need to put them in a folder?

You can watch >>> Roblox DataStore Tutorial - Data Stores & Saving Data - Scripting Tutorial - YouTube
for help with datastore , he explains the foundation for it and how to create one .
I’m sure you would be able to find a way to fix your problem.

Here change this

local dataStoreService = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait(0.1)
	local plr_key = "id_"..plr.userId
	local soulsSave = plr.nSouls
	local goldSave = plr.nGold
	
	local Get_Saved = dataStoreService:GetAsync(plr_key)
	if Get_Saved then
		soulsSave.Value = Get_Saved[1]
		goldSave.Value = Get_Saved[2]
	else
		local NFS = {soulsSave.Value, goldSave.Value}
		dataStoreService:GetAsync(plr_key, NFS)
	end
end)

to this

local dataStoreService = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait(0.1)
	local plr_key = "id_"..plr.userId
	local soulsSave = plr.nSouls
	local goldSave = plr.nGold
        
        local data
       local success, errormessage = pcall(function()
      data =  dataStoreService:GetAsync(player.UserId.."whatever your trying to save")
     end)
     if success then
              whatever your saving.value = data
      else 
      print("There was an error whilst getting your data")
     warn(errormessage)
	
	local Get_Saved = dataStoreService:GetAsync(plr_key)
	if Get_Saved then
		soulsSave.Value = Get_Saved[1]
		goldSave.Value = Get_Saved[2]
	else
		local NFS = {soulsSave.Value, goldSave.Value}
		dataStoreService:GetAsync(plr_key, NFS)
	end
end)

=============================================================================
Here change this

game.Players.PlayerRemoving:Connect(function(plr)
	dataStoreService:SetAsync("id_"..plr.userId, {plr.nSouls.Value, plr.nGold.Value,})
end)

to

game.Players.PlayerRemoving:Connect(function(plr)
local success, errormessage = pcall(function)
	dataStoreService:SetAsync("id_"..plr.userId, {plr.nSouls.Value, plr.nGold.Value,})
end)
if success then 
print("Player Data Saved successful")
else
print("There was an error when saving data")
warn(errormessage)--To check if it's working or not
end)

I haven’t tried it out , so tell me what errors you get.

Sorry for the late reply was busy last 3 days. I tried your code and it doesent seem to work. It doesent print anything i’ve tried in game and in studio too it doesent print anything. If you can try it out yourself , i would appreciate it. Thanks!

Can , you show me any errors you get in output , and yh ill try it myself, tomorrow cuz its kinda late now.