Local scripts not running?

Before you say anything. Yes my local script is in StarterGui and is not a descendant of Workspace or anything that isn’t about the client. I may be stupid, but for some reason it does not execute.

Localisation of my local script:
image

Here you can see at line 14 that I am trying to print the local player’s username, but for some reason, nothing appears in the console
image

Any help would be much appreciated, thanks!

3 Likes

Can we see the code leading up to that print statement?

4 Likes

make sure Archivable property of this local script is checked :white_check_mark:

nvm…

if it still doesn’t work, maybe it’s best to show the code, so it’s easier to spot errors

3 Likes

Sure, have a look!

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")

local player = Players.LocalPlayer
local camera = Workspace.CurrentCamera

local zoneFolder = Workspace:WaitForChild("Zonesounds")
local zones = zoneFolder:WaitForChild("ZonesoundsBlocks"):GetChildren()
local audios = zoneFolder:WaitForChild("ZonesoundsAudios"):GetChildren()

local activeSound = nil

print(player.Name)

local zoneAudioPairs = {
    {zone = zoneFolder.ZonesoundsBlocks.ShootingRangeSoundZone, audio = zoneFolder.ZonesoundsAudio.ShootingRangeZone},
    {zone = zoneFolder.ZonesoundsBlocks.TowerSoundZone, audio = zoneFolder.ZonesoundsAudio.TowerZone},
    {zone = zoneFolder.ZonesoundsBlocks.TowerSoundZone2, audio = zoneFolder.ZonesoundsAudio.TowerZone},
    {zone = zoneFolder.ZonesoundsBlocks.SecretSpotSoundZone, audio = zoneFolder.ZonesoundsAudio.SecretSpot},
    {zone = zoneFolder.ZonesoundsBlocks.ConstructZoneSoundZone, audio = zoneFolder.ZonesoundsAudio.ConstructZone},
    {zone = zoneFolder.ZonesoundsBlocks.ConstructOfficeSoundZone, audio = zoneFolder.ZonesoundsAudio.ConstructOffice}
}

print("Zones found: ", #zones)
print("Audios found: ", #audios)

for _, zone in ipairs(zones) do
    for _, audio in ipairs(audios) do
        if audio.Name == zone.Name then
            table.insert(zoneAudioPairs, {zone = zone, audio = audio})
            print("Paired zone: ", zone.Name, " with audio: ", audio.Name)
            break
        end
    end
end

for i, pair in ipairs(zoneAudioPairs) do
    print(string.format("Pair %d: Zone - %s, Audio - %s", i, pair.zone.Name, pair.audio.Name))
end

local function isCameraInZone(zone)
    local cameraCFrame = camera.CFrame
    local zonePosition, zoneSize = zone.Position, zone.Size
    local zoneMin = zonePosition - zoneSize / 2
    local zoneMax = zonePosition + zoneSize / 2

    local cameraPosition = cameraCFrame.Position

    return (cameraPosition.X >= zoneMin.X and cameraPosition.X <= zoneMax.X) and
        (cameraPosition.Y >= zoneMin.Y and cameraPosition.Y <= zoneMax.Y) and
        (cameraPosition.Z >= zoneMin.Z and cameraPosition.Z <= zoneMax.Z)
end

local function playZoneAudio(zone)
    if activeSound then
        activeSound:Stop()
    end

    for _, pair in ipairs(zoneAudioPairs) do
        if pair.zone == zone then
            pair.audio:Play()
            activeSound = pair.audio
            print("Playing audio: ", pair.audio.Name)
            break
        end
    end
end

RunService.RenderStepped:Connect(function()
    for _, pair in ipairs(zoneAudioPairs) do
        if isCameraInZone(pair.zone) then
            print("Camera is in zone: ", pair.zone.Name)
            playZoneAudio(pair.zone)
            break
        end
    end
end)
2 Likes

So potential causes are those calls to WaitForChild for the zones and audios hanging for a while. Are you able to confirm those calls are completing correctly?

2 Likes

How can I make sure to confirm the calls? I didnt see errors about my script in the console.

2 Likes

How long are you letting the script run? WaitForChild should automatically return after 5 seconds if it can’t find the child and no timeout argument was specified. Also throw some prints before those calls to check if the script is even reaching that point.

1 Like

Let me go check, i’ll be right back.

2 Likes


Here is the console logs, the only ones I get from the script.

Okay yea, that’s the issue. ZonesoundsAudios isn’t there or isn’t loading as you expect. It’s causing WaitForChild to return nil (to avoid hanging indefinitely) and you’re trying to call GetChildren on nil which crashes the script.

You’re half correct. If you do not specify a timeout, WaitForChild will just hang indefinitely, rather than returning nil. The warning is just to alert developers to any potential issues.

1 Like

Do you mind showing me the correct script so I can compare them, thanks?

Ah yea, you’re correct. I misread the 5 second warning message with it returning after 5 seconds

1 Like

You have the correct script, but you need to figure out why ZonesoundsAudios isn’t showing up like you’re expecting

ohhhhh… do you have any docs that maybe handy for me? Im not that great at programming tbh

Here’s the documentation for WaitForChild. Can you share a picture of what the Zonesounds tree/hierarchy looks like in the workspace?

Sure, I also added print debugs, here is what I get:

Im getting u the hierarchy, I’ll be right back!

image
Here it is. The folder is in the workspace and the audio all have the same children which is a reverb effect.

Wrong folder name. You added an extra s to your script calls.

2 Likes

ohhhh im hella stupid, lemme check.

1 Like