Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to get a percentage of a number?

So here is what I have so far(local script inside of a text label):

local plr = game.Players.LocalPlayer
local passes = plr:WaitForChild("leaderstats"):WaitForChild("Passes")
local fails = plr:WaitForChild("leaderstats"):WaitForChild("Fails")
local total = passes.Value + fails.Value
local average = (passes.Value / total) * 100
script.Parent.Text = "Average Pass Level: "..tostring(average)
passes:GetPropertyChangedSignal("Value"):Connect(function()
	total = passes.Value + fails.Value
	average = math.floor((passes.Value / total) * 100)
	script.Parent.Text = "Average Pass Level: "..tostring(average)
end)
fails:GetPropertyChangedSignal("Value"):Connect(function()
	total = passes.Value + fails.Value
	average = math.floor((passes.Value / total) * 100)
	script.Parent.Text = "Average Pass Level: "..tostring(average)
end)

I tested it and the TextLabel's text was: Average Pass Level: -nan(ind).

There are no errors or warnings... could someone explain where I am going wrong?

1 comment

0

passes.Value or total are probally nil, try printing those values

2 answers

I couldnt say for sure whats wrong but here's a few ideas of how to debug/fix it....

nan(ind) means that whatever you passed it (ind) is undefined (the ind part) and "not a number" (the nan part)

Make sure that you're actually getting something passed from "passes" and "fails" (I' not sure waitForChild is required here, if you're going to leave it make sure you add a timeout arguement).

Make sure the things you're doing maths on are actually numbers (...or strings, tonumber(x) doesnt work particularly well for me but I havent looked into why lol) Try printing each part of "average" individually and seeing if it gives what you'd expect (i.e. passes.Value, fais.Value, total, average) if any of these return as 0 ake sure you're not trying to divide by 0 etc. which would result in an error...

Your equations are impossible to solve

A mathematical equation returns nan, short for not a number, when it is impossible to solve.

ind tells us what the "result" of the equation is, there is three variations: inf (infinity), -inf (negative infinity) and ind (indefinite).

A number of things can generate nan:

  • Most operations with a nan as at least one operand
  • Remainder x % y when x is an infinity or y is zero
  • The equations (+0) / (+0) (+?) / (+?) or (+0) × (+?)
  • The square root of a negative number
  • The logarithm of a negative number
  • The inverse sine or inverse cosine of a number that is less than -1 or greater than 1

You have gotten nan(ind) meaning the equation has no discernible value. This almost always comes from (+0) / (+0).

So basically the values you are providing are both zero for some reason.

Solutions

You could simply make sure that the values are never zero via whatever method you create them.

OR

You can check if one of the values are zero and not run the equation if so. Since if passes has a value greater then zero total will have the same we can just check passes alone.

if passes.Value ~= 0 then
	-- calculate here --
end

OR

You can check if your result is nan.

if tostring(passes.Value / total) == "nan(ind)" then
	average = #
else
	average = (passes.Value / total)
end

2 comments

0

The Passes and Fails are both, 0. I will try this.

0

I am also very young and barely understood the words you used to explain lol.

This discussion is preserved as it was. New answers, comments, and votes are closed.