Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Detecting a Value that is already in a list?

Asked by 5 years ago
Edited 5 years 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 = Instance.new("Part")
    wall.Name = "Wall"
    wall.Size = Vector3.new((last-currentPos).Magnitude,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(Vector3.new(0,1,0))
    local CFramePos = Vector3.new(midX, midY, midZ)
    --Move wall to Midpoint
    wall.CFrame = CFrame.new(CFramePos, 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
            wall:Destroy()
            print("wall destroyed")
            break
        else
            table.insert(list01, wall.Position)
            print("Wasn't Destroyed, added to table")
        end
    end
    print("List number: ", table.getn(list01))

end

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.

1
why are you making the wall before you check if the position is ok? User#5423 17 — 5y
0
@Kingdom5 Where should I make this check? GoodCallMrOlsen 70 — 5y
0
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 70 — 5y

Answer this question