For i,v in pairs() not working

hi, im having issues with a localscript where for some reason the for _,v loop isnt working, it doesnt even print

code:

local RegionTextGui = script:WaitForChild("RegionText")
local RS = game:GetService("ReplicatedStorage")
local Skies = RS:WaitForChild("Skies")

local TweenService = game:GetService("TweenService")

local plr = game:GetService("Players").LocalPlayer
local PlayingSong = game:GetService("SoundService").PlayingSong

local ZonePlus = require(game:GetService("ReplicatedStorage").Modules.Zone)
local ZoneController = require(game:GetService("ReplicatedStorage").Modules.Zone.ZoneController)

local Zones = workspace.SoundsRegions

for _, zone_part in pairs(Zones:GetChildren()) do
	local trigger = ZonePlus.new(zone_part)
	trigger.playerEntered:Connect(function(player)
		if player == plr then
			TweenService:Create(PlayingSong, TweenInfo.new(.75), {Volume = 0}):Play()
			wait(.75)
			PlayingSong.SoundId = zone_part.Sound.SoundId
			local nextVolume = zone_part.Sound.Volume
			TweenService:Create(PlayingSong, TweenInfo.new(.5), {Volume = nextVolume}):Play()
		end
	end)
end

ZoneController.setGroup("SoundRegions", {
	onlyEnterOnceExitedAll = true;
})
8 Likes

The issue is either WaitForChild or the table is empty try printing Zones:GetChildren() before the loop and send the output

2 Likes

image
what do i do?

2 Likes

You can remove the print now are you sure that the Zones is at least an ancestor of an In stance

2 Likes

There’s nothing in your SoundsRegions folder, so there’s nothing to loop through. Maybe you misplaced your zone parts.

1 Like

image

1 Like

ok but what can i do to fix the for loop, the thing is i have another game that does the exact same and it works

1 Like

Make sure these parts are anchored

1 Like

They are indeed anchored and they do show up in the folder in game

If you put a hashtag before a table, it’ll become a number, telling you how many variables are inside that table:

local t = {1,2,3}
local p = {5,72,2,5,751,6}

print(#t)
--3

print(#p)
--6

You can use this to check if there’s anything inside the table, before calling a for parameter, to prevent errors like these.

For your example, you can do this:

if #Zones:GetChildren() > 0 then
    for index,zone in pairs(Zones:GetChildren()) do
        print(index,zone)
    end
else
    print("there are no zones")
end

i got 0, i dont think thats what its supposed to say

Try replacing the 13 line (local Zones = workspace.SoundsRegions) with local Zones = workspace:WaitForChild("SoundsRegions")

0 means there are no variables in your table, so it’s empty.

local t = {}

print(#t)
--0

i did, doesnt work

roblox limitlimi

We know there is no zones to the local player because the table they printed was empty adding an if like this would just not run the code since this Isn’t a loop and Is a start of a script

1 Like

I’m not 100% sure with what your problem exactly is, but the best reason this could be happening is if you try to index a table with no variables, it will break the script.

local t = {}

if #t > 0 then
    print("table is not empty")
else
    print("table is empty")
end

The issue here is that the client does not have a part not indexing the pairs loop doesn’t start because the table is empty even though there are Instances in the zones Instance so I am trying to figure out how to replicate it or why is it deleting / not synching with the server right Roblox made StramingEnabled enabled as default I am trying to test that

The issue is not in the for loop; it’s in local Zones = workspace.SoundsRegions. You’ve shown that a folder with that name exists, but there’s nothing in it. Can you check what’s inside while running the script? Possibly something is deleting the parts inside.

For that, you can just replace it with:

local Zones = workspace.SoundsRegions:GetChildren()
--"Zones" is now a table of children.

if #Zones > 0 then
    print("not empty")
else
    print("empty")
end

even that doesnt work btw
roblox limit