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

Sound still plays even if the number value is different, what am i doing wrong?

Asked by 5 years ago
Edited 5 years ago

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"

01game.Workspace:WaitForChild("planet") -- the number value
02local planetnumber = game.Workspace.planet.Value
03local sound = game.Workspace.jupiter.jupitersound -- the sound is named "jupitersound" inside a model called "jupiter"
04 
05while planetnumber == 0 do
06    if planetnumber == 0 then
07    sound.Looped = false
08    sound.Playing = true
09else
10    sound.Playing = false
11end
12    wait()
13end

1 answer

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

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:

01local function UpdateSound(planetNumber)
02    sound.Playing = (planetNumber == 0)
03end
04 
05game.Workspace.planet.Changed:Connect(function(newValue)
06    UpdateSound(newValue)
07end)
08 
09-- First time
10UpdateSound(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.

Ad

Answer this question