Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I repeat a If Statement?

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.

5 comments

0

Your question is too complex without knowing how your "Inventory" works. Please give some explanation and show some code.

0

Yeah, you need to give us a full in-depth on it, or I can't help as easily xD.

0

Alright I'll make it a bit simpler.

0

Try my script.

0

Is this a GUI, or what?

3 answers

✓ Accepted answer

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

3 comments

2

Thanks!

0

:connect is deprecated. use :Connect

0

i forgot to correct his already deprecated use, its fixed.

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?

7 comments

0

I tried this, but it ran the function about 100 times at once, instead of just running one time. And the false code just wont run when the BoolValue becomes false. The true value would always be running.

0

Yeah I think that's what he wants, nice job.

0

Well then why isn't this working like I'd like it to? I tried everything I know, but the true value code keeps running even if the BoolValue is false!

0

If it keeps on running it, add in the loop, boolvalue.Value = false, making that loop stop, and in the false loop, add your stuff in there. However, if you are not going to have just a print in the false loop then this would be more complex than it should be.

0

Well yeah I'm actually using a MouseButton1Click Function inside of it, so maybe that's why.

0

Most likely, rip.

0

Star it would help if you sent us some code but the method should be the same as I posted

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 

2 comments

0

Same thing happened, even though the value was true, the false code still ran.

0

Are you testing it out in studio or the actual game, also, make sure you're using RemoteEvents if you're trying to get to the PlayerGui through the Server.

This discussion is preserved as it was. New answers, comments, and votes are closed.