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

My sound (if bool == true then sound:play) script is not working, Why is this?

As the title says, My sound (if bool == true then sound:play) script is not working, Why is this?

I have spent around 10 minutes trying to make the script work. Did I do something wrong? Please help me.

local sound = script.Parent.Sound

while wait(1) do
	if script.Parent.Value == true then
		sound:Play()
	else
		sound:Stop()
	end
end

https://gyazo.com/fd3c1a22c05a63124081d76ea8116054

5 comments

0

make sure it's true lol

0

It is true when I start the test...

0

it has to be false

0

I have turned it to false and it (OBVIOUSLY) does not play the audio.

0

yeah, i cant help you, the problem is that the value is false

2 answers

EricStoynov1749, sound.Value == true is nothing, the Sound property is Playing so you must do:

if sound.Playing == true then

Thats basically detecting if the Playing is true, and if it is true it will play so make sure it's true if you want to it play.

First things first, the if statement can be shortened (get rid of == true). Second of all, you're trying to check if the the BoolValue's value is true, however with your current code you actually end up checking if the BoolValue itself equals true. A BoolValue is, well, a BoolValue (an Instance), which doesn't equal true. A BoolValue's value (or any value type object for that matter) is stored in BoolValue.Value. You should also just use the BoolValue.Changed event, which will run quicker than waiting 1 second and also use less resources on the server. The .Changed event will also pass the new value. Taking all these into account, you should end up with a script looking about like this:

local sound = script.Parent.Sound

script.Parent.Value.Changed:Connect(function(newValue)
    if newValue then
        sound:Play()
    else
        sound:Stop()
    end
end)

On line 3, we connect the .Changed event to a new function, with the function taking one parameter, newValue (the event will pass the new value as this parameter). Then, on line 4, we check if newValue is true. == true is not necessary with bools, you can check if it's true simply with if bool then or if you want to check if it's false, use if not bool then. Any instance can be checked like this actually! If you were to provide a different variable type for example, it would check if it it is not nil (or if it is nil if you prefix it with not). The rest is a standard else statement.

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