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"

game.Workspace:WaitForChild("planet") -- the number value
local planetnumber = game.Workspace.planet.Value 
local sound = game.Workspace.jupiter.jupitersound -- the sound is named "jupitersound" inside a model called "jupiter"

while planetnumber == 0 do
    if planetnumber == 0 then
    sound.Looped = false
    sound.Playing = true
else
    sound.Playing = false
end
    wait()
end

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:

local function UpdateSound(planetNumber)
    sound.Playing = (planetNumber == 0)
end

game.Workspace.planet.Changed:Connect(function(newValue)
    UpdateSound(newValue)
end)

-- First time
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.

Ad

Answer this question