How to get models/parts in a frame

How could I detect what models/parts are inside of a gui frame?

Similar to this:

1 Like

I love stuff like this its fun to code sometimes too ! lol

the basic idea for doing something like this is to convert world to screen positions or screen to world.
I think it would be easiest to find positions of all your troops or whatever you are selecting and find screen positions for them.

Then checking if they are inside a box / circle or whatever you have to tell the player to select.

a little example:
local troop_position = game.Workspace.troop.PrimaryPart.Position

game.Workspace.CurrentCamera:WorldToScreenPoint(troop_position)
 -- this gives you the position

then you just compare it to the corners of the frame to see if its inside your square!

hope that helps I might have had to get a new keyboard right after I started typing the first few words lol

4 Likes

I am pretty sure the GUI in this case is fake and is not used for detection, only as a visual guide. They most likely do some area math to calculate it and convert the 2D screen to 3D exactly as DamSam127 said.

1 Like

How could I do that?

Also, I’m not sure this will work, cause there will be multiple troops, and multiple players spamming troops…

well you get the corners of the frame ( bottom left and top right ) if the world space to screen is bigger than bottom left and smaller than top right then its supposed to be selected.

also the troop position works you just need to loop it for all troops you own like:

for i,v in pairs(troops_you_own) do
    if is_in_selection() then
        -- select
    end
end

Could I have a small code example of this? I’m just not sure how to get the corner of a gui frame.

Do you mean they detect the mouse click start and then the mouse click end and do some math to detect everything in-between?

Yeah thats pretty much the idea


local player = game.Players.LocalPlayer

local Mouse = player:GetMouse()

local Frame = player.PlayerGui.ScreenGui.Frame

local UIS = game:GetService("UserInputService")

local MouseDownAt = nil

UIS.InputBegan:Connect(function(Input,GameProcessed)
	if not GameProcessed then
		if Input.UserInputType == Enum.UserInputType.MouseButton1 then
			MouseDownAt = Vector2.new(Mouse.X,Mouse.Y)
		end
	end
end)

function IsABigger(a,b)
	if a.X > b.X and a.Y > b.Y then
		return true
	else
		return false
	end
end

UIS.InputEnded:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		if MouseDownAt then
			
			-- this depends on your anchor point for the frame ( if you have 0.5,0.5 do it like this: )
			
			local Top_Right = Frame.Position + Frame.Size / 2
			local Bottom_Left = Frame.Position - Frame.Size / 2
			
			-- you could say these mobs are troops
			for i,v in pairs(game.Workspace.World.Mobs:GetChildren()) do
				local mob_pos = v.PrimaryPart.Position
				local screen_pos = game.Workspace.CurrentCamera:WorldToScreenPoint(mob_pos)
				
				if IsABigger(screen_pos,Bottom_Left) and IsABigger(Top_Right,screen_pos) then
					-- select mob
				end
				
			end
		end
	end
end)

thats how id do it ( kinda u still need to move the frame so the player knows where they are selecting n stuff ) also need to implement a select function

2 Likes

wait, do u mean model selecting?

I think you are thinking of like studio selecting, but he wants to be able to get objects inside of a frame ( to uhhhh select…? with code )

That seems pretty hard, never done something like that

1 Like

Yeah its not super easy, but with a little bit of research and work its great! it feels really good when you create something like this.