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

Why does "while" have an syntax error? Please help, im fairly new to scripting

Asked by 3 years ago
if game.Loaded:Connect()
while wait(1) do
    script.Parent.Value = script.Parent.Value - 1 
end
if script.Parent.Value == 0 then 
    script.Disabled = true
    end

1 answer

Log in to vote
1
Answered by 3 years ago

ok first of all, to add to your knowledge of scripting, you can increment or un increment using

script.Parent.Value += 1
--or
script.Parent.Value -= 1

its easier, second, loook for any errors in your code and try your best to figure it out and only poste on this website as a last resort, many people here post without looking up the answer and its so painfully obvious that posting a question that has litteraly 600 results if oyu type the word is jsut annoying. anyway, i see a few problems here.

  1. You have 2 if statments, and i only see 1 end.there should be another end after the last end so everything runs if the game is loaded.

  2. Also, the if statement that checks to see if the value is 0 only runs one time, its not inside of any loop, so it wont work, you should put it inside the while loop so it checks the value and then adds 1 if its not 0, and if it is 0, then it disables the script, you can use elseif for that:


while wait(1) do--1 if script.Parent.Value == 0 then--2 script.Disabled = true elseif script.Parent.Value > 0 then script.Parent.Value = script.Parent.Value - 1 end---2// end--1//
  1. Whenever you check if its 0, just in case it breaks or goes too fast and it ends up going to negative numbers (which sometimes happens), put <= 0, which is less than or equal to 0, so even if it goes to a negative number, it will still work. always do this please.

So if im not mistaken, basically you forgot an end, and you placed the if in the wrong place, your code should be this:

if game.Loaded:Connect() --1

    while wait(1) do --2


        if script.Parent.Value <= 0 then --3

            script.Disabled = true

        elseif script.Parent.Value > 0 then

                script.Parent.Value = script.Parent.Value - 1 

        end ---3//


    end --2//

end --1//

btw i use --1, --1// as reminders (// means closed so i put it at the end of a function or loop) so i dont lose track of what end goes to what function, its easier to keep track of code, also i space out my code a lot to make it more readable, i suggest these things to speed up your coding and make it easier. i hope this is helpful

0
thx :3 for accepting answer AlexanderYar 788 — 3y
Ad

Answer this question