Compressing & Retrieving Numbers

Hello! Today I have a very confusing code. I’m trying to make it, so it compresses the numbers and then retrieves them upon rejoining and decompresses it. However mine always returns with 525m. regardless to their stats. No errors are showing either.

local MissionsLeaderboard3 = DataStoreService:GetOrderedDataStore("MissionsLeaderboard3")
local MissionsLeaderboard4 = DataStoreService:GetOrderedDataStore("MissionsLeaderboard4")

local abbreviations = {
	"";
	"K";
	"M";
	"B";
	"T";
	"Q";
	"Qa";
	"Qi";
	"Qt";
	"Ve";
	"Vk";
	"Li";
	"Pl";
	"Ji";
	"Tr";
	"In";
	"Qe";
	"Po";
	"Qs";
	"Nm";
	"Sk";
	"Vl";
	"Pt";
	"Op";
	"Oi";
	"Qw";
	"Sz";
	"Sa"
}

local function AbbreviateNumber(Number)
	for i = 1, #abbreviations do
		if Number <= 10 ^ (i * 3) then
			return math.floor(Number / ((10 ^ ((i - 1) * 3)) / 100)) / (100) .. abbreviations[i]
		end
	end
end

local function updateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = MissionsLeaderboard3:GetSortedAsync(false, 100)
		local MissionsPage = Data:GetCurrentPage()
		local Rank = 0
		for _, data in ipairs(MissionsPage) do
			if tonumber(data.key) > 0 and Rank<5 then
				Rank += 1
				local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
				local Name = userName
				local value = data.value
				local retrivedValue = math.floor(value ~= 0 and (1.0000001^value) or 0)
				local isOnLeaderboard = false
				for i, v in pairs(game.Workspace.GlobalLeaderboard.GUI.SurfaceGui.Holder:GetChildren()) do
					if v.Player.Text == Name then
						isOnLeaderboard = true
						break
					end
				end

				if isOnLeaderboard == false then
					local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
					newLbFrame.Player.Text = Name
					newLbFrame.Rank.Text = "#"..Rank
					newLbFrame.Missions.Text = AbbreviateNumber(value)
					newLbFrame.Position = UDim2.new(0,0, newLbFrame.Position.Y.Scale + (0.2 * #game.Workspace.GlobalLeaderboard.GUI.SurfaceGui.Holder:GetChildren()), 0)
					newLbFrame.Parent = game.Workspace.GlobalLeaderboard.GUI.SurfaceGui.Holder
				end
			else
				print("error")
			end
		end
	end)
	if not success then
		print(errorMessage)
	end
end

local function updateLeaderboard2()
	local success, errorMessage = pcall(function()
		local Data = MissionsLeaderboard4:GetSortedAsync(false, 100)
		local MissionsPage = Data:GetCurrentPage()
		local Rank = 0
		for _, data in ipairs(MissionsPage) do
			if tonumber(data.key) > 0 and Rank<5 then
				Rank += 1
				local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
				local Name = userName
				local value = data.value
				local retrivedValue = math.floor(value ~= 0 and (1.0000001^value) or 0)
				local isOnLeaderboard = false
				for i, v in pairs(game.Workspace.GlobalLeaderboard2.GUI.SurfaceGui.Holder:GetChildren()) do
					if v.Player.Text == Name then
						isOnLeaderboard = true
						break
					end
				end

				if isOnLeaderboard == false then
					local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
					newLbFrame.Player.Text = Name
					newLbFrame.Rank.Text = "#"..Rank
					newLbFrame.Missions.Text = AbbreviateNumber(value)
					newLbFrame.Position = UDim2.new(0,0, newLbFrame.Position.Y.Scale + (0.2 * #game.Workspace.GlobalLeaderboard2.GUI.SurfaceGui.Holder:GetChildren()), 0)
					newLbFrame.Parent = game.Workspace.GlobalLeaderboard2.GUI.SurfaceGui.Holder
					
					if Rank == 1 then
						game.Workspace.TopPlayerSign.MeshPartMain.SurfaceGui.Frame.TextLabel.Text = "1# Overall Player Is: ".. Name
					end
				end
			end
		end
	end)
	if not success then
		print(errorMessage)
	end
end

while true do
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, errorM = pcall(function()
			local value = player.leaderstats.Rebirths.Value
			local value = math.floor(value)
			local storedValue = value ~= 0 and math.floor(math.log(value)/math.log(1.0000001)) or 0
			MissionsLeaderboard3:SetAsync(player.UserId, storedValue)
		end)
		if not success then
			warn("Line 121 errored with the following: "..errorM)
		end
	end
	
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, errorM = pcall(function()
			local value = player.upgrades.TotalMashes.Value
			local value = math.floor(value)
			local storedValue = value ~= 0 and math.floor(math.log(value)/math.log(1.0000001)) or 0
			MissionsLeaderboard4:SetAsync(player.UserId, storedValue)
		end)
		if not success then
			warn("Line 130 errored with the following: "..errorM)
		end
	end

	for _, frame in pairs(game.Workspace.GlobalLeaderboard.GUI.SurfaceGui.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	for _, frame in pairs(game.Workspace.GlobalLeaderboard2.GUI.SurfaceGui.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	updateLeaderboard()
	updateLeaderboard2()
	print("Updated")
	task.wait(15)
end

Sorry for the nudge. But I still need help.

why do you need to compress numbers

Storing them to datastores. Not part of the issue though.

Instead of this you could try something use logs.

for i = 1, #abbreviations do
		if Number <= 10 ^ (i * 3) then
			return math.floor(Number / ((10 ^ ((i - 1) * 3)) / 100)) / (100) .. abbreviations[i]
		end
	end

Like this:

local index = math.floor(math.log10(number) / 3)

return math.round(number / 10^(3*index)) .. abbreviations[index] 
1 Like

If it helps that and you can’t find where it’s at. Here’s the mark:
Storing:

local value = player.upgrades.TotalMashes.Value
			local value = math.floor(value)
			local storedValue = value ~= 0 and math.floor(math.log(value)/math.log(1.0000001)) or 0
			MissionsLeaderboard4:SetAsync(player.UserId, storedValue)

Retrieving:

local value = data.value
local retrivedValue = math.floor(value ~= 0 and (1.0000001^value) or 0)
1 Like

Still nothing, it says strings aren’t allowed in datastores.

Which side does it seem to not be working for, the compression or extraction?

Also I’m not really sure what this is meant to achieve over just storing the value itself. It doesn’t really seem to do any true compressing.

1 Like

Not sure, I’m getting no errors or error messages from my Pcalls.

Okay well you can add prints / use the debugger to see the values you get.

1 Like

Line 130, the storing part of the script. “string not allowed in datastores.”

Hm, I will need to compress the numbers, OrderedDataStores can’t take strings.

OH, I’M SO SORRY! I just realized I was abbreviating the stored value before decompressing. The original script was fine, I’m so sorry.

1 Like

Maybe this video might also help

1 Like