Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with an if statement?

Asked by
FiredDusk 1466 Moderation Voter
8 years ago
hint = Instance.new("Hint", workspace)

hint.Text = "0"
while hint.Text ~= "10" do
    wait(1)
    hint.Text = hint.Text + 1
end 
hint:Destroy()

if hint:Destroy() then
    hint.Text = "Done" --"Done" does not show up 
end

3 answers

Log in to vote
3
Answered by 8 years ago

The loop runs forever, so code under it won't run. And saying something like:

if hint:Destroy() then
    -- blah blah
end

Is actually destroying the hint, in the if statement, and returning false. If you want to check if the hint exists, you'd use the parent of the hint, and use FindFirstChild("Hint"). Like this:

-- Assuming the hint's parent is in "Workspace"

local hint = workspace:FindFirstChild("Hint")

if hint then
    print("Hint exists")
else
    print("Hint does not exist")
end

Or:

if workspace:FindFirstChild("Hint") then
    print("Exists")
else
    print("Hint is nil")
end

And let's try using a number value for that hint text, and instead using a while loop, let's use a for loop.

local hint = Instance.new("Hint", workspace)
hint.Text =0

for i = 1,10 do
    hint.Text = i -- the variable "i" is constantly changing in order from 1-10.
    wait(1)
end

hint:Destroy()

if workspace:FindFirstChild("Hint") then
    -- The hint exists
    print("The hint still exists")
else
    -- else
    print("The hint does not exist")
end

Now, since we did call Destroy() on the hint before the if statement, it's not really necessary. So we can just say:

local hint = Instance.new("Hint", workspace)
hint.Text =0

for i = 1,10 do
    hint.Text = i
    wait(1)
end

hint:Destroy()
print("Done")

Hope that helped.

Ad
Log in to vote
0
Answered by 8 years ago

Well, I see a range of errors... First off, your while/do will repeat itself non-stop, meaning everything below it will never run.

Second of all, you remove the hint, then run an if statement on a method (which can't be done).

Then you try to change the instance's Text property regardless of the fact that it isn't there...

hint = Instance.new("Hint", workspace)

hint.Text = "0"
while hint.Text ~= "10" do --Loop will never end
    wait(1)
    hint.Text = hint.Text + 1
end 
--WILL NOT RUN--
hint:Destroy() --You remove it, then try to call it in the next if statement?

if hint:Destroy() then --Calling "if" on a method (and a nil value)
    hint.Text = "Done" --"Done" does not show up   --Can't change properties of a "nil" value...
end
----------------------
0
Actually, you can perform math on a string, if that string can be converted to a number (obviously that's the preferred method, but it's not impossible). And you probably could have given him an example of a correct script. CodingEvolution 490 — 8y
0
You can convert a string to a number using tonumber. But you have to make sure (just in case or if you want to fetch user data) that the string is in fact a number. You can do this with string patterns but that's a whole other story. BobserLuck 367 — 8y
0
You can do "if tonumber(x) then" I believe. darkelementallord 686 — 8y
Log in to vote
0
Answered by 8 years ago

Well, you are trying to use math on a string. In order to use the number in the string, you must first convert it into a integer. Or you can just make a variable number and change the text to that number.

hint = Instance.new("Hint", workspace)
hit=0
hint.Text = hit
while hit<10 do
    wait(1)
    hint.Text = hit
    hit=hit+1
end 
hint:Destroy()

if hint:Destroy() then
    hint.Text = "Done" --"Done" does not show up 
end

Now, as for the if statement, you are trying to see if hint is there. You can't have hint:Destroy() in a if statement because hint:Destroy() is a function that returns nil or void. Not only that but :Destroy() will delete your hint. So you can't change the text of something that no longer exists. If you want to check if hint does exist then you can use FindFirstChild().

hint = Instance.new("Hint", workspace)
hint.Name="Test"

hit=0
hint.Text = hit
while hit<10 do
    wait(1)
    hint.Text = hit
    hit=hit+1
end 
hint:Destroy()

if game.Workspace:FindFirstChild("Test") then
    hint.Text = "Done" 
end

This if statement will never be true though because you destroyed the hint that you called test (Unless you have something else in workspace called "Test").

Answer this question