Problems with counting Objects in a folder

Hello, I have come across with an issue while attempting to change a value to the Amount of Enemies in a folder

This Error keeps appearing when I attempt this: Players.Altomegaflowey844.PlayerGui.Wave_Enemies.Counting:13: attempt to get length of a Instance value

Here is the code of the counter:

--// Variables
local Rep = game:GetService("ReplicatedStorage")
local WaveText = Rep.GamePlayFiles.WaveText.Value
local EnemiesValue = Rep.GamePlayFiles.Enemies
local WaveLabel = script.Parent.Wave
local EnemiesText = script.Parent.Enemies
local EnemiesFolder = workspace.Enemies

--// Counting Enemies
while true do
	wait()
	for i, enemies in pairs(EnemiesFolder:GetChildren()) do
		EnemiesValue.Value = #enemies
	end
end

Any help is appreciated!

1 Like

enemies isn’t a table, it’s a child of EnemiesFolder. You’re trying to use the # operator which returns the length of a table.

Instead of #enemies, you should do #EnemiesFolder:GetChildren() instead.


-- EXAMPLE

for i,v in ipairs(workspace:GetChildren()) do
	warn(#v) -- ERROR: Attempt to get length of a Instance value
end

warn(#workspace:GetChildren()) -- e.g. 4
1 Like