Advanced Examples - Swordphin/raycastHitboxRbxl GitHub Wiki
This section incorporates some niche cases. For most cases, the beginner examples is all you need to fully maximize the potential of this module, but perhaps you can find some use cases here as well.
Contents
- Link Attachments
- Part Mode
- Grouping / Hit Distinction
- Turning off a Hitbox automatically after a few seconds
Link Attachments
We can combine two attachments together with a raycast by using the LinkAttachments function. Provided we have two parts with each having their own attachment.
We can use the following code to link the attachments. The module will raycast between these two points.
--- This script is in the workspace
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RaycastHitbox = require(ReplicatedStorage.RaycastHitboxV4)
-- We will construct a new hitbox for our part
local newHitbox = RaycastHitbox.new(workspace.Part1) --- Part1 or Part2, doesn' really matter for this example
newHitbox:LinkAttachments(workspace.Part1.Link1, workspace.Part2.Link2)
newHitbox:HitStart()
Wherever the part/attachment moves, the raycasting will also follow alongside it. Useful mainly for traps or if you want to have mainly vertical hit detection.
Detection Modes
By default, raycastHitRbxl only hits a target one time once it detects its humanoid. Depending on your use cases, you often may want to detect multiple parts of a rig (for example, you want a sword to detect a shield so you can code in custom block logic or deflect). Consider using Detection Modes for this. Essentially it acts as the default Touched function for parts.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RaycastHitbox = require(ReplicatedStorage.RaycastHitboxV4)
-- We will construct a new hitbox for our part
local newHitbox = RaycastHitbox.new(MySwordModel)
newHitbox.RaycastParams = RaycastParams.new()
newHitbox.RaycastParams.FilterDescendantsInstances = {MyPlayerCharacter}
newHitbox.RaycastParams.FilterType = Enum.RaycastFilterType.Blacklist
-- Turn on Part Mode
newHitbox.DetectionMode = RaycastHitbox.DetectionMode.PartMode
newHitbox.OnHit:Connect(function(hit)
print(hit.Name)
end)
Hit Distinction
Individual raycast points on your weapons or melee entities can have specific "group names" tagged to them. When these points hit a target, the OnHit event will return the groupName as its fourth parameter, allowing you to do more things depending on where the weapon hit. For example, Super Smash Bro Marth character has a mechanic known as a "tipper" which increases damage near the outer part of his blade. Similar ideas can be employed here.
Attachment Method
Set an Attribute into the desired attachment point and name it Group. The value in the attribute should be the name of the group you want it to be. If you are using Link Attachments, only the primary attachment needs the attribute. The default name of "Group" can be changed in the module as well.
attachment:SetAttribute("Group", "Group Name Here!")
SetPoints Method
local Points = {
Vector3.new(0, 5, 0),
Vector3.new(0, 10, 0)
}
local GroupName = "Tipper"
Hitbox:SetPoints(part, Points, GroupName)
Example
Hitbox.OnHit:Connect(function(part, humanoid, raycastResults, groupName)
if groupName == "Tipper" then
humanoid:TakeDamage(200)
else
humanoid:TakeDamage(30)
end
end)
Video Example (yellow is the tipper, green is normal): https://imgur.com/Qgfej4K
Hitbox Time Scheduler
Instead of using wait(), spawn(), or delay() which are prone to throttling, consider using RaycastHitbox's internal custom time scheduler to automatically switch the hitbox off for you. Using this will allow you to continue the script without it yielding. It is extremely simple to use, all you need to do is give HitStart a number. The module will use its preexisting loop to calculate the time automatically with no added performance cost. No hassles and no additional threads created.
local turnOffAfterThisAmountOfSeconds = 5
Hitbox:HitStart(turnOffAfterThisAmountOfSeconds) --- Turns off after 5 seconds automatically
--- Does not yield the thread, the script will automatically continue
print("Hey! I still print!")