Delete Placement System

So I’ve been wanting to make a destroy placement destroy system for ages but I can’t seem to find the answer. I also want a box to appear when the mouse hovers around it.

Here’s what I’ve tried to add a box when the mouse hovers around it:

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()


game.Players.PlayerAdded:Connect(function(Player)
	Player.PlayerGui.MainHandler.BuildMain.StarterItemBody.Build.Delete.MouseButton1Down:Connect(function()
		if mouse.Target == script.Parent then
			local Box = Instance.new("SelectionBox")
			Box.Adornee = script.Parent
			Box.Parent = script.Parent
		end
	end)
end)

If you don’t understand I apologize.

Wait, does this mean that you will have to add this script into every object you want to delete?

        if mouse.Target == script.Parent then
			local Box = Instance.new("SelectionBox")
			Box.Adornee = script.Parent
			Box.Parent = script.Parent
		end

The first thing to tackle is the selection method. To select things properly, you want to use RunService.Stepped or an equivalent to keep checking for the mouse.Target. If the mouse.Target is different from, let’s say, previousTarget, it would delete the old box on the previousTarget and then creating the new box on the mouse.Target. previousTarget will be assigned to mouse.Target for every new target.

Or is it something else with mouse:GetPropetyChangedSignal("Target") and connect a function to it? It seems this method is more practical than RunService.Stepped.

I believe you are trying to create a tool that works similarly to the old classic delete tool.

2 Likes

Yes it does mean I’ll add this on every object someone builds. I’d be pleased if you had a different method!

Consider using a ModuleScript for this kind of system. Create functions within the script that are capable of handling the deletion and ui elements for the system you are working on.

Where shall I insert the module script?

I usually place my ModuleScripts in the ReplicatedStorage.

So I’ll reference all the parts that you can build in a table of some sort and call the require function on another script?

Something like this?

the module: 
    local Table = {
        game.ReplicatedStorage.BuildItems.FrontRoof
        game.ReplicatedStorage.BuildItems.Walls
        }
        return Table

the script:

require(asset id)
-- and then  the delete script --

You can do something like that.

I’ve actually created a system for building and destroying specific parts. In my implementation, I have one ModuleScript that handles all the input needed to change the object you want to create or modify your input state from building to remove.

When I want to build, I call a function in another ModuleScript with the target and building as parameters so it can compare to a dictionary and see if I can actually build where I want to.

When I want to remove, I call another function that can check if I can actually remove the part I want to remove.

The code I have looks something like this:

-- Input Module

function Input.ChangeBuilding(building)
	Input.Building = building
end

function Input.ChangeInputState(state)
	if Input.InputState == state then
		Input.InputState = nil
		
	else
		Input.InputState = state
	end
end

function Input.Click()
	if Input.InputState then
		if Input.InputState == "Build" then
			Input.PurchaseHandler.Purchase(Input.Mouse.Target, Input.Building)
			
		elseif Input.InputState == "Remove" then
			Input.BuildHandler.Remove(Input.Mouse.Target)
		end
    end
end

-- Build Module

local Buildings = {
    House = BuildingObjects:FindFirstChild("House");
}

function Build.Build(target, building)
	if target then
		if Buildings[building] then
            local building = Buildings[building]:Clone()
        end
	end
end

function Build.Remove(target)
	if target and not target:GetAttribute("Biome") then
		target:Destroy()
	end
end

So basically there’s no table in these module scripts?

These are just snippets of code to show you how I implemented a similar system to fit my use case. Each module is it’s own table.

Is this fine?

Module:

local Input = nil

local function ObjectDestroy()
	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]
			mouse.Button1Down:Connect(function()
				if mouse.Hit.Position == RemoveObjects[1].Position or RemoveObjects[2].Position then
					RemoveObjects[1]:Destroy()  
				end			
		end)		
end
	end
end

The script that calls it:

require(6895353279)

There are a few things that stand out.

Box.Adornee = RemoveObjects[1] or RemoveObjects[2]

Box.Parent = RemoveObjects[1] or RemoveObjects[2]

if mouse.Hit.Position == RemoveObjects[1].Position or RemoveObjects[2].Position then

RemoveObjects[1] is never nil, meaning that these lines will always use RemoveObjects[1].

So do you think the module script would function(I’d already inserted a script to call it)

Never mind the script is not working with no trace of error.

The ModuleScript should have different functions that each do a specific thing. Have one to generate the preview to show the user what they are selecting. Have another to actually delete what the user is selecting and have another to build.

Use an external script to control the input of the user and call functions accordingly, passing along the required information.

mouse.Target will be baseplate or some textbutton maybe

Try doing this:

if mouse.Target:IsA("Part") and mouse.Target.Parent = workspace then
  mouse.Target:Destroy()
end

And for pc and console users I recommend a key bind as if player move their mouse to click delete button they cannot keep their cursor on the part they dont want of building system, and on mobile a 2 step thing called select part and then another gui becomes visible saying delete part

The script( NOT THE MODULE SCRIPT)keeps getting this error:
image
this is the script:
require(6895493201)

Also, is there a different way other than using module scripts. I’ve never used it before and thus, I really can’t do anything with them. Can I insert a regular script to manage this?