Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
Hakz_Studios

I need help with my If-Then commands

Recommended Posts

I need help with my autorun.lua program It won’t take my If-then statements what am I doing wrong?

(I am using Computronics)

Here is the program

— local require commands start

local component= require (“component”)

local sides = require(“sides”)

local rs = component.redstone

local event = require(“event”)

local os = require(“os”)

— local  require commands end

— Main function start

local function main()

local username.message = event.pull(“chat_message”)

—problematic loop start

if (username = “Hakz_Studios” and message = “ Alice Refuel”) then

component.speech_box.say(“Refueling”)

component.chat_box.say(“Refueling”)

repeat

local username,message = event.pull(“chat_message”)

rs.setoutput(sides.bottom)

untill username= “Hakz_Studios” and message = “Stop Refueling”

— restarting function

function main()

elseif username = “Hakz_Studios” and message = “Stop Refuel Systems”

os.execute(“Shutdown”) 

else function main()

—problematic loop end 

end

— main function end

Sorry If I had many mistakes I just started Coding Today and Thanks in advance

Link to post
Share on other sites

Hey,

in these if statements, you need to use relational operators. Since you want to see if you're variable is equal to a certain string, you need the '==' (two equal signs).

So you're code would look like this

-- local require commands start

local component= require (“component”)

local sides = require(“sides”)

local rs = component.redstone

local event = require(“event”)

-- local os = require(“os”)	remove this, the os lib does not need to be required, I think

-- local  require commands end

-- Main function start

local function main()

  local username, message = event.pull(“chat_message”)	-- you head a period (.) between both variables

  -- problematic loop start

  if username == Hakz_Studios and message ==  Alice Refuel then

  component.speech_box.say(“Refueling”)

  component.chat_box.say(“Refueling”)

  repeat

    local username,message = event.pull(“chat_message”)

    rs.setoutput(sides.bottom, 15)	-- you need to give the redstone output a value, otherwise it wont turn on
    -- also, by putting it into a loop, the redstone output will be set a ton of times
    -- it helps putting the statement before the repeat until loop

  until username == Hakz_Studios and message == Stop Refueling

  -- restarting function

function main() -- idk what is this line about, but i dont think it belongs here

  elseif username == Hakz_Studios and message == Stop Refuel Systems

  os.execute(“Shutdown”) -- it will look for a program called 'Shutdown' with a capital S

  else
    function main() -- also, dont know what you want to do here, do you want to start the function again (to have the code repeated endlessly)
  --problematic loop end 
  end

-- main function end
end

Some small info: you don't have to put brackets around your condition in the if-statements, you can, but it's not necessary.

Also, here is a program that repeats the main loop over and over again, as I think that is what you want to do in your original code, if I'm wrong tho and misinterpreting you're code, just ignore my suggestion:D

-- require libs

local component = require("component")
local rs = component.redstone
local sides = require("sides")
local event = require("event")

-- main function

local function main()
  local username, message = event.pull("chat_message")
  if username == "Hakz_Studios" and message == "Alice Refuel" then
    component.speech_box.say("Refueling")
    component.chat_box.say("Refueling")
    -- this time I set the redstone output once, and then wait for the message 'Stop refueling' then deactivate the redstoneoutput again
    rs.setOutput(sides.bottom, 15)
    repeat
      username, message = event.pull("chat_message")
      -- you dont have to use 'local' in front of username and message here, since you already created these variables at the top of the main function
      -- it will override these values but it actually doesnt matter since from this part you will skip to the end and these initial values for both variables
      -- wont be needed anymore
    until username == "Hakz_Studios" and message == "Stop Refueling"
    rs.setOutput(sides.bottom, 0)	-- I assume you want to deactivate the redstone after you're done refueling
    
  elseif username == "Hakz_Studios" and message == "Stop Refuel System" then
    os.execute("Shutdown")	-- again, this will start the program "Shutdown" with a capital "S"
  end
  	-- as you can see, we skip the last else statement, as it will call the main function recursively, resulting in a overflow error pretty fast
  	-- you could get away with returning a call to the main function, but this wouldnt be good practice eiter, I guess
end

-- we add a last while loop, which will repeat endlessly calling the main function

while true do
  main()
end

I hope this helps.

Also you did great for your first shot at programming!

Link to post
Share on other sites

I have a question why type 3 ends? I am thinking one ends the loop,another ends the main function and the last ends the program. Am I right? And it gave me this error:/lib /process.lua:52: /home/autorun.lua:18: syntax error near ‘username’:

stack traceback:

|C|: in function ‘assert’

/lib/process.lua:52: in function </lib/process.lua:35> 

(...tail calls...)

[C]: in function ‘xpcall’

machine:791: in function ‘xpcall’

/lib/process.lua:63: in function </lib/process.lua:59>

Help!

Link to post
Share on other sites
16 hours ago, Hakz_Studios said:

I have a question why type 3 ends? I am thinking one ends the loop,another ends the main function and the last ends the program. Am I right?

In what piece of code you have 3 ends? I only find 3 ends in the last program I posted. There you have one end for closing the main function (2nd end), one for closing the if-statement (1st end) and the last end is for closing the while loop.

And for the error:

First it would be good if you could post the actual screenshot.

Second,  could you post the code of the program you were running?

From what I currently see, there‘s propably a typo in line 18, but I can‘t tell exactly, what is wrong until I see the actual code.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.