Number suffix not working properly

This is a number abbreviation script in the leaderstat, the problem is that it doesn’t work

local PlayerData = game.ServerStorage.PlayerData

local abbreviations = {
	K = 4,
	M = 7,
	B = 10,
	T = 13
}  
game.Players.PlayerAdded:Connect(function(player)
	
	local data = Instance.new("Folder", PlayerData)
	data.Name = player.Name
	local dataFood = Instance.new("IntValue", data)
	dataFood.Name = "🌟 Food"
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	local Food = Instance.new("StringValue", leaderstats)
	Food.Name = "Food"

	local chosenAbbreviation
	
	while wait() do
		local FoodText =tostring(math.floor(dataFood.Value))
			
			for abbreviation, digits in pairs(abbreviations) do
				if #FoodText >= digits and #FoodText < (digits + 3) then
					chosenAbbreviation = abbreviation
					break
				end
			end
		if chosenAbbreviation ~= nil then
			local digits = abbreviations[chosenAbbreviation]
			local rounded = math.floor(dataFood.Value /10 ^ (digits - 2)) *10 ^(digits - 2)
			Food.Value = string.format("%.if", rounded /10 ^ (digits - 1)) .. chosenAbbreviation
		else
			Food.Value = dataFood.Value
		end
		
	end
end)
1 Like

What doesn’t work? Is it a syntax error, and if so, where does the error occur? I need more information to help debug your script.

There are no errors in the output, what I want is to make this abbreviation of numbers, but it doesn’t work for me

Use my custom function, it will be much easier for you:

local T = {"K","M","B","T","q","Q","s","S","O","N","d","U","D"}
local function FormatNumber(n)
	n = tonumber(n) or 0
	if n < 10000 then return n end
	local d = math.floor(math.log10(n)/3)*3
	local s = tostring(n/(10^d)):sub(1,5)
	return s.." "..tostring(T[math.floor(d/3)])
end

then just do:

Food.Value = FormatNumber(dataFood.Value)
1 Like

if #FoodText >= digits and #FoodText < (digits + 3) then

FoodText is a string, so you want to convert it to a number. Having a hashtag before the variable will change it to one instead of the value. Replace the hashtags with tonumber() and that should fix it.

Actually im pretty sure OP was going for the length of the string, not what the string represented. In which case the use of # is correct here.

How do I connect the variable?

local T = {"K","M","B","T","q","Q","s","S","O","N","d","U","D"}

local function FormatNumber(n)

n = tonumber(n) or 0

if n < 10000 then return n end

local d = math.floor(math.log10(n)/3)*3

local s = tostring(n/(10^d)):sub(1,5)

return s.." "..tostring(T[math.floor(d/3)])

end

Food.Value = FormatNumber(dataFood.Value)

what variable? if you are talking about the abbreviation stuff, you dont need it. this function is stand alone, just call it with a number and it will return with like for example: 123 M

It is that in the output I get that does not identify the Food

Food is the StringValue you have in your first post ^

local datastore = game:GetService("DataStoreService")
local DS1 = datastore:GetDataStore("JumpsSaveSystem")
local PlayerData = game.ServerStorage.PlayerData
game.Players.PlayerAdded:Connect(function(player)

	local data = Instance.new("Folder", PlayerData)
	data.Name = player.Name
	local dataFood = Instance.new("IntValue", data)
	dataFood.Name = "Food"

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	local Food = Instance.new("StringValue", leaderstats)
	Food.Name = "🌟 Food"
	local T = {"K","M","B","T","q","Q","s","S","O","N","d","U","D"}

	local function FormatNumber(n)
		n = tonumber(n) or 0
		if n < 10000 then return n end
		local d = math.floor(math.log10(n)/3)*3
		local s = tostring(n/(10^d)):sub(1,5)
		return s.." "..tostring(T[math.floor(d/3)])
	end
	Food.Value = FormatNumber(dataFood.Value)
end)

It still doesn’t work for me and I don’t understand what the problem is

yes but i’d put the function outside the PlayerAdded connection so you can keep on using it for other things.

its not working because you only use the function at the time the values are created. you need to keep using it every time the value gets updated.

so I could use this script.

