How can i make a script that waits for a value

Im trying to make a loop that waits for a value to be something multiple times how can i do this

1 Like

Can you elaborate? It’s very hard to understand what you mean

1 Like

so im making a loop that is synced to music and i tried using something like wait until but that didn’t work but it needs to wait until a vale is something and then can do something

while true do
   if value then
      --do stuff
   end
   wait(1) -- depending how often you need to check, wait() is not recommended, but go from like 0.1 minimum
end

Alternative, if you’re waiting for a music to end than there’s an event for it

You can do

sound.Ended:Connect(function()
   -- do stuff
end)

or if you’re specifically waiting for a song to end to continue you can do

sound.Ended:Wait() -- it will stay here until the sound is over, it will run what's before it but only run what's after when the sound is over
2 Likes

You can use repeat until for this which basically repeats code until true is returned, which happens when for example two values match:

repeat wait() until value == valueyouwant

Edit:
And if you want it to wait until that value has been reached mutliple times, just add a counter to how many times you have reached it already like this:

local counter = 0
repeat wait()
   if value == othervalue then
      counter += 1
   end
until counter >= amountoftimes
3 Likes

Maybe you are looking for sound.TimePosition which returns what time the sound is at.

1 Like

but what if i want more then one if with different values

You can use the boolean operators: “and”, “or” to connect multiple statements

1 Like

while wait() do
if value then
– stuff
break
end
end

1 Like

but i need multiple ifs in one loop

local valueOne = game.Workspace.Value1
local valueTwo = game.Workspace.Value2

repeat
	--In here you can create as many if statments as you want this will keep looping 
    --until the condisin is meath
until valueOne == true and valueTwo == true

Hope this helps

2 Likes

i dont think this will work because it doesn’t wait between them right?

what are you trying to do? Can you a be more specific?
and you can wait as well

1 Like

im programming lights to music basically and need it to flash at certain points

and the wait duration is based of of a value that you are giving or?

1 Like

so like i can make it so when the position in the song = 10 lets say it flashes then at 15 it flashes again

and the flashes between each flesh is different for each song position?

1 Like
local position = {5,10,15}

while true do -- not recommended to do while wait() do
   if table.find(position, sound.TimePosition) then
      --flash
   end
   wait()
end

You can add more if statements and more tables for more functions you need, or alternative you could use a dictionary

local actions = {
   ["5"] = {"flash", "move"},
   ["10"] = {"flash"}
}

while true do
   if actions[tostring(sound.TimePosition)] then
      local actionsToDo = actions[tostring(sound.TimePosition)]
      for i,v in pairs (actionsToDo) do
         if v == "flash" then
            -- flash code
         end
      end
   end
   wait()
end
1 Like

yes it is different because sometimes it will move etc