Attempt to index nil with "Boundaries"

  1. What do you want to achieve? im trying to make a script that makes the parts inside boundaries folder change transparency

  2. What is the issue? doesnt work and i get a error saying Attempt to index nil with "Boundaries" when placeholdertower

  3. What solutions have you tried so far? i looked in dev forum but i guess nothing helped me, i dont know what to do

(LOCAL SCRIPT IN STARTERGUI AND SCREEN GUI)

local map = workspace.Map:FindFirstChildOfClass("Folder")

local function mapBoundaries(transparency)
	for _, part in ipairs(map.Boundaries:GetDescendants()) do
		if part:IsA("BasePart") then
			part.Transparency = transparency
		end
	end
end

local function RemovePlaceholderTower()
	
	if towerToSpawn then
		towerToSpawn:Destroy()
		towerToSpawn = nil
		rotation = 0
		mapBoundaries(1)
		gui.Controls.Visible = false
	end
end

local function AddPlaceholderTower(name)
	local towerExists = towers:FindFirstChild(name)
	if towerExists then

		RemovePlaceholderTower()
		towerToSpawn = towerExists:Clone()
		towerToSpawn.Parent = workspace.Towers

		local Outline = ReplicatedStorage.Particle.Highlights.Tower.TowerPlaceHolderHighlight:Clone()
		Outline.Parent = towerToSpawn

		CreateRangeCirclePlaceHolder(towerToSpawn, true)
		for i, object in ipairs(towerToSpawn:GetDescendants()) do
			if object:IsA("BasePart") then
				object.CollisionGroup = "Tower"
			end
		end
		mapBoundaries(0.5)
		gui.Controls.Visible = true
	end
end

local function SpawnNewTower()
	if canPlace then
		local placedTower = spawnTowerFunction:InvokeServer(towerToSpawn.Name, towerToSpawn.PrimaryPart.CFrame)
		if placedTower then
			placedTowers += 1
			gui.Sounds.Place:Play()
			RemovePlaceholderTower()
			toggleTowerInfo()
			mapBoundaries(1)
			if placedTower.Config.Owner.Value == Players.LocalPlayer.Name then
				local boxHeight = (placedTower.PrimaryPart.Size.Y) - placedTower.Humanoid.HipHeight
				local boxOffset = CFrame.new(0, -boxHeight, 0)
				local boxPrefab = ReplicatedStorage:WaitForChild("Box")
				local box = boxPrefab:Clone()
				box.Size = Vector3.new(placedTower.PrimaryPart.Size.Y * 1.75, placedTower.PrimaryPart.Size.Y * 1.75, placedTower.PrimaryPart.Size.Y * 1.75)
				box.Parent = placedTower
				box.CFrame = placedTower.PrimaryPart.CFrame * boxOffset
			end
		end
	else
		Error("You can't place that there!")
	end
end

the rest of the script is where i set the transparency
Captura de pantalla 2024-07-25 162351

Im going to assume the error is on this line.

When you get attempt to index nil on a object it means the game cannot find the object. There are different causes for this, the most common would be that you are trying to access the object before the game actually loads so a simple fix would be to add a wait for child

local Boundaries = map:WaitForChild("Boundaries")

local function mapBoundaries(transparency)
	for _, part in pairs(Boundaries:GetChildren()) do
		if part:IsA("BasePart") then
			part.Transparency = transparency
		end
	end
end
1 Like

i still have the same issue, i didnt put the local variable of boundaries with waitforchild because it bugged my game since there is a voting map system (map didnt spawn yet)

the error is saying that the variable “map” is nil, try printing map after the first line of code and see what it outputs

Boundaries is located under Map → Grassland, so you would need to change for _, part in ipairs(map.Boundaries:GetDescendants()) do to for _, part in ipairs(map.Grassland.Boundaries:GetDescendants()) do

This is really interesting, if it says Attempt to index nil with "Boundaries" then it must mean that map is nil in the mapBoundaries function. Assuming that map was not nil when it was set (meaning there was some folder inside the Map folder), map (first folder inside of Map folder) must be somehow removed before the mapBoundaries function runs.

What I think may be going on is this:
The Placeholder Tower is inside the Map folder at the beginning and therefore the map variable gets set to Placeholder Tower. Then the map spawning function spawns the map. Deletes the Placeholder Tower and puts a new folder inside of the Map folder.
Because the Placeholder Tower was deleted, it automatically sets map to nil because what the map variable was referring too no longer exists.

I think finding the Map inside of the mapBoundaries function should solve this problem.

local function mapBoundaries(transparency)
	for _, part in ipairs(game.Workspace.Map:FindFirstChildOfClass("Folder").Boundaries:GetDescendants()) do
		if part:IsA("BasePart") then
			part.Transparency = transparency
		end
	end
end

Please let me know if my analysis is wrong or if that does not work. I would be happy to help in any way.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.