This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TaZer Software's profile picture

Trying to make a cookie clicker but getting errors

Started by TaZer Software, 11 March 2018 - 06:02 PM
TaZer Software #1
Posted 11 March 2018 - 07:02 PM
Hello, I been working on this game "Cookie Clicker" but I cant seem to get it to work, its something to do with this part I think


function drawShop()
paintutils.drawFilledBox(30, 3, 50, 6, colors.orange)
scp(32,4)
stc(1)
w([[ Clicker ]])
scp(32,5)
if Cookies >= ClickerCost then -- This part
stc( colors.green )
term.write(ClickerCost.." Cookies")
else
stc( colors.red )
scp(32,5)
term.write(ClickerCost.." Cookies")-
end
end

I'm trying to make it so if cookies are above the ClickerCost it will show green, it dose that at start like this,

https://i.imgur.com/rvpWeZE.png

as you can see its red, but when I click on the cookie I get this error,

main.lua:77: attempt to compare number with string expected, got number

Here are the two files….
https://pastebin.com/zRqXqmVu – The assests loader if your only looking at the code you dont need this
https://pastebin.com/JxZFb1LJ – The main file where the errors happen


if you want to start this up on your own computer, startup the CookieClicker.exe then when its done finishing assests terminate it because the error box I made covers the red error text and also the assests need to load in first


ik this sounds complicated but I need this fixing, any questions about the code please ask
Bomb Bloke #2
Posted 12 March 2018 - 01:42 AM
In your newGame() function, you're doing:

function newGame()
Cookies = "0"
cps = "1"
ClickerCost = "10"
end

By wrapping those numbers in quotes, you're defining them as strings, that is to say, text values. Lua will allow you to treat string-representations as numbers sometimes, but not in all cases.

For example, on line 101, you're doing:

Cookies = Cookies + cps

Because you're using the + operator, Lua assumes you want to treat the values inside the Cookies and cps strings as numbers. It converts them, adds them together, and then assigns the new number value to Cookies.

Later after this, line 77 executes:

if Cookies >= ClickerCost then

At this point, Cookies is a number, and ClickerCost is a string. Lua won't allow you to compare these: they both need to be of the same type.

Fix is to ditch any quotes around any values you want treated as numbers.

http://lua-users.org/wiki/LuaTypesTutorial