Computercraft bundled cable program not responding - lua

After several revisions, my Lua program still refuses to do anything.
--Let's Go!
--Program Infos
--Mappings
--Pink: Gate 1
--Red: East Tower 2
--Orange: West Tower 3
--Lime: Armoury 4
--Blue: Master Bedroom 5
--Grey: Guest Bedroom 6
--Cyan: Power Generation 7
--arbitrary variables
-- c is the variable for adding and subtracting. keeps track of what is CURRENTLY ACTIVE.
--Beginning values
--Start out with listening for arguments
local args = {...}
arg1=args[1]
arg2=args[2]
--Where are our outputs?
local towere = colors.red
local towerw = colors.orange
local gate = colors.pink
local armoury = colors.lime
local mstr = colors.blue
local guest = colors.grey
local power = colors.cyan
--bundled outputs
local output = "right"
-- ADD AND SUBTRACT MAKE LIFE EASIER CODE
--Courtesy of #Kingdaro on Computercraft Fourm (adapted for practical use in project)
--Original Link: http://www.computercraft.info/forums2/index.php?/topic/7641-redpower-bundled-cable/
--How to close a door
function close(door)
local input = rs.getBundledOutput("right")
rs.setBundledOutput("right", colors.combine(input, door))
end
function open(door)
local input = rs.getBundledOutput("right")
rs.setBundledOutput("right", colors.subtract(input, door))
end
--Make a good base for closing/opening things
--Basically, Functions Galore.
--GATE CONTROLS
function gateclose()
rs.setOutput(top, true)
wait(1)
rs.setOutput(top, false)
close(colors.pink)
end
function gateopen()
open(colors.pink)
end
--Beef of the program
--Start out with all doors open
if args[1] == gate and args[2] == open then --if the command is gate open
gateopen()
elseif args[1] == gate and args[2] == close then --if the command is gate close
gateclose()
end
I've tried and tried, but still, no response. I have the setup (physically, if you could call it that) correct (the bundled cable is on the right) and no error messages, but when run with the 'gate close' arguments, no response from the wires.
Any suggestions? Thanks!

Most probably you want to compare string:
if args[1] == "gate" and args[2] == "open" then --if the command is gate open
gateopen()
elseif args[1] == "gate" and args[2] == "close" then --if the command is gate close
gateclose()
end

Related

How do i make 2x cash gamepass script Roblox

So I was making a script that gives you 5 cash every minute, I also made a game pass for the script if someone owns the game pass they get double money as the Non-game pass holders. Here is my script
I Haven't any scripts to give cash but the problem is in the 2nd script block, the console print an error :
  09:10:57.466 ServerScriptService.CashGiver:6: attempt to index nil with 'UserId' - Server - CashGiver:6
local Give5Cash = game.ReplicatedStorage:WaitForChild("Give5Cash")
local Give10Cash = game.ReplicatedStorage:WaitForChild("Give10Cash")
Give5Cash.OnServerEvent:Connect(function()
print("Player Will Be Given 5 Cash")
end)
Give10Cash.OnServerEvent:Connect(function()
print("Player Will Be Given 10 Cash")
end)
while wait() do
local MPS = game:GetService("MarketplaceService")
local id = 16031172
local player = game.Players.LocalPlayer
if MPS:UserOwnsGamePassAsync(player.UserId, id) then
game.ReplicatedStorage:WaitForChild("Give10Cash"):FireServer()
print("Player Owns 2x Cash")
else
print("Players Doesnt Owns 2x Cash")
game.ReplicatedStorage:WaitForChild("Give5Cash"):FireServer()
end
wait(5)
end
local player = Players.LocalPlayer
if MPS:UserOwnsGamePassAsync(player.UserId, id) then
...
Here you assign a nil value to player which you may not index but do.
From the Roblox manual:
Players.LocalPlayer
NotReplicated
This item is not replicated across Roblox’s server/client boundary.
LocalPlayer is a read-only property which refers to the Player whose
client is running the game.
This property is only defined for LocalScripts (and ModuleScripts
required by them), as they run on the client. For the server (on which
Script objects run their code), this property is nil.
So I figured out a way to solve this issue and I have got the answer for my question.
Here is how I did it I made 3 scripts in ServerScriptService with the name of CashGiver5, CashGiver10, and CashGiverHandler
here are the scripts I added to each script.
CashGiver5:
while wait(1) do
print("Giving Player 5 Cash ")
for i, player in pairs(game.Players:GetPlayers()) do
player:WaitForChild("leaderstats").Cash.Value += 5
end
end
CashGiver10:
while wait(1) do
print("Giving Player 10 Cash ")
for i, player in pairs(game.Players:GetPlayers()) do
player:WaitForChild("leaderstats").Cash.Value += 10
end
end
CashGiverHandler:
local MarketPlace = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(player)
local g = 16031172 -- DOUBLE CASH ID
local Give5Script = game.ServerScriptService.CashGiver5
local Give10Script = game.ServerScriptService.CashGiver10
if MarketPlace:UserOwnsGamePassAsync(player.UserId, g) then
Give5Script:Destroy()
else
Give10Script:Destroy()
end
end)
WHAT THE SCRIPT DOES?
So basically the CashGiver scripts are basic scripts giving player Cash every second.
so the Handler script Destroys one of the scripts (s) when a player is added to the game.

