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

How do I make a script run everytime a BoolValue's value changes?

Asked by 5 years ago
Edited 5 years ago

So, I'm making a game and it has these songs that play depending on the value of a BoolValue in ReplicatedStorage, this is my code:

local sound = script.Parent.Ambient 
local sound2 = script.Parent.Combat
local value = game.ReplicatedStorage.Combat
if value.Value == false then
    sound2.Volume = 0.5
    wait()
    sound2.Volume = 0
    wait()
    sound2:Stop()
    sound:Play()
    sound.Volume = 0.5
    wait()
    sound.Volume = 1
else
    sound.Volume = 0.5
    wait()
    sound.Volume = 0
    wait()
    sound:Stop()
    sound2:Play()
    sound2.Volume = 0.5
    wait()
    sound2.Volume = 1
    end

I want the script to run everytime the value changes to either true or false. Notes: I have attempted the Changed event, but it did not work at all. (It doesn't play any song). If I use "while wait() do" then it'll just keep playing the song from the start. EDIT: Ok, I hope the script is proper now. Anyways, I said in the notes that I have used the Changed event, but it doesn't work. No song is playing, even when changing the value.

0
Learn indenting and don't use while wait() do, you don't even know how and why that idiom works User#24403 69 — 5y
0
Hes clearly new at coding, give him a break. moo1210 587 — 5y
0
No. There is not an excuse to not indent. User#24403 69 — 5y
0
indent pls LoganboyInCO 150 — 5y
0
also use a changed event LoganboyInCO 150 — 5y

2 answers

Log in to vote
0
Answered by
birds3345 177
5 years ago

This can be done with the ":GetPropertyChangedSignal" function, the difference between the "changed" function and the ":GetPropertyChangedSignal" is that the "changed" function tests if all the properties changes but the ":GetPropertyChangedSignal" test if only 1 property changes. This can be useful for this question because as you stated the changed function didn't work for you assuming that you used it right. Here is the code I wrote, I did not have time to test this but I think it will work:

local sound = script.Parent.Ambient 
local sound2 = script.Parent.Combat
local value = game.ReplicatedStorage.Combat
value:GetPropertyChangedSignal("Value"):Connect(function()
if value.Value == false then
    sound2.Volume = 0.5
    wait()
    sound2.Volume = 0
    wait()
    sound2:Stop()
    sound:Play()
    sound.Volume = 0.5
    wait()
    sound.Volume = 1
else
    sound.Volume = 0.5
    wait()
    sound.Volume = 0
    wait()
    sound:Stop()
    sound2:Play()
    sound2.Volume = 0.5
    wait()
    sound2.Volume = 1
    end
    end)
0
Thanks a lot man! I tested this and it worked perfectly! Mark512345 2 — 5y
0
:) birds3345 177 — 5y
0
:) birds3345 177 — 5y
Ad
Log in to vote
0
Answered by
moo1210 587 Moderation Voter
5 years ago
Edited 5 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

You can do that by doing this: Using the changed will run this function everytime something is changed in this object. So this would do what you would want.

script.Parent.Parent.CheckBox2.checked.Changed:Connect(function()

--your code here

end)
0
yeah don't explain User#24403 69 — 5y
0
xd DeceptiveCaster 3761 — 5y

Answer this question