Still have questions? Join our Discord server and get real time help.
Log in to vote

Detecting a Value that is already in a list?

Asked by 5 months ago
Edited 5 months ago

I have been struggling with preventing duplicate walls.

The function below takes two points and makes a wall between them. It then added that walls position to list01

The for loop in this function is what I've recently added and its failed at getting the desired result.

local wallOffset --Offset for midpoint Y axis
local list01 =  {} -- a table for holding each wall that's been placed

--Function for creating walls
local function createWall(last, currentPos)
    --Create a wall and size it between current and last pillar
    local wall ="Part")
    wall.Name = "Wall"
    wall.Size =,9,1)
    --(X,Y,Z) of the midpoint of the wall
    local midX = (last.X + currentPos.X)/2
    local midY = (last.Y + currentPos.Y)/2 - wallOffset
    local midZ = (last.Z + currentPos.Z)/2
    local orientation = (last - currentPos).Unit:Cross(,1,0))
    local CFramePos =, midY, midZ)
    --Move wall to Midpoint
    wall.CFrame =, CFramePos + orientation)
    --print out the last pillars POS and the current pillars POS
    print("Last:",last, "Current:",currentPos)
    wall.Anchored = true
    wall.CanCollide = false --Change soon, add to teams disabled collision group
    --Put walls POS in list or destroy wall if already in list.
    for index = 1, #list01 do
        local value = list01[index]
        if value == wall.Position then
            print("wall destroyed")
            table.insert(list01, wall.Position)
            print("Wasn't Destroyed, added to table")
    print("List number: ", table.getn(list01))


I can see that I need to change the for loop(or possibly it's the wrong approach), right now if the first value isn't the same it will add the wall POS to the list regardless if the wall does exist in the list at another index. But if I remove the else statement then I'm not sure where to 'table.insert' when it is a new wall.

That's where I'm at and any help is always appreciated.

why are you making the wall before you check if the position is ok? kingdom5 5316 — 5mo
@Kingdom5 Where should I make this check? GoodCallMrOlsen 55 — 5mo
When I check the position of a wall about to be created against the list of walls created, wall.Position comes up (0,0,0) unless it's after parenting the new wall to the workspace. Which resulted in the for loop being where it is? I am open to another route as this hasn't worked GoodCallMrOlsen 55 — 5mo

Answer this question