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

Sound doesn't replay after reentering with a certain magnitude value?

Asked by
yoshi8080 445 Moderation Voter
8 years ago

I wanted the sound to play once the player torso's distance is close by the noob and stop when the player's distance is too far.

local player = game.Players.LocalPlayer
local noob = game.Workspace.NoobStand.Noob
local song = Instance.new('Sound',player.PlayerGui)

song.SoundId = 'rbxassetid://302095292'
song.Volume = 1
song.Looped = true

    while wait() do
        local magnitude = (noob.Torso.Position - player.Character.Torso.Position).magnitude
            if magnitude < 6 then
        song:Play()
        print("Enter")
            elseif magnitude > 6 then
        print("Exit")
        song:Stop()
                    end
                        end

No errors in output.

1 answer

Log in to vote
1
Answered by 8 years ago

I think that you should try using WaitForChild because I have a feeling the character isn't loading when this runs. Try this:

local player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Torso = Character:WaitForChild("Torso")
local noob = game.Workspace.NoobStand.Noob
local song = Instance.new('Sound',player.PlayerGui)

song.SoundId = 'rbxassetid://302095292'
song.Volume = 1
song.Looped = true

    while wait() do
        local magnitude = (noob.Torso.Position - Torso.Position).magnitude
            if magnitude < 6 then
        song:Play()
        print("Enter")
            elseif magnitude > 6 then
        print("Exit")
        song:Stop()
                    end
                        end

0
For some reason, it still only works once. yoshi8080 445 — 8y
0
I fixed it, I had to check if the song is playing and if it's not. yoshi8080 445 — 8y
Ad

Answer this question