How to make a percentage?

Busy loops like these are not good practice; it’s preferable OP updates based on some event, whether manually triggered or through a Roblox connection.

3 Likes

already made a loop using an event that is triggered when DataStore2 is updated, thanks for the tip

Just for fun, you can use string’s format function too:

local text = ("%i%% of obby completed!"):format(value / maxValue * 100)

The “%i” is replaced with an integer number, thus a non-integer will just get its decimal cut off (essentially flooring the value). The “%%” just escapes the “%” so it will appear as-is and not get formatted.

3 Likes

this will help me a lot! thanks.

1 Like

I don’t know why, but it seems that the script is not being read by the client, and it doesn’t show any errors in the script!

Screenshot_22

1 Like

In this script you are actually initializing “StageText” and “Completed” as string variables; you can’t directly reference properties of instances like what you seem to be trying to do.
Try initializing them as this:

local StageText = script.Parent.Stage
local Completed = script.Parent.Completed

Then update them like this:

StageText.Text = "Stage: "..StageValue
local Percentage = math.floor(StageValue / MaxValue * 100)
Completed.Text = Percentage.."% of obby completed"
12 Likes

is there any way I can get just two decimals? for example: 51.23667 to 51.23

You can round to the n’th digit by multiplying by 10^n, flooring, then dividing back:

Percentage = StageValue / MaxValue * 100
Percentage = math.floor(Percentage * 100) / 100
1 Like

if my Stage Value starts with 1 how do I get the percentage to be zero when I start? sorry the inconvenience is that I am very confused

1 Like

You would have to offset them by one then.

Percentage = (StageValue - 1) / (MaxValue - 1) * 100

When StageValue == 1, Percentage would be zero
When StageValue == MaxValue, Percentage would be 100

already tried this look:

Screenshot_23

Could you show the exact line of code where you perform this calculation in that it results in a negative value? This should not be the case when you’re offsetting the value.

Be sure to also include updated code lines when something doesn’t work so we can build off your progress and see what the issue may be, thus allowing us to better help you.

1 Like
Remote.OnClientEvent:Connect(function(StageValue)
StageText.Text = "Stage: "..StageValue

local Percentage = math.floor(StageValue-1/(MaxValue-1) * 100) -- here

Completed.Text = Percentage.."% ".."of obby completed"
end)

realized my mistake lol I put:
StageValue-1/(MaxValue-1) * 100
rather than:
(StageValue-1)/(MaxValue-1) * 100

Your order of operations is starting to look confusing. I like to organize these kinds of expressions with parentheses. It ensures that operations are done in the order they are supposed to and also makes it more readable.

What the VM sees is this:

MaxValue - 1
1 / (MaxValue - 1)
(1 / (MaxValue - 1)) * 100
StageValue - ((1 / (MaxValue - 1)) * 100)

Which looks nothing like what we’re trying to do.

local Percentage = math.floor((StageValue-1)/(MaxValue-1) * 100)
2 Likes

my script is working properly, thanks to everyone who helped me

1 Like

Yeah. Parenthesis matter very much, don’t forget them. I ran Blokav’s code through the Lua demo page and it worked as intended.

local a = 1
local b = 30

local c = math.floor((a-1)/(b-1) * 100)

print(c) -- 0

The only problem is that the number isn’t accurate because a = 15 results in 48 where it should be resulting in 50.

This could probably be solved by him either adding a final stage as a “Winner’s” section like in classic obbies, or restructuring the game such that the player’s Stage number represents the last stage they have completed, rather than the one they are currently on. That way players would start at zero and when completing the game, they would be on a non-counted “end-game” stage, while their Stage Value would remain at 30. Offsetting the values would not be needed with that system.

The optimal way of truncating a decimal and do what you want is via string.format.
The format %.2f should achieve your goal, since it specifies a float with 2 decimal numbers.

Completed.Text = string.format('%.2f%% of obby completed', Percentage)

Note that for the literal % to show, I escaped it with a double %%.

1 Like

it worked, thanks :smile:
30character