How would I get if an object is accessible through values?

I have been trying to make a system so that a player can see if an area is accessible through other areas. These inbetween areas and the area at the end must be owned by the player.

But I’m not entirely sure how I would do this. I thought that using for loops would work but I have no idea how I would get for loops to go long enough to get to the area as it could have quite a few areas inbetween them. Each area has a StringValue in it that contains a table of the names of the areas has next to it.
The for loop solution I tried seemed like it would work at first but when I reached this point:

local selectableAreas = {}
local area = mouse.Target
for i,v in pairs(HTTP:JSONDecode(area.Connections.Value)) do
	for i2, v2 in pairs(game.Workspace.Map:FindFirstChild(v)) do
		if game.ReplicatedStorage.CurrentPlayer.Value == 1 then
			if v2.Owner.Value == "Red" then
				table.insert(selectableAreas, area)

I realised that it wouldn’t work as I would need a number of for loops that I have no idea of determining.
Any help on how I would create a system that would be able to loop through the connection values of the areas would be appreciated.

This function might help. I didn’t properly understand from the code you posted how you get the connected areas or the owner of an area, so I put parameters for functions that do those things. I haven’t tested this so I’m not sure if it works.

-- startArea is an area already owned by the player, target area is the area which you want to check if it's accessible
-- getConnectedAreas and getOwner should be functions
-- getConnectedArea should take an area and return a table of areas connected to it
-- (so if the areas are models, it should take a model and return a table of models)
-- getOwner should return any value that tells who owns the area
local function isAreaAccessible(startArea, targetArea, getConnectedAreas, getOwner)
    -- connectedAreas stores areas that are connected to startArea and owned by the player
    local connectedAreas = {}
    local alreadyChecked = {[startArea] = true}
    local owner = getOwner(startArea)

    for _, connectedArea in ipairs(getConnectedAreas(startArea)) do
        if connectedArea == targetArea then
            return true
        if getOwner(connectedArea) == owner then
            table.insert(connectedAreas, connectedArea)
            alreadyChecked[connectedArea] = true

    while #connectedAreas > 0 do
        local newConnectedAreas = {}
        for _, connectedArea in ipairs(connectedAreas) do
            for _, newConnectedArea in ipairs(getConnectedAreas(onnectedArea)) do
                if newConnectedArea == targetArea then
                    return true
                if not alreadyChecked[newConnectedArea] and getOwner(connectedArea) == owner then
                    table.insert(newConnectedAreas, newConnectedArea)
                    alreadyChecked[newConnectedArea] = true
        connectedAreas = newConectedAreas
    return false
1 Like