Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I repeat a If Statement?

Asked by 5 years ago
Edited 5 years ago
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.

0
Your question is too complex without knowing how your "Inventory" works. Please give some explanation and show some code. pidgey 548 — 5y
0
Yeah, you need to give us a full in-depth on it, or I can't help as easily xD. TheOnlySmarts 233 — 5y
0
Alright I'll make it a bit simpler. Starflyerz 44 — 5y
0
Try my script. CaptainD_veloper 290 — 5y
0
Is this a GUI, or what? CaptainD_veloper 290 — 5y

3 answers

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

.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)
2
Thanks! Starflyerz 44 — 5y
0
:connect is deprecated. use :Connect yHasteeD 1819 — 5y
0
i forgot to correct his already deprecated use, its fixed. DinozCreates 1070 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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?

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. Starflyerz 44 — 5y
0
Yeah I think that's what he wants, nice job. TheOnlySmarts 233 — 5y
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! Starflyerz 44 — 5y
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. TheOnlySmarts 233 — 5y
View all comments (3 more)
0
Well yeah I'm actually using a MouseButton1Click Function inside of it, so maybe that's why. Starflyerz 44 — 5y
0
Most likely, rip. TheOnlySmarts 233 — 5y
0
Star it would help if you sent us some code but the method should be the same as I posted windstrike 27 — 5y
Log in to vote
0
Answered by 5 years ago

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 
0
Same thing happened, even though the value was true, the false code still ran. Starflyerz 44 — 5y
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. TheOnlySmarts 233 — 5y

Answer this question