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)
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
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.
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.
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
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.
-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?