Page 1 of 1

math.round() doesn't work

Posted: Mon May 10, 2021 12:56 pm
by mk8
So I have a function that look something like this:

Code: Select all

function checkPlayerCollisions()
	local x = player.x
	local y = player.y
	local rx = math.round(x)
	local ry = math.round(y)
	. . .
end
And the problem is, that the program can't call math.round() for some reason, it always says "attempt to call field 'round' (a nil value)", even if I remove the 'local' thing, just always, with any variable in this particular function (it doesn't throw any errors in the other functions).
can some1 halp pls

Re: math.round() doesn't work

Posted: Mon May 10, 2021 1:20 pm
by Jose_antonio_920
This code has one erro, not is math.round, os math.random, but hás love.math.random too, for use math.round, before add "math.round=math.random" or "math.round=love.math.random"

Re: math.round() doesn't work

Posted: Mon May 10, 2021 1:54 pm
by Eglaios
Still new there, but I'll give a try...
From what I understood, there isn't "math.round" in Lua ; instead, we have math.floor and math.ceil (pretty much self-explanatory).

Re: math.round() doesn't work

Posted: Mon May 10, 2021 2:09 pm
by ReFreezed
math.round doesn't exist in Lua by default. You'll have to define it yourself:

Code: Select all

math.round = function(n)
	return math.floor(n + 0.5)
end
See the Lua manual for all existing math functions.

Re: math.round() doesn't work

Posted: Tue May 11, 2021 3:44 pm
by mk8
thank you all so much it works now