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

How to locally play a song in magnitude?

Asked by
Qorm 100
9 years ago
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.

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. alphawolvess 1784 — 9y
0
Using a LocalScript, play/stop the sound. Tkdriverx 514 — 9y

1 answer

Log in to vote
-1
Answered by 9 years ago

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)
0
I've edited and tested my answer, works just fine. aquathorn321 858 — 9y
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. Tkdriverx 514 — 9y
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. aquathorn321 858 — 9y
0
Figures, I answer a question correctly, efficiently and with effort and I end up losing a point :/ aquathorn321 858 — 9y
Ad

Answer this question