Decimals are going crazy in TextLabels

So I was working on a stats multiplier based on score you get in the game and for some reason the multiplier text glitched and it started showing a lot of decimals instead of showing the exact number I wanted it to show.

In this image you can see:

  • A comparison between stats without the multiplier and stats with the multiplier
  • The original multiplier at the bottom right corner
  • Rocket Cooldown text changed as well even though it’s not supposed to change

Some stuff that you need to know:

  • The multiplied stats are based on the NumberValues (base stats) in player stats and are multiplied in the local script that changes the text to all stats in the Total Stats frame.
  • The score goal also became decimals for some reason and that is NOT supposed to happen.

Please help! :pray:

EDIT: If you need to see the script for the TextLabels in the Total Stats frame tell me.

2 Likes

Could you please share the script for the TextLabels?

local RocketDamage = script.Parent.Page1.RocketDamage
local RocketCooldown = script.Parent.Page1.RocketCooldown
local RocketSpeed = script.Parent.Page1.RocketSpeed
local BlastRadius = script.Parent.Page1.BlastRadius
local MovementSpeed = script.Parent.Page2.MovementSpeed
local MaxHealth = script.Parent.Page2.MaxHealth
local PlayerRadius = script.Parent.Page2.PlayerRadius
local PlayerStats = game.Players.LocalPlayer:WaitForChild("player stats")
local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
RocketDamage.Text = "Rocket Damage: "..PlayerStats:WaitForChild("rocket damage").Value * PlayerStats:WaitForChild("multiplier").Value
RocketCooldown.Text = "Rocket Cooldown: "..PlayerStats:WaitForChild("rocket cooldown").Value * PlayerStats:WaitForChild("multiplier").Value
RocketSpeed.Text = "Rocket Speed: "..PlayerStats:WaitForChild("rocket speed").Value * PlayerStats:WaitForChild("multiplier").Value
BlastRadius.Text = "Blast Radius: "..PlayerStats:WaitForChild("blast radius").Value * PlayerStats:WaitForChild("multiplier").Value
MovementSpeed.Text = "Movement Speed: "..Character:WaitForChild("Humanoid").WalkSpeed
MaxHealth.Text = "Max Health: "..Character:WaitForChild("Humanoid").MaxHealth
PlayerRadius.Text = "Player Radius: "..PlayerStats:WaitForChild("player radius").Value * PlayerStats:WaitForChild("multiplier").Value
PlayerStats:WaitForChild("rocket damage"):GetPropertyChangedSignal("Value"):Connect(function()
	RocketDamage.Text = "Rocket Damage: "..PlayerStats:WaitForChild("rocket damage").Value * PlayerStats:WaitForChild("multiplier").Value
end)
PlayerStats:WaitForChild("rocket cooldown"):GetPropertyChangedSignal("Value"):Connect(function()
	RocketCooldown.Text = "Rocket Cooldown: "..PlayerStats:WaitForChild("rocket cooldown").Value
end)
PlayerStats:WaitForChild("rocket speed"):GetPropertyChangedSignal("Value"):Connect(function()
	RocketSpeed.Text = "Rocket Speed: "..PlayerStats:WaitForChild("rocket speed").Value * PlayerStats:WaitForChild("multiplier").Value
end)
PlayerStats:WaitForChild("blast radius"):GetPropertyChangedSignal("Value"):Connect(function()
	BlastRadius.Text = "Blast Radius: "..PlayerStats:WaitForChild("blast radius").Value * PlayerStats:WaitForChild("multiplier").Value
end)
Character:WaitForChild("Humanoid"):GetPropertyChangedSignal("WalkSpeed"):Connect(function()
	MovementSpeed.Text = "Movement Speed: "..Character:WaitForChild("Humanoid").WalkSpeed
end)
Character:WaitForChild("Humanoid"):GetPropertyChangedSignal("MaxHealth"):Connect(function()
	MaxHealth.Text = "Max Health: "..Character:WaitForChild("Humanoid").MaxHealth
end)
PlayerStats:WaitForChild("player radius"):GetPropertyChangedSignal("Value"):Connect(function()
	PlayerRadius.Text = "Player Radius: "..PlayerStats:WaitForChild("player radius").Value * PlayerStats:WaitForChild("multiplier").Value
end)
PlayerStats:WaitForChild("multiplier"):GetPropertyChangedSignal("Value"):Connect(function()
	RocketDamage.Text = "Rocket Damage: "..PlayerStats:WaitForChild("rocket damage").Value * PlayerStats:WaitForChild("multiplier").Value
	RocketCooldown.Text = "Rocket Cooldown: "..PlayerStats:WaitForChild("rocket cooldown").Value * PlayerStats:WaitForChild("multiplier").Value
	RocketSpeed.Text = "Rocket Speed: "..PlayerStats:WaitForChild("rocket speed").Value * PlayerStats:WaitForChild("multiplier").Value
	BlastRadius.Text = "Blast Radius: "..PlayerStats:WaitForChild("blast radius").Value * PlayerStats:WaitForChild("multiplier").Value
	MovementSpeed.Text = "Movement Speed: "..Character:WaitForChild("Humanoid").WalkSpeed
	MaxHealth.Text = "Max Health: "..Character:WaitForChild("Humanoid").MaxHealth
	PlayerRadius.Text = "Player Radius: "..PlayerStats:WaitForChild("player radius").Value * PlayerStats:WaitForChild("multiplier").Value
end)

