How do i make a code not wait until the last second while using "while wait() do"?

hello, i want to create a script that keeps looping an audio between two timestamps until a variable changes, and I used “while wait() do” for it, however, I have found that “if nukecountdown == 2 then
break” only updates happens when the wait(16) is over, even if the proximity prompt is triggered earlier, any way to fix this or a workaround? I’ve tried using bools and “while true do” but ran into the same issue.
here is my code below, I am sorry if nothing I am saying makes sense, I am writing this very late in the night.


local sound = game.Workspace.SoundName
sound.TimePosition = 39.95

script.Parent.MouseClick:Connect(function()
	if nukecountdown == 1 then
		sound:Play()
		game.Workspace.NukeLaunch.ProximityPrompt.Enabled = true
		if nukecountdown == 1 then
			repeat
				wait(16)
				while wait() do
					if nukecountdown == 2 then
						break
					end
				end
				sound.TimePosition = 39.95
			until nukecountdown == 2
		end
	end
end)

game.workspace.NukeLaunch.ProximityPrompt.Triggered:Connect(function()
	nukecountdown = 2
end)
3 Likes

I don’t understand what you’re trying to do here. Are you trying to sort of intercept it when the player triggers the proximity prompt? Or are you trying to make it so the player can launch it early?

Regardless, I think you’re using loops improperly:

if nukecountdown == 1 then
    local initTick = tick()
    while (tick() - initTick) < 16 and nukecountdown == 1 do
        task.wait()
    end
    -- it will have either waited 16 seconds or until nukecountdown doesn't equal 1
end
2 Likes

I think he wants it to restart before it gets to the end of the loop. Like continue

while true do
     If a == b then
        Continue
     end
     Print ("this won't run if a = b")
end
1 Like

well from start to finish:
-a player clicks on a part with a click detector
-an audio starts playing
-the audio keeps looping at 39.95 through to 45.95 until the player interacts with a proximity prompt
-afterwards, the audio jumps to 62.5 (which I haven’t implemented yet but have a solid idea on how to do it)
and thank you, I will try out your solution when I wake up.

1 Like

Oh. You don’t need an external variable then:

local hasInteracted = false
audio.TimePosition = 39.95
coroutine.wrap(function()
    while not hasInteracted do
        audio.TimePosition = 39.95
        audio:Play()
        task.wait(6)
    end
end)()
proximityPrompt.Triggered:Wait()
hasInteracted = true
audio.TimePosition = 62.5
1 Like

thank you, it works, however I still have a couple of questions:

  1. is there any way to make the script stop without waiting for the script to end after hasInteracted is set to true?
  2. if there is, how do you make it so that the script can be stopped and restarted infinitely (unless hasInteracted was set to true in the process)?

Just put it inside of your MouseClick connection, that should make it able to be restarted infinitely.

yes, sorry, I didn’t realize I had the has interacted bool messed up, although, that still leaves question 1 unanswered, so do you have any idea on how to do that? (I tried adding another value and doing “while disabled do break” but that didn’t work, so the only other solutions i can think of are ones that already failed in the past, them being script.destroy and script.disabled = true)

I am sorry if I am being too much of a burden on you right now.

Putting it inside the MouseClick connection should solve both of those issues, you shouldn’t need more than 1 loop

well it works for the second one, but it doesn’t work for the first one, since I want to be able to disable the script and mute the audio at any point, while in the current script in order to stop it and mute the audio I have to set has interacted to true and wait for the audio to be over

although, I might just be stupid and messed something up while adjusting your code, can you please take a look?

local proximityprompt2 = game.Workspace.NukeStop.ProximityPrompt
local sound = game.Workspace.BattleOnIce
local hasInteracted = false
sound.TimePosition = 39.95


script.Parent.MouseClick:Connect(function()
	proximityprompt.Enabled = true
	proximityprompt2.Enabled = true
	coroutine.wrap(function()
		while not hasInteracted do
			sound.TimePosition = 39.95
			sound:Play()
			task.wait(16)
		end
	end)()
end)

proximityprompt.Triggered:Wait()
hasInteracted = true
sound.TimePosition = 62.5

This should be in your connection as well.

ok, thank you, but my question was how do I stop the loop and mute the audio without setting hasinteracted to true, sorry for not wording it properly.

Not sure I understand, you need the hasInteracted boolean to dictate whether the prompt has been played or not, that will tell the coroutine to stop setting the sound to 39.95. Your hasInteracted boolean should be inside of the mouseClick connection though.

well I’ll try to explain it in in game events

-a player clicks on a part with a click detector
-an audio starts playing
-the audio keeps looping at 39.95 through to 45.95 until the player interacts with a proximity prompt

if the player doesn’t interact with the proximity prompt, and let’s say, is killed, or somebody presses a different proximity prompt, or literally any other function happens, it doesn’t matter, then the loop stops and the audio stops playing, however hasinteracted is not set to true in the process

is that possible with this current script or is something else required?