Error - attempt to iterate over a Instance value

I’ve been trying to script whether a barrier (forcefield) is active or not for each player that enters a game. The script tries to cycle through each forcefield and check against the players saved forcefield data (I really can’t think of another way). All I want it to do is compare the BoolValue.Name against the Forcefield.Name and if true then print(). Later on I can compare the Values of the 2 variables and replace the forcefields BoolValue.Value with the BoolValue saved by the player (if there’s a better way please tell me).

I’ve been getting countless problems as I go through it and change things and I guess I’m not understanding the issue. Here is the script and the error.

---- Testing forcefields dividing each zone (add to player data script when working) ----

game.Players.PlayerAdded:Connect(function(player)
	
	local forcefields = Instance.new("Folder", player)
	forcefields.Name = "Forcefields"
	
	local forcefield1 = Instance.new("BoolValue", forcefields)
	forcefield1.Name = "Forcefield1"
	forcefield1.Value = true
	
	local forcefield2 = Instance.new("BoolValue", forcefields)
	forcefield2.Name = "Forcefield2"
	forcefield2.Value = true
	
	local forcefield3 = Instance.new("BoolValue", forcefields)
	forcefield3.Name = "Forcefield3"
	forcefield3.Value = true
	
	local forcefield4 = Instance.new("BoolValue", forcefields)
	forcefield4.Name = "Forcefield4"
	forcefield4.Value = true
	
	local forcefield5 = Instance.new("BoolValue", forcefields)
	forcefield5.Name = "Forcefield5"
	forcefield5.Value = true
	
	---- Find the state of each forcefield in the player and game (separating the zones) ----
	if player and forcefields then
		--print("Player and forcefields exists")
		local forcefieldsInGame = game.Workspace.GameAssets.Forcefields:GetChildren()
			for _, forceState in forcefieldsInGame do
				for _, state in player:FindFirstChild("Forcefields"):FindFirstChildWhichIsA("BoolValue"):GetChildren() do -- Line 33
					if forceState.Name == state.Name then
						print("Both names are the same") --checking for future value changes (forcefield on/off for each player)
				end
			end	
		end
	end
end)

--[[
TODO:
Have a saved state in the player data for each forcefield. If bought/notBought then
allow forcefield to be the same state

rebirth would then force the players forcefield.(boolean) to true

What is the mission for the forcefields?
firstly we need to check whether a new player has entered and make all the 
forcefields CanCollide = true
Transparency = 0

if not new player then check whether the players forcefields are
on (forcefields CanCollide = true, Transparency = 0)
or off (forcefields CanCollide = false, Transparency = 1)

and then make the forcefields that way.

]]

1 Like

Did you forget GetChildren? Also make sure to point out the line when your can, I can’t see line numbers and it’s not easy to count 33 lines to find the error.

1 Like

Idk how to change the color of the line so I added a --Line 33 comment to the script. Also I get new errors when GetChildren() added in. I guess it’s something to do with the For loop but after 4 hours I’m no closer to working it out.

Adding on to what @JarodOfOrbiter said, make sure to put the array of children inside an ipairs().

And you should do the same for forceFieldsInGame.

2 Likes

Change this line to:

for _, state in player:FindFirstChild("Forcefields"):FindFirstChildWhichIsA("BoolValue"):GetChildren() do

That’s actually not necessary anymore, Roblox added the functionally so for loops can loop over tables without pairs or ipairs.

2 Likes

Oh, wow. I’m starting to feel old. I did not know that.

1 Like

And that’s a good thing, luau syntax is becoming more clean and usable overtime!

1 Like

Well the error is gone but it prints nothing now. This probably means the names are different somehow so I’ll check.

image

The names should be the same, what’s missing!

This could happen because the table has no values inside, try printing this and see if it says anything:

#player:FindFirstChild("Forcefields"):FindFirstChildWhichIsA("BoolValue"):GetChildren()

This will return 0 if you use strings as the value name.

Another possibility is this line returning false (Condition not met):

if forceState.Name == state.Name then

At the top of the loop, try printing these aswell:

print(_, state)

It’s doesn’t like a lot of the changes with #player:Find…,
error ServerScriptService.ForcefieldScript:33: attempt to iterate over a number value

(_, state) red line tells me to add a value to the placeholder.

I tried to print just the state.Name and the forceState.Name but I got nothing so i guess nothing is returned. I wonder if making 2 functions and comparing the values that way would work. I’ve never used twin for loops before.

1 Like