hint = Instance.new("Hint",game.Workspace)
hint.Text = 0
while true do
hint.Text = hint.Text + 1
wait(1)
end
if hint.Text = 10 then --Error here
hint.Text:Destroy()
end
Help with error?
2 answers
The error is that = does not compare things, it only assigns them.
You have to use == to compare:
if hint.Text == 10 then
However, that won't work. Text is a string (text), but 10 is a number -- they aren't the same kind of thing, so they aren't equal:
if hint.Text == "10" then
That will work. However, that if statement will never be executed, because the while loop before it never finishes. Remember, code runs in sequence -- if a first thing never ends, the second thing never begins.
You could add your code to the condition of the while instead:
hint = Instance.new("Hint", workspace)
hint.Text = "0"
while hint.Text ~= "10" do
wait(1)
hint.Text = hint.Text + 1
end
hint:Destroy()
However, this sort of behavior is why there is a for loop in Lua! Don't use a while loop and an if for the end for this stuff:
hint = Instance.new("Hint", workspace)
for t = 1, 10 do
hint.Text = t
wait(1)
end
hint:Destroy()
For Loops
for loops do a block of code many times, "for each" thing in a range, collection, list, etc.
The numeric for loop is what was used here. It looks like this:
for someVariable = start, stop do
-- do stuff with `someVariable`
end
Each time the loop happens, you'll get another value for someVariable -- start, start + 1, start + 2, ... , stop - 1, stop.
So if you want the numbers 1 to 10 to be printed out, that's simply
for n = 1, 10 do
print(n)
end
If you wanted to print the first ten cube numbers:
for n = 1, 10 do
print(n ^ 3)
end
If you want to go over a range of numbers, this is the best, cleanest way to do it.
They also have an optional "step", if you want to increase by more (or less) than 1. This will print all the even numbers:
for n = 0, 20, 2 do
print(n)
-- n = 0, 0 + 2, 0 + 2 + 2, 0 + 2 + 2 + 2, ...
end
First when you're trying to know if a value equal to another value you need to put two equals like that:
if 5 + 3 == 8 then
print("Awesome, 5 + 3 is 8 !!")
end
and hint.Text is a string so you need to put the 10 in this little thing '10' or again this "10" sorry I don't know what this the name of this little "thing" hehe.
now it suppose to look like that:
hint = Instance.new("Hint",game.Workspace)
hint.Text = 0
while true do
hint.Text = hint.Text + 1
wait(1)
end
if hint.Text == "10" then
hint.Text:Destroy()
end
SURPRISE It didnt work because the loop is before your code and after a loop you can't run a code excepted by another way like coroutine heres an exemple:
coroutine.resume(coroutine.create(function())
while true do
hint.Text = hint.Text + 1
wait(1)
end
end))
but again you'll get an error because when you'll destroy your hint it'll be nil.
now you have two ways for fix that.
the first way is you can use repeat
repeat
--body
until hint.Text == "10"
and for the second you can use while too o:
while hint.Text ~= "10" do
--body
end
now your code will look like that:
hint = Instance.new("Hint",game.Workspace)
hint.Text = 0
while hint.Text ~= "10" do
hint.Text = hint.Text + 1
wait(1)
end
hint:Destroy()
Yup I more love while ;)
enjoy !
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
I don't understand "for" loops.