how to restart a lua program using the script in the lua script

It's me again
I'm trying to make a Terminal program in Lua since it's my best language I know, I'm making the calculator program in it and I'm trying to make it so if the user types "exit" the program will restart and will go back to the terminal, but I don't know how to reset the program through the code. if anyone can help that be deeply appreciated.
This is the code:
io.write("Terminal is starting up --- done!")
io.write("Making sure everything works --- Done!")
cmd = io.read()
io.write(">")
if cmd == "" then
io.write(">\n")
end
if cmd == "cal"then
io.write("Calculator Terminal Program v1.0")
io.write("what operation?/n")
op = io.read()
if op == "exit"then
io.write("Exiting")
end
end
To answer your question directly, I don't think it's possible to "restart the program". However, by taking advantage of loops, you can achieve the same result.
For instance, this code probably does what you want:
print('Terminal is starting up --- done!')
print('Making sure everything works --- Done!')
repeat
io.write('>')
cmd = io.read()
if cmd == 'cal' then
print('Calculator Terminal Program v1.0')
repeat
io.write('Operation: ')
op = io.read()
until op == 'exit'
print('Exiting')
elseif cmd == 'command' then
--another command
else
print('Unknown command.')
end
until cmd == 'exit'
Other tips:
You should take advantage of elseif instead of writing multiple separate if statements to improve readability.
Consider using the print function when you want a new line after writing some text for a better Terminal experience. You could also use io.write('\n').
You probably want os.exit(), which terminates the entire program.
i think this might work through creative use of load() and coroutines
this gonna stop restarting itself when 3 total error has occured
if innerProgram == nil then --innerProgram will set to true when it load itself
local filename = nil
local errorLimit = 3 --Change this to any value to enable this code to restart itself when error occur until this amount of time set zero or below to exit instantly when error occur
local errors = 0
local filename = function()
local str = debug.getinfo(2, "S").source:sub(2)
return str:match("^.*/(.*)") or str
end
filename = filename()
local src_h = io.open(filename, "r") --open in read mode
local src = src_h:read("*a")
src_h:close()
local G = _G
local quit = false --set true when you want to exit instead restart
local code = nil
local request = false
local restart = false --set true when you want restart
local program
local yield = coroutine.yield --Incase when coroutine get removed in your calculator code for no reason
local running = coroutine.running
local exit = os.exit
function G.restart()
restart = true --Always refer to restart variable above
request = true
yield() --Always refer to yield above
end
function G.os.exit(exitcode) --Replace os.exit with this
quit = true --Always refer to quit variable above
reuqest = true
code = exitcode or nil
yield() --Always refer to yield above
end
function G.coroutine.yield()
if running() == program and request == false then --Emulating coroutine.yield when it not run inside coroutine
error("attempt to yield from outside a coroutine")
end
end
G.innerProgram = true --So the inner program not keep loading itself forever
function copy(obj, seen)
if type(obj) ~= 'table' then return obj end --got from https://stackoverflow.com/questions/640642/how-do-you-copy-a-lua-table-by-value for us to clone _G variable without reference to original _G thus we can do total restart without using same _G
if seen and seen[obj] then return seen[obj] end
local s = seen or {}
local res = setmetatable({}, getmetatable(obj))
s[obj] = res
for k, v in pairs(obj) do res[copy(k, s)] = copy(v, s) end
return res
end
print("Loading "..filename)
program = coroutine.create(load(src, filename, "bt", copy(G)))
while errors < errorLimit do
restart = false
local status, err = coroutine.resume(program)
if restart == true then
print("Restarting...")
program = coroutine.create(load(src, filename, "bt", copy(G)))
--Put errors = errors + 1 if you want errors counter to reset every time the program request restart
end
if status == false and restart ~= true then
print(filename.." errored with "..err.."\nRestarting...")
program = coroutine.create(load(src, filename, "bt", copy(G)))
errors = errors + 1
elseif restart ~= true then
print(filename.." done executing.")
exit()
end
end
return
else
innerProgram = nil --Nil-ing the variable
end
Features
Auto exit when 3 total errors has occur (configure errorLimit variable)
_G is not shared (same _G as start of the program but not linked with the actual _G)
Emulating yielding outside of coroutine
Replaced os.exit so it yield then the self-loader run the os.exit
How to use
put the code i give above to very first line of your code
Feature Number 1 and 3 test
it error with the a content the value will be different in each error restart
if a == nil then --Only set a when a equal nil so if _G was shared the error value will be same
a = math.random() --Set global a to a random value
end
error(a) --Error with number a
os.exit()

