Issue with procedural generation script

I was unsure what to put in the title since I couldn’t pinpoint what the issue is about exactly. I tried to search the errors on the forum but the solutions provided didn’t seem appropriate for my case. I have a script that procedurally generates the map (nodes > paths > rooms)

I would like to note that the issue only happens half of the time. The other half, the entire code runs as intended.

i used the print() function for troubleshooting and the values it outputted seemed normal; -33, 8, -132...south->0, 0, -66 contrary to the error on workspace:GetPartBoundsInRadius(pathInst.Position + (directions[tags[1]] / 2), 8) saying “attempt to perform arithmetic (div) on int and nil” or something like that

What i did after that was try and call the function with pcall to see what it would do, and it outputted “attempt to call a table value” this time

local directions = {
	north = Vector3.new(0, 0,  branchLength),
	south = Vector3.new(0, 0, -branchLength),
	east  = Vector3.new(-branchLength, 0, 0),
	west  = Vector3.new(branchLength, 0, 0)
}

local function createRooms()
	for _, pathInst in pairs(workspace.Template.Paths:GetChildren()) do
		if pathInst.Name ~= "Origin" then
			local tags = collectionService:GetTags(pathInst)
			
			if tags then
				task.wait(0.01)
				local success, result = pcall(workspace:GetPartBoundsInRadius(pathInst.Position + (directions[tags[1]] / 2), 8))
				
				if success then
					if #result == 0 then
						local newRoom = Instance.new("Part")
						newRoom.Anchored = true
						newRoom.Parent = workspace.Template.Rooms

						newRoom.Position = pathInst.Position + (directions[tags[1]] / 2)
						newRoom.Size = Vector3.new(10, 10, 10)

						totalParts += 1

						collectionService:AddTag(newRoom, opp[tags[1]])
						pathInst.Color = Color3.new(0, 1, 0)
					else
						collectionService:RemoveTag(pathInst, tags[1])
						pathInst.Color = Color3.new(1, 0, 0)
					end
				else
					print(result, tostring(pathInst.Position).."..."..tostring(tags[1]).."->"..tostring(directions[tags[1]])) -- i used this for troubleshooting
					pathInst.Color = Color3.new(1, 0, 0)
				end
			else
				pathInst.Color = Color3.new(1, 0, 0)
			end
		end
	end
end

i’m particularly confused since the values don’t seem to show anything unusual but it still throws the errors I said.

that’s because you’re passing a table not a function

local success, result = pcall(workspace:GetPartBoundsInRadius(pathInst.Position + (directions[tags[1]] / 2), 8))
local function createRooms()
  for _, pathInst in workspace:WaitForChild("Template"):WaitForChild("Paths"):GetChildren() do
    if pathInst.Name == "Origin" then continue end

    local tags = CollectionService:GetTags(pathInst)

    -- :GetTags() returns an empty table {} if no tags exist, not nil.
    -- so check if the count is 0.
    if #tags == 0 then
      pathInst.Color = Color3.new(1, 0, 0)
      continue
    end

    task.wait(0.01)

    local success, result = pcall(function()
      return workspace:GetPartBoundsInRadius(pathInst.Position + (directions[tags[1]] / 2), 8)
    end)

    if not success then
      warn("Error calculation bounds:", result)
      print(tostring(pathInst.Position).. "..." ..tostring(tags[1]).. "->" ..tostring(directions[tags[1]]))
      pathInst.Color = Color3.new(1, 0, 0)
      continue
    end

    if #result ~= 0 then
      CollectionService:RemoveTag(pathInst, tags[1])
      pathInst.Color = Color3.new(1, 0, 0)
      continue
    end

    local newRoom = Instance.new("Part")
    newRoom.Anchored = true
    newRoom.Parent = workspace.Template.Rooms

    newRoom.Position = pathInst.Position + (directions[tags[1]] / 2)
    newRoom.Size = Vector3.new(10, 10, 10)

    totalParts += 1

    CollectionService:AddTag(newRoom, opp[tags[1]])
    pathInst.Color = Color3.new(0, 1, 0)
  end
end

all i did was reduce indentation, so don’t expect this to work

2 Likes

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