Exercise that uses a while loop (it doesn't work) correct me

Only the first Print is printed and the rest does not work, can you help me solve it please, I am new to programming and a Rookie

local targetNumber = math.random(1, 100)
local attempts = 0
local guess = 0

print("Welcome to the Guessing Game. I'm thinking of a number between 1 and 100.")

while guess ~= targetNumber do
    io.write("Enter your guess: ")
    guess = tonumber(io.read())
    attempts = attempts + 1
    
    if guess < targetNumber then
        print("Too low. Try again!")
    elseif guess > targetNumber then
        print("Too high. Try again!")
    end
end

print("Congratulations! You guessed the number " .. targetNumber .. " in " .. attempts .. " attempts.")


3 Likes

io doesn’t exist in roblox.

3 Likes

I thought so But then what do I put?

1 Like

To be honest, I don’t use LUA outside of roblox, so I’m not really clear on io’s capabilities, but if this is just a simple game in the roblox output, you can just replace the io.write with print(), and the guess = tonumber(io.read()) to math.random(1,100), or whatever interval you want.

2 Likes

I’ll try it that way after eating because I’m very hungry

1 Like

io.write and io.read are input/outputs in Lua, they don’t exist in Roblox Lua though, but you can make input/outputs using TextBox that you can input text inside, and print as the output

3 Likes

Also You should get error after script print first message.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.