Fixing a bad selection script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

A selection box which selects NPC’s to move them (I already made a system where you can control NPC’s, now I want it so that you can select large quantities of NPC’s)

  1. What is the issue? Include screenshots / videos if possible!

the selection box’s 4 corners seems to be on the player’s camera. I tried getting the corners of the frame’s world position.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried following the solution in this different dev forum post: How to create a Selection Box - #11 by PercyGaming83. I tried getting the corners of the frame using raycasting and then get the check whether an NPC torso is near the corners of the frame

I got suspicious that the script was not working correctly because it was not detecting any torsos, so I decided to make the spawnlocation teleport to where Corner 1 of the frame is “supposedly” located at and the spawn location started teleporting towards the camera, as these screenshots show:

Anyways, here is the script im using:

local mouse = game.Players.LocalPlayer:GetMouse()
local frame = script.Parent

local function onMouseMoved()
	local camera = workspace.CurrentCamera
	local length = 500
	local framePosition = frame.Position
	local frameRotation = frame.Rotation
	local framePositionX = framePosition.X.Scale * framePosition.X.Offset
	local framePositionY = framePosition.Y.Scale * framePosition.Y.Offset
	local unitRay = camera:ScreenPointToRay(framePositionX, framePositionY)
	local ray = Ray.new(unitRay.Origin, unitRay.Direction * length)
	local corners = {}
	for i = 1, 4 do
		local corner = ray.Origin + ray.Direction * (i - 1) * Vector3.new(frame.Size.X, frame.Size.Y, 1) / 2
		corners[i] = corner
	end
	print(corners)
	game.Workspace.SpawnLocation.Position = corners[1]
	local torsosNearCorners = {}
	for _, object in ipairs(game.Workspace.Server_Troops:GetDescendants()) do
		if object:IsA("Part") and object.Name == "Torso" and object.Parent:FindFirstChild("Team").Value == game.Players.LocalPlayer.Team.Name then
			local torsoCFrame = object.CFrame
			for i = 1, 4 do
				local cornerCFrame = CFrame.new(corners[i])
				local distance = (torsoCFrame.Position - cornerCFrame.Position).Magnitude
				if distance < 20 then
					if distance > 50 then
						distance = distance - distance * 0.5
					end
					torsosNearCorners[#torsosNearCorners + 1] = object
				end
			end
		end
	end
	print("Torsos near corners:", torsosNearCorners)
	if #torsosNearCorners > 0 then
		local nearestTorso = torsosNearCorners[1]
		local nearestTorsoPosition = nearestTorso.Position
		local nearestTorsoCFrame = CFrame.new(nearestTorsoPosition)
		local nearestTorsoScreenPosition = camera:WorldToScreenPoint(nearestTorsoCFrame.Position)
		frame.Position = nearestTorsoScreenPosition
	end
end

mouse.Button1Down:Connect(onMouseMoved)

The Console shows where the 4 corners are supposedly located at and also which torso’s of friendly Npc’s are being detected.

If there are other ways to get my goal, please notify me (this is the method of which I tried to do it)
Thanks.

The selection script is fixed now

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