Detecting a Value that is already in a list?
Asked by
6 years ago Edited 6 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.
05 | local function createWall(last, currentPos) |
07 | local wall = Instance.new( "Part" ) |
09 | wall.Size = Vector 3. new((last-currentPos).Magnitude, 9 , 1 ) |
11 | local midX = (last.X + currentPos.X)/ 2 |
12 | local midY = (last.Y + currentPos.Y)/ 2 - wallOffset |
13 | local midZ = (last.Z + currentPos.Z)/ 2 |
14 | local orientation = (last - currentPos).Unit:Cross(Vector 3. new( 0 , 1 , 0 )) |
15 | local CFramePos = Vector 3. new(midX, midY, midZ) |
17 | wall.CFrame = CFrame.new(CFramePos, CFramePos + orientation) |
19 | print ( "Last:" ,last, "Current:" ,currentPos) |
21 | wall.CanCollide = false |
23 | for index = 1 , #list 01 do |
24 | local value = list 01 [ index ] |
25 | if value = = wall.Position then |
27 | print ( "wall destroyed" ) |
30 | table.insert(list 01 , wall.Position) |
31 | print ( "Wasn't Destroyed, added to table" ) |
34 | print ( "List number: " , table.getn(list 01 )) |
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.