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

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

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.

5 comments

0

Learn indenting and don't use while wait() do, you don't even know how and why that idiom works

0

Hes clearly new at coding, give him a break.

0

No. There is not an excuse to not indent.

0

indent pls

0

also use a changed event

2 answers

✓ Accepted answer

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)

3 comments

0

Thanks a lot man! I tested this and it worked perfectly!

0

:)

0

:)

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)

2 comments

0

yeah don't explain

0

xd

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