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

Soundregions with 2 parts of the same name. How do i make this work?

Asked by 5 years ago
Edited 5 years ago

I have a sound region script that detects if you are inside a parts region.

If you leave the area the music stops, if you walk into another parts region it starts another. and if the 2 parts overlap at any point, and you stand in the overlap, both audio plays at the same time. But if you have 2 parts of the same name, for 1 single audio, that audio doesnt play.

https://imgur.com/AIwdQr8

01local SoundService = game:WaitForChild("SoundService")
02 
03local SoundRegionsWorkspace = game.Workspace:WaitForChild("SoundRegion")
04 
05local found = false
06 
07while wait(1) do
08    for i, v in pairs (SoundRegionsWorkspace:GetChildren()) do
09        found = false
10        local region = Region3.new(v.Position - (v.Size/2),v.Position + (v.Size/2))
11        local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region, game.Players.LocalPlayer.Character:GetDescendants())
12 
13        for _, part in pairs(parts) do
14            if part:FindFirstAncestor(game.Players.LocalPlayer.Name)then
15                found = true
View all 31 lines...

I believe that whats happening is if your standing in 1 of the parts and not the other, its seeing it as true and false at the same time. and it starts and ends it at the same time. there is no error in the output, i've worked on it for 2 days and im still unsure how to structure it correctly so it can play the music if there are 2 parts of the same name

1 answer

Log in to vote
1
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

01local player = game:GetService("Players").LocalPlayer
02local SoundService = game:GetService("SoundService")
03local SoundRegionsWorkspace = workspace:WaitForChild("SoundRegion")
04 
05local startgui = script.Parent
06local afolder = startgui:WaitForChild("SoundRegions")
07local found = {}
08 
09for aindex, aval in pairs(afolder:GetChildren()) do
10    found[aval.Name] = false
11end
12 
13game:GetService("RunService").RenderStepped:Connect(function()
14    if player.Character then
15        for findex, fval in pairs(afolder:GetChildren()) do
View all 38 lines...
Ad

Answer this question