i made a script that if the number value in workspace is set to "0" then it will play and loop it, if not, it will not play, but it seems the sound will play and loop everytime even when when the number value is "2" or "15"
01 | game.Workspace:WaitForChild( "planet" ) -- the number value |
02 | local planetnumber = game.Workspace.planet.Value |
03 | local sound = game.Workspace.jupiter.jupitersound -- the sound is named "jupitersound" inside a model called "jupiter" |
04 |
05 | while planetnumber = = 0 do |
06 | if planetnumber = = 0 then |
07 | sound.Looped = false |
08 | sound.Playing = true |
09 | else |
10 | sound.Playing = false |
11 | end |
12 | wait() |
13 | end |
In your code, planetnumber
is a variable that gets a copy of the ValueObject's value once and then never changes. It's not a reference that updates when you change the ValueObject.
What you probably want is to remove the while loop and instead connect up a listener for changes to the value object with something like:
01 | local function UpdateSound(planetNumber) |
02 | sound.Playing = (planetNumber = = 0 ) |
03 | end |
04 |
05 | game.Workspace.planet.Changed:Connect( function (newValue) |
06 | UpdateSound(newValue) |
07 | end ) |
08 |
09 | -- First time |
10 | UpdateSound(game.Workspace.planet.Value) |
Since sound.Looped is only ever set to false, you should just check that box on the Sound object itself. Code is only needed if you change it back and forth from false to true.