I am trying to make something of a garbage door that opens once you click it and switches state to open / closed, however sometimes the tween moves back and forth even though the repeat count is set to 0.
The code is as follows:
ClickDetect.MouseClick:Connect(function()
while ReverseBool == false do
CoverTween:Play()
HandleTweenmove:Play()
CoverTween.Completed:Wait()
HandleTweenmove.Completed:Wait()
ReverseBool = true
end
while ReverseBool == true do
CoverTweenReverse:Play()
HandleTweenReverse2:Play()
CoverTweenReverse.Completed:Wait()
HandleTweenReverse2.Completed:Wait()
ReverseBool = false
end
end)
1 Like
Not really sure why you are using a while loop but you can do something like this instead:
ClickDetect.MouseClick:Connect(function()
if ReverseBool == false then
CoverTween:Play()
HandleTweenmove:Play()
CoverTween.Completed:Wait()
HandleTweenmove.Completed:Wait()
ReverseBool = true
else
CoverTweenReverse:Play()
HandleTweenReverse2:Play()
CoverTweenReverse.Completed:Wait()
HandleTweenReverse2.Completed:Wait()
ReverseBool = false
end
end)
2 Likes
How did I not think of something that simple?
It solved the issue, thank you!
1 Like
I have a question, how do I debounce the sound in this particular case?
ClickDetect.MouseClick:Connect(function()
local OpenSound = Instance.new("Sound")
OpenSound.Name = "OpeningSound"
OpenSound.Parent = script.Parent.Handle
OpenSound.Volume = 0.5
OpenSound.SoundId = "rbxassetid://" .. "7309104360"
wait()
if wait() then
if ReverseBool == false then
CoverTween:Play()
HandleTweenmove:Play()
OpenSound:Play()
CoverTween.Completed:Wait()
HandleTweenmove.Completed:Wait()
script.Parent.Handle.OpeningSound:Destroy()
ReverseBool = true
else
CoverTweenReverse:Play()
HandleTweenReverse2:Play()
OpenSound:Play()
CoverTweenReverse.Completed:Wait()
HandleTweenReverse2.Completed:Wait()
script.Parent.Handle.OpeningSound:Destroy()
ReverseBool = false
end
end
end)
I recommend you index a sound through SoundService because I’ve had issues with making new sound instances. The Sound.Loaded priority isn’t reliable and it tends to glitch and make random errors or not play so just have a sound object ready for you at all times and don’t change it’s SoundID.
ClickDetect.MouseClick:Connect(function()
local OpenSound = game.SoundService.OpenSound
if ReverseBool == false then
CoverTween:Play()
HandleTweenmove:Play()
OpenSound:Play()
CoverTween.Completed:Wait()
HandleTweenmove.Completed:Wait()
OpenSound.Ended:Wait() -- what you're interested in
ReverseBool = true
else
CoverTweenReverse:Play()
HandleTweenReverse2:Play()
OpenSound:Play()
CoverTweenReverse.Completed:Wait()
HandleTweenReverse2.Completed:Wait()
OpenSound.Ended:Wait() -- what you're interested in
ReverseBool = false
end
end)
I see issues with this structure though and it’s mainly if something completes before it’s supposed to, like OpenSound.Ended fires before CoverTween.Completed your thread will yield forever. You can instead connect a function to these events (which you must disconnect at the time you run it because it’s one-time use) in which you’ll make a variable like ReverseBoolCounter += 1 and if that’s 3 then make it 0 and then set the ReverseBool to either true or false, or ReverseBool = not ReverseBool