Need help with Polygon Triangulation

I’m trying to make a floor placement system similar to ‘Welcome to Bloxburg’ and ‘Restaurant Tycoon 2’ where the player would place down points on the ground and the system would construct a floor shaped from those points placed down by the player.

The problem I’m currently face right now is figuring out how to make the system work well with forming concave shapes.

Here is what I have made so far.


As you can see, it doesn’t do a good job whenever I try to form a concave shape.

here is the code for whenever the player presses the mouse button to place down a point on the ground.

local newnode = TempNode:Clone()
newnode.Parent = workspace
Index = Index + 1
print(Index)
newnode.Name = Index
table.insert(Nodes, newnode)
for _,v in Tri do
	v:Destroy()
end
Tri = {}
for i = 1, #Nodes - 1 do
	local startPos = Nodes[i].Position
	local endPos = Nodes[i + 1].Position
	local line = Instance.new("Part")
	line.Size = Vector3.new(0.2, 0.2, (endPos - startPos).Magnitude)
	line.CFrame = CFrame.new(startPos, endPos) * CFrame.new(0, 0, -line.Size.Z / 2)
	line.Anchored = true
	line.Parent = workspace
	table.insert(Lines,line)
end
if #Nodes > 2 then
	for i,v in Nodes do
		if (i % 2) == 0 then
			if #Nodes == i then
				if i >= 6 then
					local ta,tb
					local a = Nodes[5]
					local b = Nodes[3]
					local c = Nodes[1]
					ta, tb = Triangle(a.Position, b.Position, c.Position, workspace, ta, tb)
					table.insert(Tri,ta)
					table.insert(Tri,tb)
				end
				local ta,tb
				local a = Nodes[i-1]
				local b = Nodes[i]
				local c = Nodes[1]
				ta, tb = Triangle(a.Position, b.Position, c.Position, workspace, ta, tb)
				table.insert(Tri,ta)
				table.insert(Tri,tb)
			elseif #Nodes - 1 == i then
				local ta,tb
				local a = Nodes[i-1]
				local b = Nodes[i]
				local c = Nodes[1]
				ta, tb = Triangle(a.Position, b.Position, c.Position, workspace, ta, tb)
				table.insert(Tri,ta)
				table.insert(Tri,tb)
			else
				local ta,tb
				local a = Nodes[i-1]
				local b = Nodes[i]
				local c = Nodes[i+1]
				ta, tb = Triangle(a.Position, b.Position, c.Position, workspace, ta, tb)
				table.insert(Tri,ta)
				table.insert(Tri,tb)
			end
		else
			if #Nodes == i then
				local ta,tb
				local a = Nodes[i-1]
				local b = Nodes[i]
				local c = Nodes[1]
				ta, tb = Triangle(a.Position, b.Position, c.Position, workspace, ta, tb)
				table.insert(Tri,ta)
				table.insert(Tri,tb)
			elseif i >= 5 then
				local ta,tb
				local a = Nodes[i]
				local b = Nodes[i-2]
				local c = Nodes[1]
				ta, tb = Triangle(a.Position, b.Position, c.Position, workspace, ta, tb)
				table.insert(Tri,ta)
				table.insert(Tri,tb)
			end
		end
	end
	for i,v in Tri do
		v.Transparency = 0.8
	end
end

I would appreciate it if someone would help me with coming up with a better approach so that my system would work well with forming concave shapes.