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

[SOLVED] How would I make an audio play in a different area?

Asked by 5 years ago
Edited 5 years ago

Introduction

I want to create an immersive roleplay game, but I wish to make an area play a specific audio. For example, the player enters the wilderness, and I want it to play music that is related to that.

As for my attempts, I made two parts; InsidePart and OutsidePart. InsidePart lets the script know that the player has entered the inside of the area. OutsidePart will let the script know if the player has exited the area.

Scripts

My script inside InsidePart:

1script.Parent.Touched:Connect(function(hit)
2    if hit.Parent:FindFirstChild('Humanoid') then
3        workspace.InsideAudio:Play()
4        workspace.OutsideAudio:Stop()
5    end
6end)

My script inside OutsidePart:

1script.Parent.Touched:Connect(function(hit)
2    if hit.Parent:FindFirstChild('Humanoid') then
3        workspace.InsideAudio:Stop()
4        workspace.OutsideAudio:Play()
5    end
6end)

Conclusion

Any help please? It doesn't work when it's supposed to.

P.S, I heard that placing audio inside of the workspace will play it server-wide, placing audio inside of StarterPlayer will play it client-side. Let me know if you know how to make it play client-side only. Thanks!

0
you could place an invisible part to play the audio and give a specific range in which the audio plays by editing its properties. Roviospace 22 — 5y
0
Alright, I'll try that later. But what I want is when a player enters the part, it will play the audio. Thanks for the suggestion anyways. Sensei_Developer 298 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I think you'd be better off using Region3 for this kind of thing. I'll give you some example code and explain every line in detail, as well link some sources.

01-- put this in a local script
02local Players = game:GetService("Players") -- We get the Players Service in order to get LocalPlayer, and using GetPlayerFromCharacter method.
03local RunService = game:GetService("RunService") -- We will be using RunService.Heartbeat
04--  event to check whether or not our player is in the area
05 
06local Client = Players.LocalPlayer -- I prefer to call LocalPlayer client;
07-- it's just my personal preference.
08local InsideAudio = workspace.InsideAudio -- or path to the audio
09local OutsideAudio = workspace.OutsideAudio -- or path to the audio
10 
11 
12local InsidePart = workspace.InsidePart -- or path to the part
13local OutsidePart = workspace.OutsidePart -- or path to the part
14 
15function CreateRegion3FromPart(Part)
View all 97 lines...

Sources

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartsInRegion3 https://developer.roblox.com/api-reference/function/Players/GetPlayerFromCharacter https://developer.roblox.com/en-us/api-reference/event/RunService/Heartbeat https://developer.roblox.com/en-us/api-reference/datatype/RBXScriptConnection

I hope this answer was helpful, and I'm sorry if this answer is slightly bad, I'm slightly inexperienced with answering. Feel free to ask further.

Cheers, Luke.

Edit Updated the code very slightly to not repeatedly play sound's if they are inside the Area.

0
Woah woah. Slow down there, pal. Why on Earth would I need a Region3 to play audio? But thanks for answering, I might take this into consideration. Sensei_Developer 298 — 5y
0
Region3's fit well for what you want. Region3's are pretty much designed for stuff like this. guidable 42 — 5y
Ad

Answer this question