Skip to content

Beginner Examples

Jackie edited this page Jul 2, 2021 · 9 revisions

This initial setup guide will walk you through the most basic form of making this module work. It will be up to you on how to use it in your games and/or applications. Do note that the raycastHitboxRbxl works on both Client and Server.

Contents

Initial Requirements

  1. Download the raycastHitboxRbxl module from Roblox or Github.
  2. Place the module in a place where scripts can easily reference it, such as ReplicatedStorage.

Making a Deadly Part (The Easy/Lazy Way)

  1. Create a part in your workspace.

  1. Make sure to enable Constraint Details to see the attachments we are going to insert. Add a few attachments, around 1 stud or so apart from each other in any direction you like. These attachments will act as points for the raycasts to shoot out of. Rename those attachments to DmgPoint (this is the default name that the module looks for so it doesn't interfere with any other attachments).

  1. Insert a script inside the part you just created and use the code below.

-- << Initial Setup >> 
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RaycastHitbox = require(ReplicatedStorage.RaycastHitboxV4)

-- We will construct a new hitbox for our part
local newHitbox = RaycastHitbox.new(script.Parent)

-- Tell the module that we want to start firing the raycasts
newHitbox:HitStart()
  1. Play test/run the game in studio and move the part around. You should now see the part visualizer being drawn. This means you got it correctly!

  1. Now it's time for some hit detection goodness. Go back in the script and insert this part.
-- << Initial Setup >> 
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RaycastHitbox = require(ReplicatedStorage.RaycastHitboxV4)

-- We will construct a new hitbox for our part
local newHitbox = RaycastHitbox.new(script.Parent)

-- Makes a new event listener for raycast hits
newHitbox.OnHit:Connect(function(hit, humanoid)
    print(hit)
    humanoid:TakeDamage(50)
end)

-- Tell the module that we want to start firing the raycasts
newHitbox:HitStart()
  1. Going back into the game, the part should now damage your character exactly 50 points!

  1. You may notice that the rays won't damage your character again. This is because the module will only hit each character / detected humanoid only once. You may make it damage again by calling HitStop() which will reset its damage pool.
-- << Initial Setup >> 
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RaycastHitbox = require(ReplicatedStorage.RaycastHitboxV4)

-- We will construct a new hitbox for our part
local newHitbox = RaycastHitbox.new(script.Parent)

-- Makes a new event listener for raycast hits
newHitbox.OnHit:Connect(function(hit, humanoid)
    print(hit)
    humanoid:TakeDamage(50)
end)

-- Let's just run it on a loop, cause why not?
while true do
    newHitbox:HitStart()
    wait(5)
    newHitbox:HitStop()
    wait(5)
end

That's all!

Making a Deadly Part (Intermediate Way)

Instancing attachments via scripts can be performance hungry or doing them by hand can be tedious. Instead of using attachments to create our points, we can use the SetPoints() function to create the points for us without instancing any physical objects.

  1. I will be reusing the script from above and also leaving the attachments as it is to showcase that SetPoints can be used in combination with attachments. SetPoints is easy to use, needs a part to offset its points off of and a table of vector3 values.
-- << Initial Setup >> 
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RaycastHitbox = require(ReplicatedStorage.RaycastHitboxV4)

-- We will construct a new hitbox for our part
local newHitbox = RaycastHitbox.new(script.Parent)

-- We can use SetPoints to create new DmgPoints artificially
newHitbox:SetPoints(script.Parent, {Vector3.new(0, 3, 0), Vector3.new(-5, 3, 0), Vector3.new(5, 3, 0)})

-- Makes a new event listener for raycast hits
newHitbox.OnHit:Connect(function(hit, humanoid)
    print(hit)
    humanoid:TakeDamage(50)
end)

-- Let's just run it on a loop, cause why not?
while true do
    newHitbox:HitStart()
    wait(5)
    newHitbox:HitStop()
    wait(5)
end

Utilizing an Ignore List

Let's say you want to adapt this code to your tools and weapons. There are instances where you don't want your own weapons to damage yourself. We can ignore certain targets by using RaycastParams.

local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {MyCharacter} --- remember to define our character!
Params.FilterType = Enum.RaycastFilterType.Blacklist

-- We will construct a new hitbox for our sword
local newHitbox = RaycastHitbox.new(Sword)
newHitbox.RaycastParams = Params --- Define our RaycastParams

--- The raycasts will no longer damage your character or any objects you put in the ignore list!

Conclusion

That's it for its basic functionalities. Of course, there are much better uses for this module than just on a brick, so I look forward to what creative things you can do with it!

If you want to change any of the module's default settings, you can do so by opening up the module script and any scripts inside.

Notice
It is highly recommended that you turn off Visualizer once you are done playtesting. The part visualizer is a massive performance hog and you do not want it to be running at runtime. If you need it to run at runtime for testing purposes, consider utilizing the Hitbox.Visualizer property to turn it on/off at will.

More Examples