I have this script that automates the assignment of little hexagons (territory) to stars in my game. I need to get the position of an object in part of my script. For some reason, folders are passing through two condition checks in my script, causing errors as folders do not have positions.
I can’t find the reason why, any help is nice
local function findTerritory(star)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {game.Workspace.Baseplate, game.Workspace.Backdrop}
local raycastResult = workspace:Raycast(star.Position, Vector3.new(0, -1, 0), raycastParams)
if raycastResult.Instance then
return raycastResult.Instance
else
print("Unable to find territory")
return nil
end
end
local function findOtherStarsWithinTerritory(territory)
local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Exclude
overlapParams.CollisionGroup = "Default"
local touchingObjects = workspace:GetPartBoundsInBox(CFrame.new(territory.Position.X, 0.85, territory.Position.Z), Vector3.new(14.245, 1, 15.405), overlapParams)
local otherStarsWithinTerritory = {}
if #touchingObjects > 0 then
print(touchingObjects)
for _, object in pairs(touchingObjects) do
if object:IsA("BasePart") and object.Name == "StarClickDetector" then --first check
local otherStarsTerritory = findTerritory(object.Parent)
if otherStarsTerritory == territory then
table.insert(otherStarsWithinTerritory, object.Parent)
end
elseif object.Parent:IsA("Folder") then --second check
print(object.Name)
local otherStarsTerritory = findTerritory(object)
if otherStarsTerritory == territory then
table.insert(otherStarsWithinTerritory, object)
end
end
end
else
print("Did not find stars within " .. territory.Name)
return nil
end
return otherStarsWithinTerritory
end
local function assignCapital(territory)
local capitalValue = territory:FindFirstChild("TerritoryCapital")
local possibleCapitals = findOtherStarsWithinTerritory(territory)
local closestCapital = nil
local closestDistance = math.huge
if possibleCapitals and #possibleCapitals > 0 then
for _, capital in pairs(possibleCapitals) do
local distance = (capital.Position - territory.Position).Magnitude
if distance < closestDistance then
closestDistance = distance
closestCapital = capital
end
end
capitalValue.Value = closestCapital.Name
else
local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Exclude
overlapParams.CollisionGroup = "Default"
local result = workspace:GetPartBoundsInBox(CFrame.new(territory.Position.X, 0.5, territory.Position.Z), Vector3.new(30, 0.5, 30), overlapParams)
if #result > 0 then
local nearbyStars = {}
for _, object in pairs(result) do
if string.find(object.Name, "Star") then
table.insert(nearbyStars, object.Parent.Parent)
end
end
for _, star in pairs(nearbyStars) do
local distance = (star.Position - territory.Position).Magnitude
if distance < closestDistance then
closestDistance = distance
closestCapital = star
end
end
if closestCapital then
local closestTerritoryWithCapital = findTerritory(closestCapital)
--capitalValue.Value = closestTerritoryWithCapital:FindFirstChild("TerritoryCapital").Value
if closestTerritoryWithCapital then
local territoryCapital = closestTerritoryWithCapital:FindFirstChild("TerritoryCapital")
if territoryCapital then
capitalValue.Value = territoryCapital.Value
else
warn("No 'TerritoryCapital' found in " .. closestTerritoryWithCapital.Name)
end
else
warn("findTerritory returned nil for " .. closestCapital.Name)
end
else
warn("No closest capital found.")
end
else
warn("No objects found in GetPartBoundsInBox for territory " .. territory.Name)
end
end
end
for _, model in pairs(game.Workspace.Territory:GetChildren()) do
for _, territory in pairs(model:GetChildren()) do
if string.find(territory.Name, "Territory") then
assignCapital(territory)
task.wait(0.05)
end
end
end
this function in specific:
local function findOtherStarsWithinTerritory(territory)
local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Exclude
overlapParams.CollisionGroup = "Default"
local touchingObjects = workspace:GetPartBoundsInBox(CFrame.new(territory.Position.X, 0.85, territory.Position.Z), Vector3.new(14.245, 1, 15.405), overlapParams)
local otherStarsWithinTerritory = {}
if #touchingObjects > 0 then
print(touchingObjects)
for _, object in pairs(touchingObjects) do
if object:IsA("BasePart") and object.Name == "StarClickDetector" then --first check
local otherStarsTerritory = findTerritory(object.Parent)
if otherStarsTerritory == territory then
table.insert(otherStarsWithinTerritory, object.Parent)
end
elseif object.Parent:IsA("Folder") then --second check
print(object.Name)
local otherStarsTerritory = findTerritory(object)
if otherStarsTerritory == territory then
table.insert(otherStarsWithinTerritory, object)
end
warn("Skipping object with no Position property: " .. object.Parent.Name)
end
end
else
print("Did not find stars within " .. territory.Name)
return nil
end
return otherStarsWithinTerritory
end
when adding a warn to tell me what objects are being passed, folders are shown before receiving an error
I cannot find why this is happening, and it is breaking my script. I can’t seem to find any further fixes on this