What are the multipliers? If it is the multipliers, you could use math.floor or math.ceiling to round off the numbers.

1 Like

So I have this local script for tracking the score left until my multiplier increases.

local Goal = 1
local Base = 1000
script.Parent.Text = (Base * Goal) - (game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("score").Value).." Score"
game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("score"):GetPropertyChangedSignal("Value"):Connect(function()
	if (Base * Goal) - (game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("score").Value) <= 0 then
		game.ReplicatedStorage.Events.Game.IncreaseMulti:FireServer(--[[Secret string password]]--)
		Goal += 1
		Base *= 1.125
		if (Base * Goal) - (game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("score").Value) <= 0 then
			Base = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("score").Value + 1000
			script.Parent.Text = (Base * Goal) - (game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("score").Value).." Score"
		else
			script.Parent.Text = (Base * Goal) - (game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("score").Value).." Score"
		end
	else
		script.Parent.Text = (Base * Goal) - (game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("score").Value).." Score"
	end
end)

I know it’s quite weird but it fixes a problem with negative score goals.
As you can see it fires an event secured with a password to a server script.

This is the server script:

game.ReplicatedStorage.Events.Game.IncreaseMulti.OnServerEvent:Connect(function(Player,password)
	if password ~= --[[Secret String Password-]] then return end
	Player:WaitForChild("player stats"):WaitForChild("multiplier").Value += 0.025
end)

As you can see it’s supposed to increase the multiplier by 0.025 every time this event is fired.
The multiplier of course starts at 1.

image

I have another result where the multiplier is fine but the rest is glitched.

The number values are not supposed to change by the way. The number values are the base values and the multiplied stats are shown in the frame.

Everything in the frame is calculated just for the frame…

i think this may be due to floating point imprecision, but I’m entirely not sure.

1 Like

do you know how I can fix this?

theres not really a fix, but you can keep the value to only a few decimal places by doing this:

local Places = 3

local function Floor(n,p) -- n is a number, p is the place
	return math.floor(n * 10^p) / 10^p
end

print(Floor(15.235536363424,Places))
1 Like

where do I use this function???

use the function on the already calculated values, set the place however you like, depends on your game if decimals are extremely important. this function can solve the imprecision, but it doesn’t fix it, it just hides the imprecision

The decimals happening here is because of floating point numbers. If you want to fix this, you can use math.floor(number) to remove the decimals.

local number = 3.555555
print(math.floor(number)) -- prints 3

Though, keep in mind math.floor(number) essentially removes the decimals only. If you want the number to round to the nearest number instead, use math.round(number):

local number = 3.555555
print(math.round(number)) -- prints 4

To approximate the decimals instead of completely removing them, you can use string.format to format the number, for example:

local number = 3.555555
local places = 3
print(string.format("%." .. places .. "f", number)) -- prints 3.556

Thank you, I’ll take a look later and tell you if it worked.

@PerceptualReality so I took a look at your function and it looked good at first until I realized it’s not the exact number. Thank you though!

That’s what I was looking for! Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.