Error message displaying when attempting to use full numbers: [attempt to get length of a nil value]

The get decimals detects how many decimals the value has, I use this to display a number GUI. However if the number is a full number, ex: (1, 3, 7) Then it causes an error, idk how to fix it.

2dd33cea2e913fcbf2a54e8758266354

function GetDecimals(Num)
	return #(tostring(Num):split(".")[2])
end

while true do task.wait(waittime.Value)
	coin += 1
	local value = .1 * Multiplier.GainMultiplier.Value
	print(value)
	Values.Movement.JumpPower.Value += value
	RS.Events.Bindable.GainIndicator:Fire(value, (GetDecimals(value)),"JP")
2 Likes

You are using string.split [2], but if the number is full (not decimal), then it returns nil because theres nothing to return, and I think you are getting a length of the text the function returns in another script with remoteEvent. so I think you should modify your remote event script, or do this

function GetDecimals(Num)
        local val = (tostring(Num):split(".")[2])
        if val then
            return val
        else
            return 0
        end
end

this changed function will return the decimal length like before if the number is decimal, but it will return 0 when the number is full, and the split value returns nil.

2 Likes

Instead of assuming that there is a decimal, you can add a check first:

function GetDecimals(Num)
	if Num % 1 == 0 then
		return 0 
	else
		return #(tostring(Num):split(".")[2])
	end
end

if you want it as a one line solution, you can also use this:

function GetDecimals(Num)
	return Num % 1 == 0 and 0 or (tostring(Num):split(".")[2])
end

(Num % 1 basically just gets rid the whole number part, so 3.14 becomes 0.14. Checking if it equals 0 is basically just returns true if it was an integer, and false if it has decimals)

2 Likes

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