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:
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
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)
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?
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.
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.