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

4: attempt to perform arithmetic (sub) on nil. What? [solved and closed]

So I'm trying to make a lag detector but apparently this happens. I don't know why.

local one = print(elapsedTime())
wait()
local two = print(elapsedTime())
local final = two - one
print(final)

What the heck?

6 comments

0

My guess is the print part on one and two. Try doing it without the print?

0

Yeah, I just tested it, and it worked without the print.

0

Because print returns a string, and you can't subtract one string from another, they have to be values.

0

You can also use tick(), works just about the same. Returns the same number.

0

If you want a whole number, use math.floor(two-one)

0

thanks

1 answer

✓ Accepted answer

This doesn't work because the function, print, returns a string. You cannot subtract 2 strings, you can only subtract values.

So, for this you would do

local one = elapsedTime()
wait()
local two = elapsedTime()
local final = two - one
print(final)

This will return a Number Value.

If you want a whole number, you would use:

math.floor (Rounds Up)

math.ceil (Rounds Down)

local one = elapsedTime()
wait()
local two = elapsedTime()
local final = math.floor(two - one)
print(final)

local one = elapsedTime()
wait()
local two = elapsedTime()
local final = math.ceil(two - one)
print(final)

Hope this helped! If this worked, let me know by selecting this as the answer!

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