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

Error : attempt to concatenate string and boolean

Started by raflnatorr, 26 August 2016 - 12:20 AM
raflnatorr #1
Posted 26 August 2016 - 02:20 AM
I'm writing my first program, which is for Big Reactors. And when i want to write out some of the data it won't work.
I can easily get it to write out if the reactor is on, if i don't put it in my "draw_text" function, but when i do i get the error (lasse:25: attempt to concatenate string and boolean) Everything else in the code works though.

Here is the code.

while true do
local reactor = peripheral.wrap("BigReactors-Reactor_6")
local mon = peripheral.wrap("monitor_8")
mon.clear()
mon.setBackgroundColor(colors.black)
mon.setTextScale(1.75)

------ Functions ------

-- Text --
function draw_text(x, y, text, text_color)
		mon.setTextColor(text_color)
		mon.setCursorPos(x,y)
		mon.write(text)
end

------ Screen ------

-- Name of program --
draw_text(6, 1, "Reactor Controle", colors.orange)

-- Active --
local ReactorActive = reactor.getActive()
draw_text(1, 2, "Active: ".. ReactorActive, colors.lime) -- It is here the problem is i think, as it is line 25
if ReactorActive == 0 then do
draw_text(1, 2, "Active: ".. ReactorActive, colors.red)
end

-- Fuel amount --
local FuelAmount = math.floor(reactor.getFuelAmount() /1000)
local MaxFuelAmount = math.floor(reactor.getFuelAmountMax() /1000)
local FuelTogether = FuelAmount .." / ".. MaxFuelAmount

draw_text(1, 3, "Fuel: ".. FuelTogether, colors.lime)
if FuelAmount / MaxFuelAmount <= 0.1 then do
draw_text(1, 3, "Fuel: ".. FuelTogether, colors.red)
end

end
sleep(5)
end
end
Edited on 26 August 2016 - 12:24 AM
KingofGamesYami #2
Posted 26 August 2016 - 02:30 AM
ReactorActive is a boolean. Make it a string before concatenating.

draw_text(1, 2, "Active: ".. tostring( ReactorActive ), colors.lime) 
Also, since boolean values can never equal 0 (they are either true or false), you should change the if statement.
Edited on 26 August 2016 - 12:30 AM
raflnatorr #3
Posted 26 August 2016 - 02:55 AM
ReactorActive is a boolean. Make it a string before concatenating.

draw_text(1, 2, "Active: ".. tostring( ReactorActive ), colors.lime) 
Also, since boolean values can never equal 0 (they are either true or false), you should change the if statement.

Thanks a lot! That helped with the problem, but it also spawned another one!
For some reason i can't quite get, my if statement doesn't end with the "end".
If the if statement gets toggled it also activates the next part of the code, the first part of the fuel amount.

I'm talking about this part:

-- Active --
local ReactorActive = reactor.getActive()
draw_text(1, 2, "Active: ".. tostring(ReactorActive), colors.lime)
if ReactorActive == 0 then do
draw_text(1, 2, "Active: ".. tostring(ReactorActive), colors.red)
end
-- Fuel amount --
local FuelAmount = math.floor(reactor.getFuelAmount() /1000)
local MaxFuelAmount = math.floor(reactor.getFuelAmountMax() /1000)
local FuelTogether = FuelAmount .." / ".. MaxFuelAmount
draw_text(1, 3, "Fuel: ".. FuelTogether, colors.lime) -- It toggles this part for some reason. It only shows when the reactor.getActive()=false.
if FuelAmount / MaxFuelAmount <= 0.1 then do
draw_text(1, 3, "Fuel: ".. FuelTogether, colors.red)

Am i missing an end somewhere or what?
Emma #4
Posted 26 August 2016 - 03:19 AM
–snip–

When writing if statements, you only need a then token, you included a do after the then, which technically isn't invalid syntax, and you can create new scopes using that, but it isn't what you want to do. Just remove the do after your then tokens.
Edited on 26 August 2016 - 01:20 AM