Issue with creating a system of selection by pressing the button

I’ve been trying for the second day now to make a selection system by button holding. That when the player holding the button and clicked on a few parts they were selected and when you release the button the selection is applied and parts can be moved, change color, etc. But I tried many options and they were not working. I searched the entire forum and did not find an answer. So far I have stopped at this option. I will be very grateful for your help. Here is my script:

local Mouse = game.Players.LocalPlayer:GetMouse()
local CAS = game:GetService("ContextActionService")

local Handles = game.Players.LocalPlayer.PlayerGui:WaitForChild("Handles"):FindFirstChild("Handle")
local ArcHandles = game.Players.LocalPlayer.PlayerGui:WaitForChild("Handles"):FindFirstChild("ArcHandles")

local PlayerGUI = game.Players.LocalPlayer.PlayerGui

local debounce = false

local Selected = {}

local CountofPress = 0

for _, v in ipairs(workspace:WaitForChild("JustPlace"):GetChildren()) do
	if v:IsA("BasePart") then
		Mouse.TargetFilter = v	
	end
end

Mouse.Button1Down:Connect(function()
	if game:GetService("CollectionService"):HasTag(Mouse.Target, "CanBeEdited") then
		local CheckOwner = game.ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("CheckIfPlayerOwnerOfThisPart"):InvokeServer(Mouse.Target)
		local IsHolding = UserInputService:IsKeyDown(Enum.KeyCode.LeftControl)
		
		if CheckOwner then
			if IsHolding then
				table.insert(Selected, Mouse.Target)
				local Model = Instance.new("Model")
				Model.Parent = workspace:WaitForChild("Parts"):WaitForChild(game.Players.LocalPlayer.Name.."_s folder")
				for _, v in pairs(Selected) do
					v.Parent = Model
					local SelectionBox = Instance.new("SelectionBox")
					SelectionBox.Parent = Model
					SelectionBox.Adornee = Model
					v:SetAttribute("IsActive", true)
					--Handles.Adornee = Model
					--ArcHandles.Adornee = Model
				end
			else
				Selected = {}
				for _, v in pairs(workspace:WaitForChild("Parts"):WaitForChild(game.Players.LocalPlayer.Name.."_s folder"):GetDescendants()) do
					if v:IsA("SelectionBox") then
						v:Destroy()
					end
				end
				for _, v in pairs(workspace:WaitForChild("Parts"):WaitForChild(game.Players.LocalPlayer.Name.."_s folder"):GetChildren()) do
					if v:IsA("Part") or v:IsA("Model") then
						if v:GetAttribute("IsActive") == true then
							v:GetAttribute("IsActive", v:SetAttribute("IsActive", false))
						end
					end
				end
			end
			
			if CountofPress == 0 then
				Handles.Style = Enum.HandlesStyle.Resize
				Handles.Visible = true
			elseif CountofPress == 1 then
				Handles.Style = Enum.HandlesStyle.Movement
			elseif CountofPress == 2 then
				Handles.Visible = false
				ArcHandles.Visible = true
			elseif CountofPress >= 3 then
				CountofPress = 0
				Handles.Visible = true
				ArcHandles.Visible = false
			end

			Handles.Adornee = Mouse.Target
			ArcHandles.Adornee = Mouse.Target

			local SelectionBox = Instance.new("SelectionBox")
			SelectionBox.Parent = Mouse.Target
			SelectionBox.Adornee = Mouse.Target
			Mouse.Target:GetAttribute("IsActive", Mouse.Target:SetAttribute("IsActive", true))
			PlayerGUI:WaitForChild("EditProperty").Enabled = true
			if Mouse.Target.CanCollide == true then
				PlayerGUI:WaitForChild("EditProperty"):WaitForChild("PropertiesFrame"):WaitForChild("ScrollingFrame"):WaitForChild("CanCollide").Text = "Collide: On"
			else
				PlayerGUI:WaitForChild("EditProperty"):WaitForChild("PropertiesFrame"):WaitForChild("ScrollingFrame"):WaitForChild("CanCollide").Text = "Collide: Off"
			end
			if Mouse.Target.CanTouch == true then
				PlayerGUI:WaitForChild("EditProperty"):WaitForChild("PropertiesFrame"):WaitForChild("ScrollingFrame"):WaitForChild("CanTouch").Text = "Touchable: On"
			else
				PlayerGUI:WaitForChild("EditProperty"):WaitForChild("PropertiesFrame"):WaitForChild("ScrollingFrame"):WaitForChild("CanTouch").Text = "Touchable: Off"
			end
			if Mouse.Target.Anchored == true then
				PlayerGUI:WaitForChild("EditProperty"):WaitForChild("PropertiesFrame"):WaitForChild("ScrollingFrame"):WaitForChild("Anchored").Text = "Anchored: On"
			else
				PlayerGUI:WaitForChild("EditProperty"):WaitForChild("PropertiesFrame"):WaitForChild("ScrollingFrame"):WaitForChild("Anchored").Text = "Anchored: Off"
			end
		end
	else
		Selected = {}
		Handles.Adornee = nil
		ArcHandles.Adornee = nil

		for _, v in pairs(workspace:WaitForChild("Parts"):WaitForChild(game.Players.LocalPlayer.Name.."_s folder"):GetDescendants()) do
			if v:IsA("SelectionBox") then
				v:Destroy()
			end
		end
		for _, v in pairs(workspace:WaitForChild("Parts"):WaitForChild(game.Players.LocalPlayer.Name.."_s folder"):GetChildren()) do
			if v:IsA("Part") or v:IsA("Model") then
				if v:GetAttribute("IsActive") == true then
					v:GetAttribute("IsActive", v:SetAttribute("IsActive", false))
				end
			end
		end
		PlayerGUI:WaitForChild("EditProperty").Enabled = false
	end
end)

local function ChangeHandle(ActionName, UserInputState, Obj)
	if UserInputState == Enum.UserInputState.Begin then
		CountofPress += 1
		
		Handles.Style = Enum.HandlesStyle.Resize
		
		if CountofPress == 1 then
			Handles.Style = Enum.HandlesStyle.Movement
		elseif CountofPress == 2 then
			Handles.Visible = false
			ArcHandles.Visible = true
		elseif CountofPress >= 3 then
			CountofPress = 0
			Handles.Visible = true
			ArcHandles.Visible = false
		end
	end
end

CAS:BindAction("Change", ChangeHandle, true, Enum.KeyCode.R)

Sorry about the bad script.

2 Likes

Hey, could you send a video of your issue, tell us what is happening, and what you want it to do?

All seems to be fine, but the models are created a lot of plus if the player did not finish selecting the model is still created. I want to achieve that Parts can be changed at will if the player has selected the desired parts

What if instead of creating a new model, there would already be a model ready, and you’d just parent everything into that?

I think it’ll probably work. I’ll try to make it

It seems to work fine for me, so that is probably your best bet

Screenshot 2024-09-28 164459

Yes it worked, but I didn’t understand how to fix the fact that Selection boxes are created so much

Because you are creating a new selectionbox every time you click on a part

				for _, v in pairs(Selected) do
					v.Parent = Model
					local SelectionBox = Instance.new("SelectionBox")
					SelectionBox.Parent = Model
					SelectionBox.Adornee = Model
					v:SetAttribute("IsActive", true)
					--Handles.Adornee = Model
					--ArcHandles.Adornee = Model
				end

you should look for the SelectionBox as the children of the model, if not found then create a new one, otherwise use the already existing one

Yes, it’s working! Thank you so much for your help!:slightly_smiling_face:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.