Water rising and descending

57 Views Asked by At

im in a process of making a roblox game. I want to make the water rise and descend again down. I think i did something wrong with coding,(its my second day of coding and i cant find any video on it) im not sure what. So everytime i run roblox after 1st descending and starting of 2nd game, it freezes,water dissapears and it throws a error: Script timeout: exhausted allowed execution time. Here is my code:

wait(21)
t = 17
repeat
    t = t-1
    script.Parent.Position = script.Parent.Position + Vector3.new(0, 1, 0)
    wait(2)
until
    t == 0
wait(1)
t = 17
repeat
    t = t-1
    script.Parent.Position = script.Parent.Position + Vector3.new(0, -1, 0)
    wait(0.5)
until
t == 0
wait(9)
t = 17

while true do
    t = t-1
    script.Parent.Position = script.Parent.Position + Vector3.new(0, 1, 0)
end
wait(2)
while false do
    t = t-1
    script.Parent.Position = script.Parent.Position + Vector3.new(0, -1, 0)
end

1

There are 1 best solutions below

4
David On

Script timeout: exhausted allowed execution time.

Because the code never stops looping here:

while true do
  //...
end

This will continue to loop forever. (Well, for as long as true is true. And I can't think of a condition in which it wouldn't be.)

Contrast that with your loops above:

t = 17
repeat
  t = t-1
  //...
until
  t == 0

In this case the loop explicitly repeats "until t equals 0", and each iteration of the loop brings t one step closer to 0.

Modify your later loops to have similar structures and conditions. For example:

t = 17
while t > 0 do
  t = t-1
  //...
end

Note also that this loop will never execute:

while false do
  //...
end

Because false is never true. Now is a good time to take a step back and consider the logic of what you want these loops to do. Just like with your first two loops, modify your second two loops to indicate how many times they should repeat.