How to fix an end expected error near eof

I made a cash for kill script in Roblox, but wanted to go further than that and implement a script where when a player has a gamepass, then that player would recieve more cash than another normal player would.
This is for a battle royale styled game in Roblox, and when I playtested it, there were no errors, but the script didn't work.
game.Players.PlayerAdded:connect(function(player)
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local currency1 = Instance.new("IntValue",folder)
currency1.Name = "Cash"
local increment = 50
if game:GetService("MarketplaceService"):PlayerOwnsAsset(player,7382818)then
increment = increment + 50
end
player:WaitForChild("Humanoid").Died:connect(function()
local tag = player.Humanoid:FindFirstChild("creator")
if tag ~= nil then
local killer = tag.Value
if killer ~= nil then
-- Find the killer's leaderstats folder
local killerStats = killer:FindFirstChild("leaderstats")
if killerStats ~= nil then
-- Find the killer's Cash IntValue
local killerCash = killerStats:FindFirstChild("Cash")
-- Increase cash as before
killerCash.Value = killerCash.Value + increment
end
end
end
end)
I expected a VIP player, who has the gamepass, to receive more cash, but when I tested it, no player received any cash for killing another player at all.
How to fix an end expected error near eof
If the Lua interpreter complains about a missing end you`re missing it somewhere.
Read through your code and make sure everything that is supposed to be closed with an end has one. Read through the Lua Reference Manual to find out which keywords need an end.
In your code it's if statements and function definitions. Checking each pair from inside out you'll end up one end) short to close this game.Players.PlayerAdded:connect(function(player) as mentioned in the comments.

ESP8266 Tcp-to-uart with reprogramming ability

I'm using this example from nodemcu repo:
uart.setup(0,9600,8,0,1,0)
sv=net.createServer(net.TCP, 60)
global_c = nil
sv:listen(9999, function(c)
if global_c~=nil then
global_c:close()
end
global_c=c
c:on("receive",function(sck,pl) uart.write(0,pl) end)
end)
uart.on("data",4, function(data)
if global_c~=nil then
global_c:send(data)
end
end, 0)
But, since I'm using uart module, I'm no longer able to communicate with my chip via LuaLoader and can not upload updated init.lua files. Instead, I have to put chip into flash-upload mode, then flash initial nodemcu firmware and then my updated init.lua. Too much steps.
How can I retain ability to communicate via LuaLoader? I've tried something like this:
uart.on('data', '\n', handleUartResponse, 0)
...
...
function handleUartResponse(response)
if response == 'flash\n' then
g_flash = true
toggleOutput(true)
uart.write(0, 'flash mode')
elseif response == 'endflash\n' then
g_flash = false
uart.write(0, 'normal mode')
toggleOutput(false)
elseif g_flash then
node.input(response)
else
if g_conn ~= nil then
g_conn:send(response, function(sock)
closeConnection(sock)
g_conn = nil
end)
end
end
end
function toggleOutput(turnOn)
if turnOn then
node.output(nil, 1)
else
node.output(silent, 0)
end
end
It prints flash mode and normal mode in another serial terminal, but it doesn't work in LuaLoader. I think the problem is in uart setup, maybe it should not be \n, but other condition, I don't know what.
Got it! Not sure if it is the best option, since I'm new to lua, but it works.
function handleNormalMode(response)
if response == 'flash\r' then -- magic code to enter interpreter mode
toggleFlash(true)
else -- tcp-to-uart
if g_conn ~= nil then
g_conn:send(response, function(sock)
closeConnection(sock)
g_conn = nil
end)
end
end
end
function ignore(x)
end
function uartSetup(echo)
uart.setup(0, 115200, 8, 0, 1, echo)
end
function toggleFlash(turnOn)
if turnOn then
uart.on('data') -- unregister old callback
uartSetup(1) -- re-configure uart
uart.on('data', 0, ignore, 1) -- this allows lua interpreter to work
node.output(nil) -- turn on lua output to uart
uart.write(0, 'flash mode') -- notify user
else
node.output(ignore, 0) -- turn off lua output to uart
uart.on('data') -- unregister old callback
uartSetup(0) -- re-configure uart
uart.on('data', '\r', handleNormalMode, 0) -- turn on tcp-to-uart
uart.write(0, 'normal mode') -- notify user
end
end
I'm calling toggleFlash(false) at the beginning of script. Then, typing flash\r enters lua interpreter mode and to switch back I'm just typing toggleFlash(false) and it works! That simplifies updating init.lua every time.

Remote event problems

I'm making a game using ROBLOX about the tank battle system of Dragon Quest Heroes Rocket Slime (although not using 100% GUI's)
Since I didn't know how to make ammo become locked onto a path in normal ROBLOX, I decided to use a gui to show the ammo "firing"
To do this, I have a remote event that fires a function inside the main script of the GUI system when ammo is loaded
cannon.Touched:connect(function(v)
if fireable[v.Name] and v.Parent == workspace then
event:FireAllClients("Left",v.Name)
v:Destroy()
end
end)
Then, the GUI gets the correct sprite for the ammo loaded and fires it out of the right tank (the first argument in the FireAllClients part)
This is one of the two if statements for firing ammo (the other one is literally the same except that it's for the right side tank)
local tank = tankFiring == "Left" and tank1 or tankFiring == "Right" and tank2
if tank == tank1 then
print("yo!")
script.Fire:Play()
local ammoFrame = sp.Ammo:Clone()
ammoFrame.Parent = tank
ammoFrame.Visible = true
ammoFrame.Position = UDim2.new(0,120,0,68)
playAnimation("Cannon Fire",UDim2.new(0,120,0,68-25),tank.Frame)
ammoFrame.Image = ammoTypes[type]["img"]
ammoFrame.Size = ammoTypes[type]["Size"]
repeat
wait(.1)
ammoFrame.Rotation = ammoTypes[type]["Rotatable"] == true and ammoFrame.Rotation + 15 or 0
ammoFrame.Position = ammoFrame.Position + UDim2.new(0,1,0,0)
until
tank2:FindFirstChild("Ammo") and isTouching(ammoFrame,tank2:GetChildren()[3]) or isTouching(ammoFrame,tank2) or ammoFrame == nil
if tank2:FindFirstChild("Ammo") and isTouching(ammoFrame,tank2:GetChildren()[3]) then
script.Collision:Play()
local lastAmmoPos = ammoFrame.Position
playAnimation("Explosion",lastAmmoPos-UDim2.new(0,15,0,25),tank.Frame)
ammoFrame:Destroy()
tank2:GetChildren()[3]:Destroy()
end
if isTouching(ammoFrame,tank2) then
script.Collision:Play()
ammoFrame:Destroy()
workspace["Tank2"].Health.Value = workspace["Tank2"].Health.Value - ammoTypes[type]["dmg"]
end
end
The problem with this, is that if a player joins AFTER the ammo has been shot, they will not see the ammo on the GUI
Is there anyway to fix this? I can't just FireAllClients again, since that'll just fire another piece of ammo for all the players.
Have a service that keeps track of what ammo is in what state. Then all clients first asks the servers of current ammo states when joining, and then subscribes to further changes.

Resources