Making A drag select system?

Hello! I Am RequiemZap, the title says it all, so i was working on my game lately, so i had this issue, i needed to make a drag system (like the one in this image) that selects multiple objects, in my case i want to select multiple troops,i don’t really know how would i do it.
image

3 Likes

You mean something like this?

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

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

local Frame = script.Parent.Frame -- Change to the "drag frame".
local TroopsFolder = workspace.Troops -- Change to the folder with the troops.

local SelectedTroops = {}
function SelectTroop(Troop) -- Customize for your specific case.
	if table.find(SelectedTroops, Troop) then return end
	Instance.new("Highlight", Troop).FillTransparency = 1
	table.insert(SelectedTroops, Troop)
end

function DeselectAllTroops() -- Customize for your specific case.
	for _,Troop in ipairs(SelectedTroops) do
		local Highlight = Troop:FindFirstChild("Highlight")
		if Highlight then Highlight:Destroy() end
	end
	table.clear(SelectedTroops)
end

local Corner1 = Vector2.zero
local DragConnection
UserInputService.InputBegan:Connect(function(InputObject, GameProcessed)
	if InputObject.UserInputType ~= Enum.UserInputType.MouseButton1 or GameProcessed then return end
	Corner1 = Vector2.new(Mouse.X, Mouse.Y)
	Frame.Position = UDim2.fromOffset(Mouse.X, Mouse.Y)
	Frame.Size = UDim2.fromOffset(0, 0)
	Frame.Visible = true
	
	if DragConnection then DragConnection:Disconnect() end
	DragConnection = Mouse.Move:Connect(function()
		Frame.Size = UDim2.fromOffset(Mouse.X, Mouse.Y) - Frame.Position
	end)
end)

UserInputService.InputEnded:Connect(function(InputObject, GameProcessed)
	if InputObject.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
	if DragConnection then DragConnection:Disconnect() end
	
	Frame.Visible = false
	
	if not UserInputService:IsKeyDown(Enum.KeyCode.LeftControl) then DeselectAllTroops() end --In case you want LeftCtrl to stop all others from deselecting. Optional line.
	
	local Corner2 = Vector2.new(Mouse.X, Mouse.Y)
	local Size = Corner2 - Corner1
	
	local Negate = Vector2.new(Size.X - math.abs(Size.X), Size.Y - math.abs(Size.Y)) / 2
	Corner1 += Negate
	Corner2 -= Negate
	
	for _,Troop in ipairs(TroopsFolder:GetChildren()) do
		local ScreenPosition = workspace.CurrentCamera:WorldToScreenPoint(Troop.Position) -- Possibly modify a little bit.
		if ScreenPosition.X < Corner1.X or 
			ScreenPosition.X > Corner2.X or
			ScreenPosition.Y < Corner1.Y or
			ScreenPosition.Y > Corner2.Y then continue end
		
		SelectTroop(Troop)
	end
end)

It looks like this:
robloxapp-20230806-2032515.wmv (1.2 MB)

11 Likes

oh wow, didn’t expect that, like AT ALL, i am gonna look into it rn
thanks in advance

tysm, it’s working well, i am gonna edit it to fit the other scripts, you are a life saver fr, respect+

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