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

How to locally play a song in magnitude?

while true and wait() do
for o,p in pairs(game.Players:getChildren()) do
if (p.Character:findFirstChild("Torso").Position-script.Parent.Position).magnitude<10 then
script.Parent.Sound:Play()
else script.Parent.Sound:Stop()
end end
end

What is the most efficient way on making it so it plays once, and if you're over the magnitude of "10" then it stops? Also, how would I make it so the sound only plays to the certain character? Cloning the Sound into the Character, then playing it?

Help would be appreciated.

2 comments

1

Sorry, I cannot help with the magnitude part. Although, for just one player to hear the sound, you must clone it to that players PlayerGui.

0

Using a LocalScript, play/stop the sound.

1 answer

✓ Accepted answer

I recommend you use Coroutines and Debounce. Coroutines so you can accurately check if the player is near the part, and debounce so only play the sound once. Alternatively to coroutines, you can use the PlayerAdded() and CharacterAdded() events, so that it runs on a separate thread and works for every player without having to rescan them. Also, do you want the sound to be played once and once only, or do you want it to play whenever the player steps near the part, so that it will remain repeatable? In the finished script I made, it's repeatable and cuts whenever the player steps out of range.

Finished Script:

--Assuming the sound is named Sound and is located in StarterGui
local debounce = {}

game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(character)
while true do
if not (player or character) then break end
if (character:FindFirstChild("Torso").Position-script.Parent.Position).magnitude<=10 then
if not debounce[player.Name] then
debounce[player.Name] = true
player.PlayerGui.Sound:Play()
end
else
debounce[player.Name] = nil
player.PlayerGui.Sound:Stop()
end
wait()
end
end)
end)

4 comments

0

I've edited and tested my answer, works just fine.

0

This code will seem to work just fine in Solo mode, but it might play over other people's sounds. It's best to use LocalScripts to do it.

0

It doesn't actually, I tested it. It works fine because the sound is in PlayerGui, making it player specific. Putting the script in a local script would accomplish virtually nothing except cause more lag because it would require more instances.

0

Figures, I answer a question correctly, efficiently and with effort and I end up losing a point :/

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