Okay so, I made this script which generates a polygon from different nodes, this method is called ear clipping and it works perfectly, the first loop will create a triangle in the first three nodes while the second loop will create a triangle in the subsequent nodes.
Now, this is just a preview of the polygon I want to generate and it is a local script in a text button. What I want to achieve is whenever I press āXā or backspace on my keyboard, the preview, and its nodes will be destroyed and the loop will stop, until I press the button again and secondly, when I right-click on my mouse, a remote event gets fired and the nodes get deleted, while the preview becomes a real model and visible to everyone.
local mouse = player:GetMouse()
local UIS = game:GetService('UserInputService')
local runService = game:GetService('RunService')
local remote = game.ReplicatedStorage.generateFloor
local nodes = {}
local a
local b
local c
local toggle = false
function Round(Num, To)
return math.floor(Num / To + .5) * To
end
local node1 = Instance.new("Part", workspace)
node1.Name = "FloorNode1"
node1.Anchored = true
node1.Transparency = 0.5
node1.Material = Enum.Material.SmoothPlastic
node1.Shape = Enum.PartType.Ball
node1.Size = Vector3.new(1, 1, 1)
node1.CanCollide = false
node1.Color = Color3.fromRGB(202, 202, 202)
runService.RenderStepped:Connect(function()
if toggle then
local MouseP = mouse.Hit.Position
node1.CFrame = CFrame.new(Vector3.new(Round(mouse.Hit.p.X, 4), 10, Round(mouse.Hit.p.Z, 4)))*CFrame.Angles(0,0,math.rad(90))
end
end)
script.Parent.MouseButton1Click:Connect(function()
toggle = true
mouse.Button1Down:Connect(function()
local node2 = Instance.new('Part', workspace)
node2.Name = "SecondNode"
node2.Anchored = true
node2.Transparency = 0.2
node2.Material = Enum.Material.SmoothPlastic
node2.Shape = Enum.PartType.Ball
node2.Size = Vector3.new(1, 1, 1)
node2.CanCollide = true
node2.Color = Color3.fromRGB(129, 255, 137)
node2.CFrame = node1.CFrame
table.insert(nodes, node2)
if #nodes == 3 then
node2.Color = Color3.fromRGB(129, 255, 137)
a = nodes[1]
b = nodes[2]
c = nodes[3]
local ta
local tb
ta, tb = require(script.Triangle)(a.Position, b.Position, c.Position, workspace, ta, tb)
elseif #nodes > 3 then
node2.Color = Color3.fromRGB(129, 255, 137)
a = nodes[1]
b = nodes[#nodes - 1]
c = nodes[#nodes]
local ta
local tb
ta, tb = require(script.Triangle)(a.Position, b.Position, c.Position, workspace, ta, tb)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == "X" or input.KeyCode == "Backspace" then
toggle = false
-- Preview gets deleted and its nodes, loop stops
end
end)
mouse.Button2Down:Connect(function()
if #nodes == 3 or #nodes > 3 then
remote:FireServer() -- Nodes gets deleted and preview will appear to everyone in the server
end
end)
end
end)
end)
Iām using the traingle module, you can find it here
Any help? Thanks for reading.