Money Abbreviate in gui not working

Hi guys! :slight_smile:

How can I fix this problem?

I trying to abbreviate cash. And this isn’t working.
When the player respawn his money got lost… But the CurrencyScript has a DB. :frowning:
I can’t explain it more to you guys… :confused:

Here is some pictures and the scripts:
Képkivágás

Képkivágás3

Képkivágás4

-- MoneyUpdate [LocalScript]

local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local leaderboard = player:WaitForChild("leaderstats")
local money = leaderboard.Money
local value = script.Parent

local AbbreviateNumber = require(game.ReplicatedStorage.AbbreviateNumber)

local number = player.leaderstats.Money.Value
print(number)
script.Parent.Parent.Money2.Text = number



local text = AbbreviateNumber:Abbreviate(number)

print(text)
script.Parent.Text = text
-- AbbreviateNumber

local AbbreviateNumber = {}

local abbreviations = {
	K = 4,
	M = 7,
	B = 10,
	Q = 13
}

function AbbreviateNumber:Abbreviate(number)
local text = tostring(math.floor(number))
	
local chosenAbbreviation
	for abbreviation, digits in pairs(abbreviations) do
		if #text >= digits and #text < (digits + 3) then
			chosenAbbreviation = abbreviation
		break
		end
	end
	
		if chosenAbbreviation then
			local digits = abbreviations[chosenAbbreviation]
			
			local rounded = math.floor(number / 10 ^ (digits - 2)) * 10 ^ (digits - 2)
			
			text = "$" .. string.format("%.1f", rounded / 10 ^ (digits - 1)) .. chosenAbbreviation
		else
			text = "$" .. number
		end
		
		return text
	end

return AbbreviateNumber
-- CurrencyServer

local currencyName = "Money"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
game.Players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player	
	
	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder
	
	local ID = currencyName.."-"..player.UserId
	local savedData = nil	
	
	pcall(function()
		savedData = DataStore:GetAsync(ID)
	end)
	
	if savedData ~= nil then
		currency.Value = savedData
	else
		-- New player
		currency.Value = 100
		print("New player to the game")
		end
	end)


game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()
	
	-- When game is ready to shutdown
	
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This game is shutting down")
		end
	end
	
	wait(5)	
	
end)
3 Likes

Make sure the ResetOnSpawn property of the ScreenGui is false

I need to give the folder location to StarterPlayerScripts
folder.Name = “leaderstats”
folder.Parent = “StarterPlayerScripts”

or nah?

1 Like

Can you help me? How can I put it to StarterPlayerScripts. I changed the folder.Parent to starterplayerscripts, but nothing. Im dumb :confused:

folder.Name = “leaderstats”
folder.Parent = game.StarterPlayer.StarterPlayerScripts

Maybe?

Tried but now working :confused:
Képkivágás

Try looking at this thread, this is a more proper way of doing this and easier as well:

1 Like

In the explorer, there is a folder called starter scripts near the bottom. In starterscripts, there is a folder called StarterPlayerScripts. Put it in there. :slight_smile:

No, his “leaderstats” folder should be created in a server script in ServerScriptService, nothing is wrong with is new Instances he makes in the script above.

Hey @Mrattilla, here is some things for your datastore and to add on, it should be working properly, try printing to see if the leaderstat folder is nil after you create it.

Here you can just say local savedData

And here you can just say if savedData then, this is just to shorten some things up in your script and otherwise from that your datastore should be fine.

Also let me know if you need me to explain how to use the suffix thread I provided for you above. Make sure to mark something as a solution :white_check_mark: if it is the answer to your problem, Thanks! :grin:

1 Like

Oh, he said he needs to put his script in starterplayerscripts, but he didn’t know how to do it.

That is not necessary at all, why would he need to do that? the way he set it up should already work properly.

I just realized someone already answered that, so nvm.

And I have no idea why he would want to do that.