WorldRoot
This base class provides an API for any instance intended for handling 3D spatial queries and simulation, such as Workspace and WorldModel.
Summary
Properties
Methods
Returns true if any of the given BasePart are touching any other parts.
Casts a block shape in a given direction and returns a RaycastResult if the shape hits a BasePart or Terrain cell.
Returns an array of parts whose bounding boxes overlap a given box.
Returns an array of parts whose bounding boxes overlap a given sphere.
Returns an array of parts whose occupied space is shared with the given part.
Moves the specified part to the specified location via inverse kinematics rather than moving it there directly, to ensure any joints, constraints, or collisions that part is participating in remain physically satisfied.
Casts a ray using an origin, direction, and optional RaycastParams, then returns a RaycastResult if an eligible object or terrain intersects the ray.
Casts a spherical shape in a given direction and returns a RaycastResult if the shape hits a BasePart or Terrain cell.
Events
Properties
Methods
ArePartsTouchingOthers
ArePartsTouchingOthers returns true if at least one of the given BasePart are touching any other parts. Two parts are considered "touching" if they are within the distance threshold, overlapIgnored.
If no parts are provided, false is returned.
Parameters
A list of parts checks to see if any parts in the list are touching any parts not in the list.
The part overlap threshold in studs that is ignored before parts are considered to be touching.
Returns
Code Samples
local part1 = Instance.new("Part")
part1.Name = "Part1"
part1.Anchored = true
part1.Transparency = 0.5
part1.Color = Color3.fromRGB(185, 100, 38)
part1.Size = Vector3.new(2, 2, 2)
part1.Position = Vector3.new(0, 4, 0)
part1.Parent = workspace
local part2 = Instance.new("Part")
part2.Name = "Part2"
part2.Anchored = true
part2.Transparency = 0.5
part2.Color = Color3.fromRGB(200, 10, 0)
part2.Size = Vector3.new(2, 2, 2)
part2.Position = Vector3.new(0, 5, 0)
part2.Parent = workspace
local partList = { part1 }
print(workspace:ArePartsTouchingOthers(partList, 0)) -- True
print(workspace:ArePartsTouchingOthers(partList, 0.999)) -- True
print(workspace:ArePartsTouchingOthers(partList, 1)) -- False
Blockcast
Casts a block shape in a given direction and returns the first collision with a BasePart or Terrain cell. This is analogous to how WorldRoot:Raycast() casts a linear ray in a direction to find a collision, but it uses a 3D shape instead of a ray.
Unlike WorldRoot:GetPartsInPart(), this method does not detect BaseParts that initially intersect the shape.
If a hit is detected, a RaycastResult is returned containing the hit information. The Distance property represents the distance the shape has to travel to find a hit, and the Position property represents the intersection point that causes the hit.
This method throws an error if it is passed invalid CFrame, size, or direction inputs.
Parameters
The initial position and rotation of the cast block shape.
The size of the cast block shape.
Direction of the shapecast, with the magnitude representing the maximum distance the shape can travel.
Returns
Code Samples
local params = RaycastParams.new()
params.FilterDescendantsInstances = {workspace.Model}
local part = workspace.Part
local direction = part.CFrame.LookVector * 100
local result = workspace:Blockcast(part.CFrame, part.Size, direction, params)
if result then
local finalPos = part.Position + direction.Unit * result.Distance
print("Object/terrain hit:", result.Instance:GetFullName())
print("Hit position:", result.Position)
print("Final position:", finalPos)
print("Surface normal at the point of intersection:", result.Normal)
print("Distance until hit was found:", result.Distance)
print("Material hit:", result.Material.Name)
else
print("Nothing was hit!")
end
BulkMoveTo
Warning: You should only use this function if you are sure that part movement is a bottleneck in your code, simply setting the CFrame property of the individual parts / welded models you want to move will be fast enough in the vast majority of cases.
This function moves a table of parts to the location specified in a table of CFrames. This makes it a very fast way to move large numbers of parts, as you don't have to pay the cost of separate property sets for each individual part.
The third argument of BulkMoveTo allows you to further speed up movement of the parts by specifying the Position and Orientation. Changed events should not be fired on the parts. If you specify FireCFrameChanged as the BulkMoveMode then only CFrame.Changed be fire, rather than changed firing for Position, Orientation, and CFrame like it normally does.
Parameters
Returns
GetPartBoundsInBox
WorldRoot:GetPartBoundsInBox() returns an array of parts whose bounding boxes overlap a box whose volume is described using the given center (CFrame) and size (Vector3).
As emphasized, this spatial query method efficiently considers the volume of parts' bounding boxes rather than their actual occupied volume. This may be important when considering cylinders, spheres, unions, and MeshParts which have non-block shapes. For cases where accuracy especially matters, use WorldRoot:GetPartsInPart() instead, or further filter the results of this method yourself.
This method uses a OverlapParams object to describe reusable portions of the spatial query, such as an inclusion or exclusion list, the maximum number of parts to query, what collision group to use, and whether the query favors an intersected part's BasePart.CanCollide value over its BasePart.CanQuery value.
Parameters
The location of the center of the given box volume to be queried.
The size of the given box volume to be queried.
Contains reusable portions of the spatial query parameters.
Returns
GetPartBoundsInRadius
WorldRoot:GetPartBoundsInRadius() returns an array of parts whose bounding boxes overlap a sphere whose volume is described using the given center (Vector3) and radius (number).
As emphasized, this spatial query method efficiently considers the volume of parts' bounding boxes rather than their actual occupied volume. This may be important when considering cylinders, spheres, unions, and MeshParts which have non-block shapes. For cases where accuracy especially matters, use WorldRoot:GetPartsInPart() instead, or further filter the results of this method yourself.
This method uses a OverlapParams object to describe reusable portions of the spatial query, such as an inclusion or exclusion list, the maximum number of parts to query, what collision group to use, and whether the query favors an intersected part's BasePart.CanCollide value over its BasePart.CanQuery value.
Parameters
The location of the center of the given sphere volume to be queried.
The radius of the given sphere volume to be queried.
Contains reusable portions of the spatial query parameters.
Returns
GetPartsInPart
WorldRoot:GetPartsInPart() returns an array of parts whose occupied space is shared with the given part (which must exist in the same WorldRoot as the parts to be queried). This method can be used in place of BasePart:GetTouchingParts() and is generally a better choice.
As noted, this spatial query method considers the exact volume occupied by the given part using a full geometric collision check. As an example, a concave/hollow part won't match queried parts within it unless they actually overlap/touch such a part. For simpler volumes, consider using WorldRoot:GetPartBoundsInBox() or WorldRoot:GetPartBoundsInRadius(), as they are less accurate but perform more efficiently.
This method uses a OverlapParams object to describe reusable portions of the spatial query, such as an inclusion or exclusion list, the maximum number of parts to query, what collision group to use, and whether the query favors an intersected part's BasePart.CanCollide value over its BasePart.CanQuery value.
Parameters
The part whose volume is to be checked against other parts.
Contains reusable portions of the spatial query parameters.
Returns
IKMoveTo
This function moves the specified part to the specified location via inverse kinematics rather than moving it there directly, to ensure any joints, constraints, or collisions that part is participating in remain physically satisfied. Currently this function is only available in Studio to plugins, as it currently conflicts with the physics of a running game.
Translate stiffness is a number between 0 and 1 specifying how agressively to match the part's position to the position part of the target CFrame. Rotate stiffness is a number between 0 and 1 specifying how agresively to match the part's rotation to to the rotation part of the target CFrame.
For example:
- If translate stiffness and rotate stiffness are both equal to 1, then the part will be moved exactly to the target CFrame regardless of what physical constraints there are on it.
- If translate stiffness and rotate stiffness are both equal to 0.5, then the part will try to move to exactly the target CFrame, but may be pushed out of the way by physical constraints on it.
- If translate stiffness and rotate stiffness are both equal to 0, then the target CFrame will be ignored and physical constraints will be solved for the object at the position where it was.
Parameters
The part being moved.
The location to move the specified part.
A number between 0 and 1 specifying how aggressively to match the part's position to the position part of the target CFrame.
A number between 0 and 1 specifying how aggresively to match the part's rotation to to the rotation part of the target CFrame.
Allows you to specify what objects should be effected by the physical resolution.
Returns
Raycast
Casts a ray using an origin, direction, and optional RaycastParams. If it finds an eligible BasePart or Terrain cell, a RaycastResult is returned containing the results of the operation. If no RaycastParams object is provided, the defaults are used (all parts are considered and Terrain water is not ignored).
Note that the length (magnitude) of the directional vector is important, as objects/terrain further away than its length will not be tested. If you're using a CFrame to help create the ray components, consider using CFrame.LookVector as the directional vector and multiply it by the desired length as shown in the example below. The maximum length of the direction vector is 5,000 studs.
This method does not use a Ray object, but its origin and direction components can be borrowed from Ray.Origin and Ray.Direction.
Parameters
The origin point of the ray.
The directional vector of the ray. Note that the length of this vector matters, as parts/terrain further away than its length will not be tested.
An object used to specify hit eligibility in the raycast operation. If not provided, default values are used where all parts are considered and Terrain water is not ignored.
Returns
Code Samples
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Include
raycastParams.FilterDescendantsInstances = {workspace.Model}
raycastParams.IgnoreWater = true
local part = workspace.Part
local raycastResult = workspace:Raycast(part.Position, part.CFrame.LookVector * 50, raycastParams)
if raycastResult then
print("Object/terrain hit:", raycastResult.Instance:GetFullName())
print("Hit position:", raycastResult.Position)
print("Surface normal at the point of intersection:", raycastResult.Normal)
print("Material hit:", raycastResult.Material.Name)
else
print("Nothing was hit!")
end
Spherecast
Casts a spherical shape in a given direction and returns the first collision with a BasePart or Terrain cell. This is analogous to how WorldRoot:Raycast() casts a linear ray in a direction to find a collision, but it uses a 3D shape instead of a ray.
Unlike WorldRoot:GetPartsInPart(), this method does not detect BaseParts that initially intersect the shape.
If a hit is detected, a RaycastResult is returned containing the hit information. The Distance property represents the distance the shape has to travel to find a hit, and the Position property represents the intersection point that causes the hit.
This method throws an error if it is passed invalid CFrame, size, or direction inputs.
Parameters
The initial position of the cast spherical shape.
The radius of the cast spherical shape in studs.
Direction of the shapecast, with the magnitude representing the maximum distance the shape can travel.
Returns
Code Samples
local params = RaycastParams.new()
params.FilterDescendantsInstances = {workspace.Model}
local part = workspace.Part
local direction = part.CFrame.LookVector * 100
local result = workspace:Spherecast(part.Position, part.Size.X / 2, direction, params)
if result then
local finalPos = part.Position + direction.Unit * result.Distance
print("Object/terrain hit:", result.Instance:GetFullName())
print("Hit position:", result.Position)
print("Final position:", finalPos)
print("Surface normal at the point of intersection:", result.Normal)
print("Distance until hit was found:", result.Distance)
print("Material hit:", result.Material.Name)
else
print("Nothing was hit!")
end