[SOLVED] Module returns nil when the return isn't nil

I’ve got a function on my module with simplifies numbers, i.e 12302 > 12.3K.

function module.simplify(n)
	local hundreds = floor(n/100)
	local kilos = floor(n/1000)
	local mils = floor(kilos/1000)
	local bils = floor(mils/1000)
	local regular = round(n*10)/10
	if kilos < 10 then
		return regular
	elseif kilos >= 10 and mils == 0 then
		return kilos..'.'..hundreds-(kilos*10)..'K'
	elseif mils > 0 then
		return mils..'.'..floor((kilos-mils*1000)/100)..'M'
	end
end

All of the sudden it keeps returning nil. The input value is 2132.423 so it should return regular, which is 2132.4. I know that regular isn’t nil as when I put prints in the function regular would be what it’s intended to be, 2132.4.

Yet for all scripts requiring the module, or even scripts who have their own simplify function with the same code it is returning nil.


All I have changed was some code on an entirely different script that doesn’t even require the module, and the other script isn’t at all memory expensive. I’ve noticed that the module stopped working and so I reverted the code from the other script, yet it still doesn’t work.

If anyone knows the cause of this, please inform me!

I want to assume that floor and round are both existing functions or written as math.floor and math.round.

I did run the code with the value of 2132.423 and it did return 2132.4. My assumption is that this doesn’t have to do with the module, but with something else in your script.

This is an example of the usage of the module [ess refferes to the module]


All the inputs are numbers and not nil

I ran the following script and got 2132.4:

function simplify(n)
	local hundreds = math.floor(n/100)
	local kilos = math.floor(n/1000)
	local mils = math.floor(kilos/1000)
	local bils = math.floor(mils/1000)
	local regular = math.round(n*10)/10
	if kilos < 10 then
		return regular
	elseif kilos >= 10 and mils == 0 then
		return kilos..'.'..hundreds-(kilos*10)..'K'
	elseif mils > 0 then
		return mils..'.'..math.floor((kilos-mils*1000)/100)..'M'
	end
end

print(simplify(2132.423))

What I would do in your situation would be to store the ess.simplify result in a different variable, and print it. Additionally, right before I call the ess.simplify function, I’d print the stats.balance.Value. If the balance value is truly 2132.423 and the output is truly nil, then you’d have to debug the function. Let me know what the results are.

stat.balance.Value refers to how much money the player has, for my case it is 2132.423.

The module used to work until it suddenly didn’t. The inputs work, the calculations work, the function seems to just not return when it should.

Putting the results in a different variable wouldn’t help much as it would still be nil. It seems to be a problem on the module’s side instead of the requiree’s side.

Just did some more debugging tests. The function does not return nil, it returns absolutely nothing.

Still haven’t found the issue, if anyone knows the root cause of this please inform me!

Add some print statements between the if-statements on the module function. It could be that you’ve done something that overwrites it or references another function instead. Just ensure that this is the actual function that works. The function itself, given the input, should give you the proper result, so something isn’t right, and I suspect that it doesn’t have to do with the function itself, but outside factors.

I have done that before making this thread, the if statements work perfectly, and the inputs to the returns aren’t nil. It may be a studio problem itself.

Well, my recommendation is that you debug outside factors and everything you can, really. Many times, we overlook stuff and think that “there is no need to check this” because of this and that, but it may be the root cause of the problem.

The main reason I like to separate it like that when debugging is because I can isolate factors. By isolating like that, I can see where exactly everything goes downhill and move on from there. Never underestimate a problem. Don’t assume that it won’t help because you might actually find out that the error you are getting is because of another unforeseen factor.

I highly doubt that unless you are running it in the command line. Studio works just like Roblox, so there’s no reason it would be a studio problem. It worked for me, so it is highly likely that something in your code is messing it up.

1 Like

strange, the system used to work well and I’ve changed no code on the module or the requiree. Errors started popping up with the requiree’s once I added some code to an un-related script that doesn’t even intervene with the requiree script nor the module. Even when reverting the updates of the un-related script, the issues were still happening

Like I said, there may be outside factors that affect it. You may have changed something that seemed unrelated but was related in some way (this could be execution order, or anything else). Regardless, I recommend that you find the exact issue if possible and fix it so you don’t leave your code’s success up to luck.

function simplify(n)
	local hundreds = math.floor(n/100)
	local kilos = math.floor(n/1000)
	local mils = math.floor(kilos/1000)
	local bils = math.floor(mils/1000)
	local regular = math.round(n*10)/10
	if kilos < 10 then
		return regular
	elseif kilos >= 10 and mils == 0 then
		return kilos..'.'..hundreds-(kilos*10)..'K'
	elseif mils > 0 then
		return mils..'.'..math.floor((kilos-mils*1000)/100)..'M'
	end
end

try this

That was my original script before i changed math.floor and math.round to floor and round. Both versions don’t work.

Strange because for me it did. Could the problem be from somewhere else?

Found out the problem.

The Balance Value had for whatever reason corrupted and became nil.

Previously Nil balances weren’t filtered by my datastore saving systems, now I’ve got that in place. The problem was not with the simplify function at all, but another function that used the balance (nil) and was causing the error.

Want to mark this as the solution?

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