Need help with Computercraft

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here
  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

Aperturez

New Member
Jul 29, 2019
3
0
0
Hi. I need help with some code for a non-advanced computercraft program. I'm building a scoreboard for a game, and I need it to add one to the score each time a redstone switch is hit (and the score is displayed on a monitor). I know how to get the input from a redstone switch and I know how to display it on the monitor, but I don't know how to make it add score each time and display it. I've read the wiki and it didn't really help. Can anyone here help?
 
Something like:
Code:
local score = 0
while true do
   if redstone.getInput("back") == true then
      score = score + 1
      monitor.setCursorPos(2,2)
      monitor.write("Score:"..score)
   end
end
 
Last edited:
  • Like
Reactions: gold49
Actually you probably want to do something like this instead:
Code:
local score = 0
while true do
local event = os.pullEvent("redstone")
   if redstone.getInput("back") == true then
      score = score + 1
      monitor.setCursorPos(2,2)
      monitor.write("Score:"..score)
   end
end
The os.pullEvent("redstone") will keep yielding until a redstone signal is changed. Else you might end up with problems getting it timed right with the if statement to trigger properly on a redstone pulse.
You can also just add elseif's to the if statements for different sides/redstone strengths/bundled signals for different players scores if you like.
 
Last edited:
Actually you probably want to do something like this instead:
Code:
local score = 0
while true do
local event = os.pullEvent("redstone")
   if redstone.getInput("back") == true do
      score = score + 1
      monitor.setCursorPos(2,2)
      monitor.write("Score:"..score)
   end
end
The os.pullEvent("redstone") will keep yielding until a redstone signal is changed. Else you might end up with problems getting it timed right with the if statement to trigger properly on a redstone pulse.
You can also just add elseif's to the if statements for different sides/redstone strengths/bundled signals for different players scores if you like.
Alright
 
Just realised when I had to do a bit of other programming that I derped up the if's. It is ofc: "if xxxxx then" not do :P
Edited other posts to fix the error.