CC: Tweaked

CC: Tweaked

42M Downloads

Integer value taken as string value

Midnight-Github opened this issue ยท 1 comments

commented

Minecraft Version

1.18.x

Version

1.100.9

Details

The problem is with getItemDetail() function.

when I do,
local turtleSlot = read()
print(turtle.getItemDetail(turtleSlot).name)

It gives an error message - Bad argument, received string instead of number.

but when I do,
print(turtle.getItemDetail(turtleSlot + 0).name)

It works properly.
Revising that function could help.

commented

read can only return strings and so if you press 1 read returns "1". You need to tonumber() your value before trying to use it as a number.

Adding 0 to "1" (or any other number in a string) implicitly does the conversion to a number as strings can not be used as numbers so lua tries to use the string "1" as a number and manages to successfully convert it to 1, it then performs the addition of 0.

local turtleSlot = tonumber(read()) will work, but be warned, if tonumber can't convert the string then it will return nil. You may need to handle this in your code (unless you really trust the user to input only valid values.