My code doesn't do anything - lua

game:GetService("Players").PlayerAdded:connect(function()
for _, Player in pairs(game:GetService("Players"):GetPlayers()) do
Player.Chatted:connect(function(msg)
if string.sub(msg,1,5) == "oofergang" then
Player:Kick("no no no cringe baby")
end
end)
end
end)
return ''
How do I fix this? It doesn't do anything, no errors nothing.

Your issue seems to be in your string.sub() usage. (I'm assuming this is Roblox, which I don't know much about).
The string.sub(a, b, c) method takes the substring of the string a, starting from index b and going to index c.
Your problem is that you're trying to get the substring from characters 1-5. Character 1 is the first character and character 5 is the 5th character in the string. Your if block is checking the first 5 characters of the player's message. The issue is that the string you're comparing it to, "oofergang", is longer than 5 characters.
If the player does correctly type oofergang, the string.sub() that you're using will output oofer, which is the first 5 characters of the message. Essentially, this is what the program will see when running:
if "oofer" == "oofergang" then
oofer is never going to equal oofergang.
If you want to check if the player starts their message with oofergang then you should use the following if block instead:
if (string.sub(msg, 1, 9) == "oofergang") then
--Whatever you want to do here, in your case kick the player
end
EDIT: As suggested, the following code allows you to find a string within another string ANYwhere, not just a the start:
if (string.find(msg, "oofergang")) then
--Whatever you want to do here, in your case kick the player
end

Related

Is it possible to move the for loop counter in lua?

I am trying to move my i location forward and backward if a certain string arrives kind of like assembly code, is this possible using lua?
something like this:
local array = {"Hi", "Goodbye", "Cat"}
for i in pairs(array) do
if string.find(array[i], "Hi") then
move_to(3) -- so it will basically skip over "Goodbye" but I need this on a large scale so it can jump from 3 to 234 and then jump back to 1 etc etc
elseif array[i] == "Cat" then
print("Cat")
end
end
Also I cannot just make a variable to check if a jump is in progress and just ignore the other values until I am at the descried location, because then I cannot jump backwards
Thanks!
You cannot use a numeric for loop or a generic for loop with the standard iterator functions to do this. You cannot properly control their state from inside. Just use a while loop.
while notDoneCondition do
-- do stuff that may trigger the jumpCondition
if jumpCondition then
pos = jumpTarget
end
end

Infinite loop bug in Lua

I'm new to this platform and I'm still learning to
program in Lua, so, if any newbie errors appear, forgive me.
The following code is from one of the functions in my project that reads the insert
of the user and validates whether or not it is a data of type "Number". If,
the loop will be broken and the function will return the user input, otherwise, the
program will ask the user to enter the data again:
function bin.readnum(text)
local insertion
if text == nil then text = "Text: " end
while (insertion == nil) do
insertion = nil
print(text)
insertion = io.read("number")
if insertion ~= nil then break end
end
return insertion
end
But, if the user enters a wrong data (string) the function prints the text
madly instead of asking the user to re-enter the data.
When io.read fails to parse the data it got into a number, it doesn't discard it, but instead leaves it in the buffer for the next call to it. That means that in your code, instead of letting the user enter something else, it'll just keep trying to parse the same non-number forever. To fix it, in your if insertion ~= nil then block, do io.read() right before break, to read and discard the whole invalid line.
In addition to what Joseph Sible said:
io.read("number") is wrong: 5.1 docs demand "*n" and 5.4 docs demand just "n" for reading numbers. It probably works nevertheless due to Lua just searching for the chars in the string.
I recommend just replacing insertion = io.read("number") withinsertion = tonumber(assert(io.read(), "EOF")) - this will read a line and try to parse it as a number; the assert gracefully deals with nil being returned by io.read for EOF.
You don't need to set insertion to nil, the later assignment will do that already if what was read is not a valid number.
Style: Consider replacing your explicit nil checks with truthiness checks and removing the parentheses around the while-condition. You don't need a break, you can immediately return the read number; finally, you can even replace the entire loop with tail recursion.
All in all I'd rewrite it as follows:
function bin.readnum(text)
print(text or "Text: ")
local num = tonumber(assert(io.read(), "EOF"))
if num then return num end
return bin.readnum(text)
end
or alternatively using a repeat-until loop:
function bin.readnum(text)
local num
repeat
print(text or "Text: ")
num = tonumber(assert(io.read(), "EOF"))
until num
return num
end

Why does using two variables give me an error message when using an if statement?

