Hello stack overflow users, I have some code here:
local input = nil
print("What file do you want to access?")
input = io.read();
local file = io.open(input, "r")
function infiniteLoop()
print("What do you want to know more about?")
input = io.read();
while true do
line = file:read()
if line == nil then break end
if string.find(line, input) then
print(line)
end
end
end
repeat
infiniteLoop()
until false
As you could guess from the title, it works the first run, but the second time it won't print out what you want, regardless of it being in the file.
Example here
You need reset file pointer. try add file:seek(0) in the begining of function
Related
My goal here is to send the player object and the object name through to the client script to display the object name on a text label.
When I run this, it prints nil and nil. I want player.Name and name as you will see in the second code sample. Another error I get is "attempt to concatenate nil with string" from the client script.
Here is my server-side code:
script.onTouch.OnInvoke = function(button, sellObj, sellObjValue, buyObj, buyObjValue, afterWave, isLoadedPart)
if isLoadedPart == true then
local info = script.Parent.Parent.info
local player = info.player.Value
local owner = info.owner.Value
local savedItems = info.savedItems.Value
local builds = script.Parent.Parent.activeBuilds
if afterWave > info.activeWave.Value then
info.activeWave.Value = afterWave
end
button.Parent = savedItems.buttons
button.jobDone.Value = true
if sellObj ~= nil then
sellObj.Parent = savedItems.builds
end
if buyObj ~= nil then
buyObj.Parent = builds
end
local td = require(script.Parent.tycoonDictionary)
if not table.find(td.boughtButtons, button.objectId.Value) then
table.insert(td.boughtButtons, button.objectId.Value)
end
local ui = game.ReplicatedStorage:FindFirstChild('onPartLoad')
if ui then
ui:FireClient(player, 'buyObj.Name')
print('yes')
else
print('no')
end
else
local info = script.Parent.Parent.info
local player = info.player.Value
local owner = info.owner.Value
local money = info.player.Value.leaderstats.Money
local savedItems = info.savedItems.Value
local builds = script.Parent.Parent.activeBuilds
if money.Value >= buyObjValue or money.Value == buyObjValue then
if afterWave > info.activeWave.Value then
info.activeWave.Value = afterWave
end
button.Parent = savedItems.buttons
button.jobDone.Value = true
if sellObj ~= nil then
sellObj.Parent = savedItems.builds
money.Value += sellObjValue
end
if buyObj ~= nil then
buyObj.Parent = builds
money.Value -= buyObjValue
end
local td = require(script.Parent.tycoonDictionary)
if not table.find(td.boughtButtons, button.objectId.Value) then
table.insert(td.boughtButtons, button.objectId.Value)
warn(td.boughtButtons)
end
else
player.PlayerGui.inGame.error.label.invokeScript.errorInvoke:Invoke("Insufficient Funds")
end
end
script.Parent.waveChecker.afterRun:Invoke()
end
And here is my client-side code:
game.ReplicatedStorage.onPartLoad.OnClientEvent:Connect(function(player, name)
print(player.Name, name)
print(script.Parent.Text)
script.Parent.Text = name .. 'is loaded.'
print(script.Parent.Text)
end)
Here I will tell you a little about this game. It is a tycoon that saves data using a table with all button Ids in it. When it loads, it gets the button associated with the id and fires the server code for every button. If the button is a load button, it fires the client with the player and the buyObj.Name.
Is there just a little mistake or can I not send arguments to the client at all? Any help will be appreciated!
The OnInvoke callback's first argument is always the player that fired it, so you should change line 1 of the first script to:
script.onTouch.OnInvoke = function(playerFired, button, sellObj, sellObjValue, buyObj, buyObjValue, afterWave, isLoadedPart)
And :FireClient() requires a player argument as its first argument, so you should change
ui:FireClient(player, 'buyObj.Name')
to
ui:FireClient(playerFired, player, 'buyObj.Name')
Here is the solution I came up with;
First, I read through some Roblox documentation to find that the first argument I had to send when using :FireClient was the player, and because I already had the player there, it was just sending the function to that player. Now, I had 2 choices, I could send the player twice, or delete the player variable from the script. I chose the second one.
Here is what the :FireClient line in the server script looks like now:
game.ReplicatedStorage:WaitForChild('onPartLoad'):FireClient(player, buyObj.Name)
And here is what the client function script looks like now:
game.ReplicatedStorage.onPartLoad.OnClientEvent:Connect(function(name)
if name ~= 'starterFoundation' then
script.Parent.Text = name .. ' is loaded.'
end
end)
Thank you #TypeChecked for helping me realize this!
local level = 3 -- Required access level
local sideIn = "bottom" -- Keycard Input Side
local sideOut = "right" -- Redstone output side
local rsTime = 3 -- Redstone time
while true do
if disk.isPresent(sideIn) then
term.clear()
term.setCursorPos(1,1)
local code = fs.open("disk/passcode.lua", "r").readAll()
if code == nil then
local code = 0
else
local code = tonumber(code)
end
if code >= level then
print("> Access Granted")
disk.eject(sideIn)
rs.setOutput(sideOut,true)
sleep(rsTime)
rs.setOutput(sideOut,false)
else
print("> Permission Denied")
disk.eject(sideIn)
end
end
end
When there's no disk inserted, it throws an error:
.temp:15: attempt to compare string with number expected, got string
Does anyone know how to fix this issue? I throwed in a nil checker but it seems to not work. Any ideas on how could I fix this? I've been trying for at least half an hour now, and I still have no clue.
In this section:
local code = fs.open("disk/passcode.lua", "r").readAll() --(1)
if code == nil then
local code = 0 --(2)
else
local code = tonumber(code) --(3)
end
It first makes a new local variable with local code = .... In the new block that you create with the if, you also create new local variables with local code = .... Since it has the same name as the local before it, it "masks" it, prohibiting you from accessing the first code in the rest of the block. The value that you assign 0 to is not the same variable outside the if, so the first code is unaffected. At else the block for the second code ends and the same thing happens between else and end when the condition is false. To not assign the values 0 or tonumber(code) to new variables, you have to remove the local from local code = .... So the following is what it should be:
local level = 3 -- Required access level
local sideIn = "bottom" -- Keycard Input Side
local sideOut = "right" -- Redstone output side
local rsTime = 3 -- Redstone time
while true do
if disk.isPresent(sideIn) then
term.clear()
term.setCursorPos(1,1)
local code = fs.open("disk/passcode.lua", "r").readAll()
if code == nil then
code = 0
else
code = tonumber(code)
end
if code >= level then
print("> Access Granted")
disk.eject(sideIn)
rs.setOutput(sideOut,true)
sleep(rsTime)
rs.setOutput(sideOut,false)
else
print("> Permission Denied")
disk.eject(sideIn)
end
end
end
local move = game.StarterGui.BottmRight.Hotbar
local moves = false
script.Parent.MouseButton1Click:connect(function()
if moves == false then
move:TweenPosition{UDim2.new{0.724,0,0.919,0},"In", "Bounce", 2}
moves = true
wait(2)
move.Visible = true
else
move:TweenPosition{UDim2.new{0.912,0,0.919,0},"Out", "Quint", 2}
moves = false
move.Visible = false
the error is:
Error:(28,1) Expected'end'(to close 'else' at line 11),got <eof>
Any idea?
You need an "end" to close the else block of your conditional. Try putting "end)" as the last line... as you also need to close out of the :connect method
You need two end statements (to close if ... else and to close function()) and also closing ) for the connect method. As the result, you need to add end end) to fix the error.
It is my first lua project and i have a trouble with skipping part of my code. I want the code to stop after part "Cool". So if i write good and it answers cool i want the rest of the code to stop since after that the next question is not relative anymore.
How it works:
Code says: Hello
You say: anything
Code says: How are you?
You say: good
after you say good it will say cool.
If you say anything other than good it will ask "Why?"
e.g. you say: bad
Code says: It will be alright
I want it to stop after "cool" and skip out the further part of the code.
os.execute(" cls ")
print("Hello")
odp = io.read()
if odp == string then
end
tof = true or false
print("How are you?")
odp2 = io.read()
if odp2 == "good" then print("Cool") tof = true
else print("Why?") tof = false
if tof == true then os.execute(" pause ")
end
end
odp3 = io.read()
if odp3 ~= math then print("It will be alright")
print("Okay, I have to go see you.")
end
os.execute(" pause ")
When you compile code, it becomes the body of a function. The prototypical way of exiting a function is with a return statement. A function can have zero or more return statements.
But, since you want to exit the program, you can instead call os.exit().
You've just got to nest your "if" statements differently. All you need to do is put the rest of the code into the "else" part of your "if" statement, like this:
os.execute(" cls ")
print("Hello")
odp = io.read()
if odp == string then
end
tof = true or false
print("How are you?")
odp2 = io.read()
if odp2 == "good" then
print("Cool")
tof = true
else
print("Why?")
tof = false
if tof == true then
os.execute(" pause ")
end
-- You had an "end" here.
odp3 = io.read()
if odp3 ~= math then
print("It will be alright")
print("Okay, I have to go see you.")
end
os.execute(" pause ")
end -- You literally just need to move it here.
That way, it only gets input from the user after it asks for it, and only if the user doesn't answer, "good" to the "How are you?" question.
Note that I re-indented the code, but it's still the same code in the same order. I just made it more standard-looking and easier to visually see the structure of the program.
I have the following code:
local text = 'CIA'
if text == text..'+X' then
print 'true'
else
print 'false'
end
I want check if last text ends with ('+X')
The string.sub function extracts a substring. Negative indices start from the end.
if string.sub(text, -2) == '+X' then
-- Ends with +X, do stuff accordingly.
end