SelectionBox help

So in my game I have this delete tool and I want a selectionBox to be created whenever you hover you mouse over a part.

I have that working but I want to make it so that when you hover you mouse over another object the previous selectionbox gets destroyed.

For context I am busy making a block building game:


And this is what I have so far:

Here is the script:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local DeleteTool = script.Parent
local DeleteToolEquiped = false

DeleteTool.Activated:Connect(function()
	print(Mouse.Target)
	if Mouse.Target then
		local BlocksFolder = game.Workspace:FindFirstChild(Player.Name .. "Blocks")--This is the folder where all the blocks that the player places are kept.
		if BlocksFolder then
			if Mouse.Target.Name == "Block" and Mouse.Target:IsDescendantOf(BlocksFolder) then
				Mouse.Target:Destroy()
			end
		end
	end
end)

DeleteTool.Equipped:Connect(function()
	DeleteToolEquiped = true
	Mouse.Move:Connect(function()
		if DeleteToolEquiped == true then
			if Mouse.Target then
				local BlocksFolder = game.Workspace:FindFirstChild(Player.Name .. "Blocks")
				if BlocksFolder then
					if Mouse.Target.Name == "Block" and Mouse.Target:IsDescendantOf(BlocksFolder) then
						if not Mouse.Target:FindFirstChildWhichIsA("SelectionBox") then
							local Target = Mouse.Target
							local SelectionBox = Instance.new("SelectionBox")
							SelectionBox.Parent = Target
							SelectionBox.Adornee = Target
							SelectionBox.Color3 = Color3.fromRGB(255,0,0)
						end
					end
				end
			end
		end
	end)
end)

DeleteTool.Unequipped:Connect(function()
	DeleteToolEquiped = false
end)
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local DeleteTool = script.Parent
local DeleteToolEquiped = false

DeleteTool.Activated:Connect(function()
	print(Mouse.Target)
	if Mouse.Target then
		local BlocksFolder = game.Workspace:FindFirstChild(Player.Name .. "Blocks")--This is the folder where all the blocks that the player places are kept.
		if BlocksFolder then
			if Mouse.Target.Name == "Block" and Mouse.Target:IsDescendantOf(BlocksFolder) then
				Mouse.Target:Destroy()
			end
		end
	end
end)


local LastSelectionBox = nil
DeleteTool.Equipped:Connect(function()
	DeleteToolEquiped = true
	Mouse.Move:Connect(function()
		if DeleteToolEquiped == true then
			if Mouse.Target then
				local BlocksFolder = game.Workspace:FindFirstChild(Player.Name .. "Blocks")
				if BlocksFolder then
					if Mouse.Target.Name == "Block" and Mouse.Target:IsDescendantOf(BlocksFolder) then
						if not Mouse.Target:FindFirstChildWhichIsA("SelectionBox") then
							if LastSelectionBox ~= nil then
								LastSelectionBox:Destroy()
							end
							local Target = Mouse.Target
							local SelectionBox = Instance.new("SelectionBox")
							SelectionBox.Parent = Target
							SelectionBox.Adornee = Target
							SelectionBox.Color3 = Color3.fromRGB(255,0,0)
							LastSelectionBox = SelectionBox
						end
					end
				end
			end
		end
	end)
end)

DeleteTool.Unequipped:Connect(function()
	DeleteToolEquiped = false
end)
1 Like