Delete Placement System

If you want to use a regular script, you can. However, ModuleScripts allow for cleaner code, so you should definitely try to be familiar with them.

As for that error, try doing require(insertPathToModuleScriptHere), rather than using require with the assetid.

I’ll use a module script but I keep getting this error:
image

Module script code:

local Input = nil


local Obj = {
	"FrontRoof",
	"Walls"
}

local function OnSelect()
	Input = "Remove"
	if game.ReplicatedStorage.Input == "Built" then
		local RemoveObjects = {
			workspace.FrontRoof,
			workspace.Walls
		}
		local mouse = game.Players.LocalPlayer:GetMouse()
		local target = mouse.Target
		if target == RemoveObjects[1] or RemoveObjects[2] then
			local Box = Instance.new("SelectionBox")
			Box.Adornee = RemoveObjects[1] or RemoveObjects[2]
			Box.Parent = RemoveObjects[1] or RemoveObjects[2]
			
			
			
			local function DestroyObject()
				mouse.Button1Down:Connect(function()
					if mouse.Target:IsA("UnionOperation") or mouse.Target:IsA("Model") or mouse.Target:IsA("Part") and target.Name == Obj[1] or Obj[2] then
						target:Destroy()
					end
			end)
				end

			
				end			
			end		
		end

ModuleScripts need to follow the same format:

local ModuleName = {}

local function Module.OnSelect()
-- code goes here
end

local function Module.DestroyObject()
-- code goes here
end

return ModuleName

You then call functions in the Module by doing

local module = require(pathToModuleScript)

module.OnSelect()
module.DestroyObject()

You can read more about ModuleScript’s here.

I’m quite fascinated with module scripts. This error pops up on the script(NOT THE MODULE SCRIPT!)
image

The code:

local module = require(game.ReplicatedStorage.Destroy)

module.OnSelect()

module.DestroyObject()

Try doing require(game:GetService("ReplicatedStorage"):WaitForChild("Destroy"))

Similar error on line 2:
image

I take it that error occurs when you attempt to call module.OnSelect(). Can you post your current ModuleScript?

local ModuleName = {}


local Input = nil


local Obj = {
	"FrontRoof",
	"Walls"
}

local function OnSelect()
	Input = "Remove"
	if game.ReplicatedStorage.Input == "Built" then
		local RemoveObjects = {
			workspace.FrontRoof,
			workspace.Walls
		}
		local mouse = game.Players.LocalPlayer:GetMouse()
		local target = mouse.Target
		if target == RemoveObjects[1] or RemoveObjects[2] then
			local Box = Instance.new("SelectionBox")
			Box.Adornee = RemoveObjects[1] or RemoveObjects[2]
			Box.Parent = RemoveObjects[1] or RemoveObjects[2]
			
			
			
			local function DestroyObject()
				mouse.Button1Down:Connect(function()
					if mouse.Target:IsA("UnionOperation") or mouse.Target:IsA("Model") or mouse.Target:IsA("Part") and target.Name == Obj[1] or Obj[2] then
						target:Destroy()
					end
					
			end)
				end

			
				end			
			end		
end

return ModuleName

You have to do function ModuleName.OnSelect(). If your functions are not appended to the table that is returned, they cannot be accessed by other scripts.

Oh… I hate the fact that ROBLOX studio keeps crashing.

image
Again this error pops up :man_facepalming:
Here’s the module script:
local ModuleName = {}

local Input = nil


local Obj = {
	"FrontRoof",
	"Walls"
}

function ModuleName.OnSelect()
	Input = "Remove"
	if game.ReplicatedStorage.Input == "Built" then
		local RemoveObjects = {
			workspace.FrontRoof,
			workspace.Walls
		}
		local mouse = game.Players.LocalPlayer:GetMouse()
		local target = mouse.Target
		if target == RemoveObjects[1] or RemoveObjects[2] then
			local Box = Instance.new("SelectionBox")
			Box.Adornee = RemoveObjects[1] or RemoveObjects[2]
			Box.Parent = RemoveObjects[1] or RemoveObjects[2]



			 function ModuleName.DestroyObject()
				mouse.Button1Down:Connect(function()
					if mouse.Target:IsA("UnionOperation") or mouse.Target:IsA("Model") or mouse.Target:IsA("Part") and target.Name == Obj[1] or Obj[2] then
						target:Destroy()
					end

				end)
			end


		end			
	end		
end

Here’s the script that calls the module script:
local module = require(game:GetService("ReplicatedStorage"):WaitForChild("Destroy"))

module.OnSelect()

module.DestroyObject()

If that’s the entire ModuleScript, then you need to add a return ModuleName at the very end.

Apologies I’ve forgot to implement that.

image
Error for the script that calls the module script.
Code:
local module = require(game:GetService(“ReplicatedStorage”):WaitForChild(“Destroy”))

module.OnSelect()

module.DestroyObject()

You put ModuleName.DestroyObject() within another function. Place it outside of ModuleName.OnSelect().

Same error, the exact same line…

I get the same error in the exact line. I’ve tried removing it to see but there was no traces of any errors or any performance of the code.

local ModuleName = {}

local Input = nil


local Obj = {
	"FrontRoof",
	"Walls"
}

function ModuleName.OnSelect()
	Input = "Remove"
	if game.ReplicatedStorage.Input == "Built" then
		local RemoveObjects = {
			workspace.FrontRoof,
			workspace.Walls
		}
		local mouse = game.Players.LocalPlayer:GetMouse()
		local target = mouse.Target
		if target == RemoveObjects[1] or RemoveObjects[2] then
			local Box = Instance.new("SelectionBox")
			Box.Adornee = RemoveObjects[1] or RemoveObjects[2]
			Box.Parent = RemoveObjects[1] or RemoveObjects[2]
		end			
	end		
end

function ModuleName.DestroyObject()
	local mouse = game.Players.LocalPlayer:GetMouse()
	
	mouse.Button1Down:Connect(function()
		if mouse.Target:IsA("UnionOperation") or mouse.Target:IsA("Model") or mouse.Target:IsA("Part") and mouse.Target.Name == Obj[1] or Obj[2] then
			mouse.Target:Destroy()
		end

	end)
end

return ModuleName
local test = require(game:GetService("ReplicatedStorage"):WaitForChild("ModuleScript"))

test.OnSelect()
test.DestroyObject()

Works for me.

GetMouse() property only works on local scripts. How do I reference the player’s mouse?