3D Percentage
Showcase
In this example, this resource calculates the progress of the player’s advancement in a 3D environment.
Code used
_3DPercentage:SetGroup("Way #1", {Priority = 1})
_3DPercentage:AddPartToGroup(workspace.Path1, "Way #1", {DominantAxis = Enum.Axis.Z})
_3DPercentage:AddPartToGroup(workspace.Path2, "Way #1")
_3DPercentage:AddPartToGroup(workspace.Path3, "Way #1", {DominantAxis = Enum.Axis.Z})
_3DPercentage:AddPartToGroup(workspace.Path4, "Way #1")
_3DPercentage:AddPartToGroup(workspace.Path5, "Way #1", {DominantAxis = Enum.Axis.Z})
_3DPercentage:AddPartToGroup(workspace.Path6, "Way #1")
_3DPercentage:SetGroup("Way #2")
_3DPercentage:AddPartToGroup(workspace.Path7, "Way #2", {DominantAxis = Enum.Axis.Y})
local event = game:GetService("ReplicatedStorage"):WaitForChild("Percentage")
while game:GetService("RunService").Stepped:Wait() do
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
if player.Character then
local progress = _3DPercentage:GetGlobalProgress(player.Character.PrimaryPart)
progress = math.round(math.clamp(progress or 0, 0, 1) * 100) .. "%"
event:FireClient(player, progress)
end
end
end
Usage
Installation:
In order to set up this resource, just download the model or use require()
. You should put it like this:
local _3DPercentage = require(8207443586) -- Or the ModuleScript you have in your game
-- Start using it
_3DPercentage:SetGroup("Way #1")
_3DPercentage:AddPartToGroup(workspace.Path1, "Way #1")
_3DPercentage:AddPartToGroup(workspace.Path2, "Way #1")
while game:GetService("RunService").Stepped:Wait() do
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
if player.Character then
print(_3DPercentage:GetGlobalProgress(player.Character.PrimaryPart))
end
end
end
Groups:
To start using this resource, you have to put the parts in groups. They will be tracking the BasePart
s that are within their boundaries. This is how this resource works. Example:
You can make and use more than 1 group. For example, if you want to track BasePart
s Y position as well:
These are the options you can set for a group:
Key | Datatype | Default | Description |
---|---|---|---|
OverlapParams | OverlapParams |
OverlapParams{MaxParts=false, CollisionGroup=Default, FilterDescendantsInstances={}} | Contains reusable portions of the spatial query parameters |
Priority | int |
0 | Indicates the priority if a place is touching two parts of differents groups at the same time |
Parts:
The Instance
s which are going to track the BasePart
s have to be Part
s (it will error if you are trying to add another BasePart
in a group). Put it them one in front of the other for better results, like this:
Key | Datatype | Default | Description |
---|---|---|---|
DominantAxis | Axis |
Enum.Axis.X | Indicates how the Part should track BasePart s (for example, if Y then it will calculate from bottom to top). |
Order | int |
The order of the parts, following a way (please be careful since this setting can mess up the calculations). |
Functions:
This resource includes 8 functions:
void _3DPercentage:AddPartToGroup (Part part, string group, Dictionary Settings)
Adds the given part to a group with the given Settings.
Variant, string _3DPercentage:GetGlobalProgress (Variant place, OverlapParams overlapParams)
Gets the progress of the givenBasePart
,Vector3
orCFrame
considering all the groups in order of theirPriority
. Since this function ignores all groups’OverlapParams
, you can set it from this function instead (MaxParts
property is ignored). It returns the percentage (number
ornil
) and the group of the touched part.
Dictionary _3DPercentage:GetGroup (string name)
Returns a dictionary with information about the given group.
Dictionary _3DPercentage:GetGroups ()
Returns a dictionary with information about all the groups.
Variant _3DPercentage:GetProgress (Variant place, string group)
Gets the progress of the givenBasePart
,Vector3
orCFrame
if it’s touching a part of the given group (number
ornil
). It also consider all parts and groupSettings
.
void _3DPercentage:RemoveGroup (string name)
Removes the given group and all its references.
void _3DPercentage:RemovePartFromGroup (Variant reference, string group)
Removes with the givenint
orPart
the part from the given group.
Dictionary _3DPercentage:SetGroup (string name, Dictionary Settings)
Creates or edits the given group with the given Settings.
Known bugs
- For some reason, the percentage will be less than 0 or greater than 1 sometimes, to solve this use
math.clamp(percentage, 0, 1)
.
Changelog
v1.0.1
- This resource now counts the objects themselves of the passed array when using
OverlapParams.FilterDescendantsInstances
property.
v1.0.0
- Released
Feel free to comment if you have any questions.