Answered by
5 years ago Edited 5 years ago
Improvements
Use GetService()
for Roblox services rather than WaitForChild()
Make a local variable for instances which are used multiple times
Use workspace
instead of game.Workspace
I prefer placing the wait()
inside the loop than in the while // do as it's cleaner
RenderStepped works on every frame and only on the client so its preferred for audio changes to happen ASAP
Issues
I changed the "found" to be a table for the audio that is supposed to play so that it can be changed based on the name and not every part connected to the audio.
Switching the positions of the region and audiofolder allowed the table to run through each audio once, and the value was only changed for true as the false value was set as a default before the table was run
Revised Local Script
01 | local player = game:GetService( "Players" ).LocalPlayer |
02 | local SoundService = game:GetService( "SoundService" ) |
03 | local SoundRegionsWorkspace = workspace:WaitForChild( "SoundRegion" ) |
05 | local startgui = script.Parent |
06 | local afolder = startgui:WaitForChild( "SoundRegions" ) |
09 | for aindex, aval in pairs (afolder:GetChildren()) do |
10 | found [ aval.Name ] = false |
13 | game:GetService( "RunService" ).RenderStepped:Connect( function () |
14 | if player.Character then |
15 | for findex, fval in pairs (afolder:GetChildren()) do |
16 | found [ fval.Name ] = false |
19 | for mindex, audio in pairs (afolder:GetChildren()) do |
20 | for rindex, child in pairs (SoundRegionsWorkspace:GetChildren()) do |
21 | local region = Region 3. new(child.Position - (child.Size / 2 ), child.Position + (child.Size / 2 )) |
22 | local desc = workspace:FindPartsInRegion 3 WithWhiteList(region, player.Character:GetDescendants(), 20 ) |
24 | for rpart, part in pairs (desc) do |
25 | if part:FindFirstAncestor(player.Name) then |
26 | found [ child.Name ] = true |
30 | if found [ child.Name ] = = true and audio.Name = = child.Name and audio.IsPlaying = = false then |
32 | elseif found [ child.Name ] = = false and audio.Name = = child.Name and audio.IsPlaying = = true then |