I'm attempting to write a script in LUA for the Minecraft mod ComputerCraft. It's supposed to send a turtle down, mine a hole, and place ladders before returning to the surface. I'm trying to make an error display when the turtle doesn't have enough ladders, but I'm receiving an error that prevents it from running. "mineDown :18: attempt to compare string with number expected, got string."
-- This gets the user to tell the turtle how far to dig down
print('How far down should I go?')
distDown = io.read()
distMoved = 0
ladders = turtle.getItemCount(13)
-- Check if the number of ladders is less than the distance needed to move. If so, returns error.
turtle.select(13)
if ladders < distDown then
error('Not enough ladders!')
end
The error means that ladders is number and distDown is a string. You need to convert them to the same type. For example to convert ladders to a string use tostring or distDown to a number use tonumber:
if ladders < tonumber(distDown) then

Ruby - Writing a program that guesses letters in a word

Background: Using a class, I'm building a hangman-like game where player 1 enters a word to be guessed, and player 2 attempts to guess it. The number of guesses allotted to player 2 is relative to the word in question, but repeated guesses do not count against the player.
Each guess should provide continual feedback to player 2 by showing their progress towards guessing the word, which should be printed at the end of each guessing phase. ex) The word 'code' would be displayed as "_ _ _ _ ", if the letter 'o' were to be guessed, the feedback would look like " _ o _ _", etc. Once the word is guessed or a player has 0 attempts left, a winner is announced.
Issue 1: I can't get the program to close when the game_won? method evaluates to true. It continues to run until attempts == game_word.length + 2. Any ideas on how to end the program with the winning statement?
Issue 2: I tried adding a game_lost method, but couldn't get it to work once player 2 runs out of attempts (tried creating an instance variable to be tied to attempts in the interface, but whenever it was called outside of the class, an error popped up stating that it was a nil class, rather than an integer. How can I make a functional method that states when the game is lost?
Issue 3: Whenever an incorrect letter is entered, the "Nope, try again..." response is printed out as many times as the length of the word is. It seems to be printing out whatever is evaluated last, x amount of times, in the guess_the_letter method.
Issue 4: If the word entered contains more than one of the same letter, the word progress update will appear as many times as that letter exists within the game word. (Seems to be a similar issue to issue 3) Any ideas as to what I'm doing wrong here.
class GuessingGame
def initialize(word)
#word = word.downcase
#display_word = "_" * word.length
end
# Take a guess in the form of a letter and check to see if it is in the
# target word, update the word pattern to include the missing letter
def guess_the_letter(g_letter)
g_letter.downcase
#word.split("").each_with_index do |w_letter, index|
if g_letter == w_letter
#display_word[index] = g_letter
puts "Here is your progress towards guessing the word:"
p #display_word
end
if !#word.include? (g_letter)
puts "Nope, try again..."
end
end
end
# Determine winning conditions
def game_won?
if #word == #display_word
puts "Congratulations Player 2, you won!"
true
else
false
end
end
def game_lost?
#method body
end
end
puts "Welcome to the Word Guessing Game!"
puts "This game is for 2 players."
puts "Player 1, please enter a word for player 2 to guess..."
game_word = gets.chomp
game = GuessingGame.new(game_word)
attempts = 0
guessed_letters = []
#Create an interface for the users to play the game
until attempts == game_word.length + 2
puts "Player 2, please guess a letter..."
letter_guess = gets.chomp
if guessed_letters.include? letter_guess
puts "You already tried that letter, enter a new one."
letter_guess = gets.chomp
end
guessed_letters << letter_guess
game.guess_the_letter(letter_guess)
game.game_won?
attempts += 1
end
When asking this kind of question, you should indicate, where line 46 is.
In your case, I guess it's the expression attempts == #word.length. You are not in class context, so #word does not refer to the instance variable in GuessingGame.
You can not directly access an instance variable of another object (i.e. an object different to self), so you need to provide an accessor method. Ruby makes this easy using attr_reader:
attr_reader :word
creates a read-accessor method to #word which is called word.
UPDATE: I just see that you have modified your original code. With the new code, you will have the same problem with #bad_guesses.
BTW, if you edit your posting, please always explain what you changed.

In Lua, how do I prevent a program from crashing, after a user left an value blank, which the program needs to calculate?

I wanted to create a program for me and friends, which calculates a value.
However I also wanted to make the program uncrashable, so my friends dont tell me how they broke my code.
So the problem is that they could leave this: d = io.read()
blank resulting the program to crash once it wants to calculate d.
So how do I stop them from leaving d blank or entering a string?
You can make it so it defaults to 0
local d = tonumber(io.read()) or 0

Resources