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