Adding features to a script

  1. What do you want to achieve? Keep it simple and clear!
    I want to add two features to my polygon placement system. Which are:
  • Whenever the player presses “X”, the preview and its nodes gets deleted.
  • When the player left click, the preview I created becomes visible to everyone and the nodes gets destroyed. And you can then create another polygon.
  1. What is the issue? I tried removing nodes from the tables and it didn’t work.

  2. What solutions have you tried so far? I searched some videos on youtube and all of them didn’t have a solution for my problem.

Here’s my script, it is located in a textbutton (local script).

local player = game.Players.LocalPlayer
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 == 4 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 also using the traingle module, any help? Thanks.

1 Like

Made a folder named “clientPreviewNodes” to hold the nodes and this should work:

local player = game.Players.LocalPlayer
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.clientPreviewNodes)
		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.clientPreviewNodes, 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.clientPreviewNodes, 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
					workspace.clientPreviewNodes:ClearAllChildren()
					nodes = {}
				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
					toggle = false
					nodes = {}
					workspace.clientPreviewNodes:ClearAllChildren()
				end
			end)
		end
	end)
end)

Works, but there is a problem:
robloxapp-20220408-1109265.wmv (1.5 MB)

Whenever I click X the triangle gets destroyed, but one of the nodes is still in the folder. Which causes the system to break, and create the polygon with that node.

local player = game.Players.LocalPlayer
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

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()
        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)

	toggle = true
	mouse.Button1Down:Connect(function()
		local node2 = Instance.new('Part', workspace.clientPreviewNodes)
		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.clientPreviewNodes, 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.clientPreviewNodes, 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
					workspace.clientPreviewNodes:ClearAllChildren()
					nodes = {}
              node1:Destroy()
				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
					toggle = false
					nodes = {}
					workspace.clientPreviewNodes:ClearAllChildren()
              node1:Destroy()
				end
			end)
		end
	end)
end)

does this work?

Let me try it. Sorry for the delay to respond.

1 Like

Same problem. This time, I can’t move the node too.

1 Like
local player = game.Players.LocalPlayer
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
	node1.Transparency = 0.5
	mouse.Button1Down:Connect(function()
		local node2 = Instance.new('Part', workspace.clientPreviewNodes)
		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.clientPreviewNodes, 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.clientPreviewNodes, ta, tb)

			UIS.InputBegan:Connect(function(input)
				if input.KeyCode == Enum.KeyCode.X or input.KeyCode == Enum.KeyCode.Backspace then
					toggle = false
					-- Preview gets deleted and its nodes, loop stops
					workspace.clientPreviewNodes:ClearAllChildren()
					nodes = {}
					node1.Transparency = 1
				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
					toggle = false
					nodes = {}
					workspace.clientPreviewNodes:ClearAllChildren()
					node1.Transparency = 1
				end
			end)
		end
	end)
end)

this works for me

1 Like