game.Players.PlayerAdded:Connect(function(n)

	local data = Instance.new("Folder", n)
	data.Name = n.Name
	local dataFood = Instance.new("IntValue", data)
	dataFood.Name = "Food"

	local leaderstats = Instance.new("Folder", n)
	leaderstats.Name = "leaderstats"
	local Food = Instance.new("StringValue", leaderstats)
	Food.Name = "🌟 Food"
	local T = {"K","M","B","T","q","Q","s","S","O","N","d","U","D"}
    local function FormatNumber(n)
		n = tonumber(n) or 0
		if n < 10000 then return n end
		local d = math.floor(math.log10(n)/3)*3
		local s = tostring(n/(10^d)):sub(1,5)
		return s.." "..tostring(T[math.floor(d/3)])
	end
	Food.Value = FormatNumber(dataFood.Value)
end)
game.Players.PlayerAdded:Connect(function(player)

	local data = Instance.new("Folder", player)
	data.Name = player.Name
	local dataFood = Instance.new("IntValue", data)
	dataFood.Name = "Food"

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	local Food = Instance.new("StringValue", leaderstats)
	Food.Name = "🌟 Food"
	local T = {"K","M","B","T","q","Q","s","S","O","N","d","U","D"}
    local function FormatNumber(n)
		n = tonumber(n) or 0
		if n < 10000 then return n end
		local d = math.floor(math.log10(n)/3)*3
		local s = tostring(n/(10^d)):sub(1,5)
		return s.." "..tostring(T[math.floor(d/3)])
	end
	Food.Value = FormatNumber(dataFood.Value)
end)

You didnt put the function outside the PlayerAdded connection. Place it above the PlayerAdded and then call the function every time your Food value gets changed.

I don’t want to bother you, but you could help me modify the script, it’s just that I’m lost

local PlayerData = game.ServerStorage.PlayerData
game.Players.PlayerAdded:Connect(function(player)

	local data = Instance.new("Folder", player)
	data.Name = player.Name
	local dataFood = Instance.new("IntValue", data)
	dataFood.Name = "Food"

	local leaderstats = Instance.new("Folder", PlayerData)
	leaderstats.Name = "leaderstats"
	local Food = Instance.new("StringValue", leaderstats)
	Food.Name = "🌟 Food"
	local T = {"K","M","B","T","q","Q","s","S","O","N","d","U","D"}
	local function FormatNumber(n)
		n = tonumber(n) or 0
		if n < 10000 then return n end
		local d = math.floor(math.log10(n)/3)*3
		local s = tostring(n/(10^d)):sub(1,5)
		return s.." "..tostring(T[math.floor(d/3)])
	end
	Food.Value = FormatNumber(dataFood.Value)
end)

This is a basic scripting solution, knowing how to use functions and connections and how to debug is crucial for any scripter. Scripts get ran from top to bottom, so you want to put your most used functions in a spot in the script where it can be accessed easier which is usually near the top or center. With the things needing those functions below them. For example, to fix your script you would take the FormatNumber function that I provided, and place it above the PlayerAdded connection, like so:

local T = {"K","M","B","T","q","Q","s","S","O","N","d","U","D"}
local function FormatNumber(n)
	n = tonumber(n) or 0
	if n < 10000 then return n end
	local d = math.floor(math.log10(n)/3)*3
	local s = tostring(n/(10^d)):sub(1,5)
	return s.." "..tostring(T[math.floor(d/3)])
end

local PlayerData = game.ServerStorage.PlayerData
game.Players.PlayerAdded:Connect(function(player)

	local data = Instance.new("Folder", player)
	data.Name = player.Name
	local dataFood = Instance.new("IntValue", data)
	dataFood.Name = "Food"

	local leaderstats = Instance.new("Folder", PlayerData)
	leaderstats.Name = "leaderstats"
	local Food = Instance.new("StringValue", leaderstats)
	Food.Name = "🌟 Food"
	Food.Value = FormatNumber(dataFood.Value)
end)

then every time you need to update that value, you will run it through the FormatNumber function like it does here: Food.Value = FormatNumber(dataFood.Value)

You can make it update Food.Value automatically by adding:

dataFood.Changed:Connect(function() Food.Value = FormatNumber(dataFood.Value) end)

right above the end) for the PlayerAdded connection.

1 Like
local T = {"K","M","B","T","q","Q","s","S","O","N","d","U","D"}

local function FormatNumber(n)

n = tonumber(n) or 0

if n < 10000 then return n end

local d = math.floor(math.log10(n)/3)*3

local s = tostring(n/(10^d)):sub(1,5)

return s.." "..tostring(T[math.floor(d/3)])

end

local PlayerData = game.ServerStorage.PlayerData

game.Players.PlayerAdded:Connect(function(player)

local data = Instance.new("Folder", player)

data.Name = player.Name

local dataFood = Instance.new("IntValue", data)

dataFood.Name = "Food"

local leaderstats = Instance.new("Folder", PlayerData)

leaderstats.Name = "leaderstats"

local Food = Instance.new("StringValue", leaderstats)

Food.Name = "🌟 Food"

Food.Value = FormatNumber(dataFood.Value)

dataFood.Changed:Connect(function() Food.Value = FormatNumber(dataFood.Value) end)

end)