local SoundPlaying = WorkspacePlayer.UpperTorso.IsSoundPlaying if SoundPlaying.Value == false then Soundbutton.MouseButton1Click:connect(function() print("Sound Item Was equipped") playerSound.SoundId = "rbxassetid://2639636357" playerSound.Volume = 1 playerAnim.AnimationId = "rbxassetid://2607315975" playerWait.Value = 6.7649 playerEarnings.Value = 1 end) elseif SoundPlaying.Value == true then wait(0.1) end
In my game, a if statement needs to be constantly checking a BoolValue, since I'm making a inventory and it needs to check if a SoundItem is already equipped. a BoolValue is perfect for this. I know how to make it so when the BoolValue is true, code will run, but when it's false, then another type of code runs, but how do i make the If statement loop, so it can always keep checking?
Please note that the if statement is supposed to run a function, so using a while loop wouldn't work.
.Changed should only run when the value changes, let me know if this works as you're hoping for.
local SoundPlaying = WorkspacePlayer.UpperTorso.IsSoundPlaying Soundplaying.Changed:Connect(function() if SoundPlaying.Value == false then Soundbutton.MouseButton1Click:Connect(function() print("Sound Item Was equipped") playerSound.SoundId = "rbxassetid://2639636357" playerSound.Volume = 1 playerAnim.AnimationId = "rbxassetid://2607315975" playerWait.Value = 6.7649 playerEarnings.Value = 1 end) elseif SoundPlaying.Value == true then wait(0.1) end end)
So basically you wanna run a loop
while true do if boolvalue == true then --runcode elseif boolvalue == false then --runcode end wait() --so doesn't cause an infinite loop end
Is this what you're asking?
Try this:
while true do if boolvalue == true then --runcode repeat wait() until boolvalue == false elseif boolvalue == false then --runcode repeat wait() until boolvalue == true end end