Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to use magnitude in a different way?

So I wanted to make a proximity script which is a script that activates a function after some object is within a certain range so I wanted to make it so when I press "q" I can get all the objects within a certain magnitude of my torso and do something with them..... However the only time I've seen magnitude being used is when people subtract positions of 2 CERTAIN objects and then they get the magnitude of the solution and then confirm if the objects are within a certain range. However I want to use it so any object within a certain range of my torso get deleted or something of that sort.... My question for this is if that is possible and if it is please tell me how I would because I have no idea where to go with this....

1 answer

There's something called FindPartsInRegion3 which allows you to get parts within a box (returned as a table). It's worth noting that FindPartsInRegion3 is limited to 100 parts.

local player = game.Players.LocalPlayer -- assuming this is a LocalScript
local character = player.Character or player.CharacterAdded:wait()

function getPartsInRadialBox(origin, radius)
	local a = origin - Vector3.new(radius, radius, radius) -- these two lines find the two points needed to make a Region3
	local b = origin + Vector3.new(radius, radius, radius) 
	local region = Region3.new(a, b) -- create a region out of two points
	return game.Workspace:FindPartsInRegion3(
		region, -- find parts in the region
		character, -- ignore parts that are part of our character.
		100) -- get at most 100 parts (which is the limit)
end

function activateSkill(radius)
	local pos = character:FindFirstChild("Torso").Position
	local parts = getPartsInRadialBox(pos, radius)
	--TO-DO: Iterate through the parts and do something
end

--TO-DO: button to activate the skill

6 comments

0

How would I do that if I wanted a keydown event for the letter q? I mean is there anyway I can make it all inside of 1 whole function?

0

Also what is the argument called "origin" used for?

0
  1. You never ever want the entire script to be just one whole function. That's bad programming. 2. You would use UserInputService.InputBegan to detect key presses. 3. "origin" would be the center of the box. In this case, it would be your torso.
0

Alright well I am confused as to what you want met'TO DO' in the script I want it so when the character press 'Q' a region gets created around him and anything within that region gets destroyed except his character....

0

I'm getting the feeling that this is more of a scripting request.

0

I don't learn as well by explainations as I do by Examples when I am dealing with something new like Region3.new

This discussion is preserved as it was. New answers, comments, and votes are closed.