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

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

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

✓ Accepted answer

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

2 comments

0

For some reason, it still only works once.

0

I fixed it, I had to check if the song is playing and if it's not.

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