Using NumberRange in an if statement

What I need to do is change the text on a surfacegui according to a Number Range (such as 1 - 100) in an if statement. And yes I’ve already checked the TextLabel in the exxplorer to see if the text field changed.

What happens is that it doesn’t change the text at all. No errors are outputted or

Here’s the current code:

    local egrade = NumberRange.new(0, 25)
    local dgrade = NumberRange.new(25.1, 30.9)
    local cgrade = NumberRange.new(31, 49.9)
    local bgrade = NumberRange.new(50, 64.9)
    local agrade = NumberRange.new(65, 79.9)
    local sgrade = NumberRange.new(80, 89.9)
    local ssgrade = NumberRange.new(90, 99.9)
    local sssgrade = 100

while true do
if game.Workspace.PV.TP.Value == egrade then
script.Parent.Text = "E"
elseif game.Workspace.PV.TP.Value == dgrade then
script.Parent.Text = "D"
elseif game.Workspace.PV.TP.Value == cgrade then
script.Parent.Text = "C"
elseif game.Workspace.PV.TP.Value == bgrade then
script.Parent.Text = "B"
elseif game.Workspace.PV.TP.Value == agrade then
script.Parent.Text = "A"
elseif game.Workspace.PV.TP.Value == sgrade then
script.Parent.Text = "S"
elseif game.Workspace.PV.TP.Value == ssgrade then
script.Parent.Text = "SS"
elseif game.Workspace.PV.TP.Value >= sssgrade then
script.Parent.Text = "SSS"
end
wait(1)
end

Thanks for all help! :slight_smile:

3 Likes

You can check if a value falls within a NumberRange like so…

local egrade = NumberRange.new(0, 25)

while true do
	local Value = game.Workspace.PV.TP.Value  -- For convenience

	if Value >= egrade.Min and Value <= egrade.Max then

...
5 Likes

You can also put the grade and range in a table for simplicity, also if you’re not using integer values, values between .9 and the next number may not be included from your current list.

local SurfaceGrade = workspace.PV.TP
local LabelGrade = script.Parent

-- grading list with range
local GradeList = {
	SSS = NumberRange.new(100),
	SS = NumberRange.new(90,100),
	S = NumberRange.new(80,90),
	A = NumberRange.new(65,80),
	B = NumberRange.new(50,65),
	C = NumberRange.new(30,50),
	D = NumberRange.new(25,30),
	E = NumberRange.new(0,25),
}

local function onGradeAdjust()
	local adjGrade = SurfaceGrade.Value
	for grade, property in pairs(GradeList) do
		if adjGrade >= property.Min and adjGrade <= property.Max then -- found value in range
			LabelGrade.Text = grade 
			break
		end
	end
end

SurfaceGrade:GetPropertyChangedSignal("Value"):Connect(onGradeAdjust) -- run the grade function when surfacegrade value changes
8 Likes

Hey thanks for your help, I appreciate it!