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.
My script inside InsidePart
:
1 | script.Parent.Touched:Connect( function (hit) |
2 | if hit.Parent:FindFirstChild( 'Humanoid' ) then |
3 | workspace.InsideAudio:Play() |
4 | workspace.OutsideAudio:Stop() |
5 | end |
6 | end ) |
My script inside OutsidePart
:
1 | script.Parent.Touched:Connect( function (hit) |
2 | if hit.Parent:FindFirstChild( 'Humanoid' ) then |
3 | workspace.InsideAudio:Stop() |
4 | workspace.OutsideAudio:Play() |
5 | end |
6 | end ) |
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!
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 |
02 | local Players = game:GetService( "Players" ) -- We get the Players Service in order to get LocalPlayer, and using GetPlayerFromCharacter method. |
03 | local RunService = game:GetService( "RunService" ) -- We will be using RunService.Heartbeat |
04 | -- event to check whether or not our player is in the area |
05 |
06 | local Client = Players.LocalPlayer -- I prefer to call LocalPlayer client; |
07 | -- it's just my personal preference. |
08 | local InsideAudio = workspace.InsideAudio -- or path to the audio |
09 | local OutsideAudio = workspace.OutsideAudio -- or path to the audio |
10 |
11 |
12 | local InsidePart = workspace.InsidePart -- or path to the part |
13 | local OutsidePart = workspace.OutsidePart -- or path to the part |
14 |
15 | function CreateRegion 3 FromPart(Part) |
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.