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

Random "attempt to index nil with 'GetDescendents' errors? [SOLVED]

Asked by 1 year ago
Edited 1 year ago

Title says it all, I am trying to setup sound regions and make it so that the sounds fade in or out realistically, however this has proven to be a real pain.

Basically, sometimes when I launch the game the script works as intended, other times though, it'll throw this error and it won't work.

Here is the code in question.

-- [ TODO: FIX LAG ] --
-- [ stops and starts regional sounds with fading in and out ] --
-- [ DO NOT MAKE A SOUND WITH ANY VOLUME OTHER THAN 0, THIS SCRIPT WILL MANAGE THAT ]

-- get sound service
local sounds = game:GetService("SoundService")

-- get sound regions (area where sounds are played)
local sound_regions = workspace.SpecialBrushes:WaitForChild("SoundRegions")

-- detect if player is in zone
local found = false
local player = game.Players.LocalPlayer

while wait() do
    -- do this for each individual region
    for i, v in pairs(sound_regions:GetChildren()) do
        found = false

        -- create region from 2 points
        local region = Region3.new(v.Position - (v.Size/2), v.Position + (v.Size/2))

        -- find parts of user that are in region
        local parts = workspace:FindPartsInRegion3WithWhiteList(region, player.Character:GetDescendants())

        -- go through everything in parts table
        for _, part in pairs(parts) do
            if part:FindFirstAncestor(player.Name) then
                found = true
                break
            else
                found = false
            end
        end

        -- play audio, target audio is 0.3
        if found == true then
            if sounds.Ambience[v.Name].isPlaying == false then
                sounds.Ambience[v.Name]:Play()

                -- fade out
                for count = 0, 300, 1 do
                    sounds.Ambience[v.Name].Volume += 0.001
                    wait(0.0001)
                end
                break
            end
        else
            if sounds.Ambience[v.Name].isPlaying == true then
                -- fade in
                for count = 0, 300, 1 do
                    sounds.Ambience[v.Name].Volume -= 0.001
                    wait(0.0001)
                end

                sounds.Ambience[v.Name]:Stop()
            end
        end
    end
end

Help would be much appreciated, and I apologize in advance for any newbie errors I may have made as it's been a while since I scripted on ROBLOX.

EDIT: ANSWER BELOW

0
where is the localscript parented? T3_MasterGamer 2189 — 1y
0
@T3_MasterGamer the script is parented in StarterPlayer.StarterPlayerScripts darkstar2121a 13 — 1y

1 answer

Log in to vote
0
Answered by 1 year ago

Thanks to T3_MasterGamer I found out that no, you should not put something like this in the StarterPlayerScripts, and that a more appropriate place to put it would be in StarterGui or StarterPack, I myself chose StarterPack as I feel like that fits it more, but you could also choose StarterGui.

0
I didn't even say anything abt "put it in StarterGui or StarterPack" and it should also work in StarterPlayerScripts the same as with StarterPack and StarterGui :skull: T3_MasterGamer 2189 — 1y
0
But it doesn't matter anymore if you solved it. T3_MasterGamer 2189 — 1y
0
If that's the case put either [SOLVED] or (answered) in your title T3_MasterGamer 2189 — 1y
Ad

Answer this question