#Learning hitbox scripting

90 messages · Page 1 of 1 (latest)

fiery moat
#

So I am looking at some tutorials but I just can't seem to get this script to report to console which parts of the rig touch the hitbox.

local ourPlr = workspace.Enemy
local ourHumanoidRootPart = ourPlr:FindFirstChild("HumanoidRootPart")

local overlapParams = OverlapParams.new()
local hitContents = workspace:GetPartBoundsInBox(ourHumanoidRootPart.CFrame*CFrame.new(0,0,-7),Vector3.new(6,6,4), overlapParams)

print(hitContents)
#

Heres a image showing the size of the hitbox

#

(the part doesn't mean anything its just an example)

#

Not sure if it matters but this is the location of the script

#

the part being the brown brick

worn swift
#

that could work-

upper fern
#

What's it printing currently?

#

because I think GetPartBoundsInBox() prints a table, so you'd just do a for i,v in pairs() loop

mild dragon
#

@fiery moat what's it printing?

mild dragon
fiery moat
fiery moat
#

when i move the rig into the field it doesn't do anything

upper fern
#

are you doing that inside a touched function?

#

or only once?

fiery moat
#

So far i just have the script

upper fern
#

okay u gotta do that once they touch the part

#

and maybe have a debounce if u want

#

make a variable for the part

fiery moat
#

Ok

#

ill try and add that when i get back from town

#

if you want ill ping you with the result

upper fern
#

I'll write u something

fiery moat
#

tysm btw

#

I'm trying to learn coding for a game i'm trying to make

#

so im very new

upper fern
#

No problem and nice job

#
local Part = script.Parent
local DBs = {}
local DBTime = 0.1

local function Hitbox(Char)
    local overlapParams = OverlapParams.new()
    
    local CF,Size = Char:GetBoundingBox()
    local hitContents = game.Workspace:GetPartBoundsInBox(CF,Size,overlapParams)
    for i,v in pairs(hitContents) do
        print(v.Name.." has touched the part") -- RightHand has touched the part
    end
end

Part.Touched:Connect(function(hit)
    if not hit.Parent:FindFirstChild("Humanoid") then return end
    if table.find(DBs,hit.Parent) ~= nil then return end
    table.insert(DBs,hit.Parent)
    Hitbox(hit.Parent)
    task.wait(DBTime)
    table.remove(DBs,DBs[hit.Parent])
end)```
There u go something like that
mild dragon
fiery moat
#

I haven't learned how to repeat a script yet

#

(i'm very new)

mild dragon
#

What you can do is either connect it to a while loop or what I would recommend is doing this:

Part.Touched:Connect(function()
   local hitContents =            workspace:GetPartBoundsInBox(ourHumanoidRootPart.CFrame*CFrame.new(0,0,-7),Vector3.new(6,6,4), overlapParams)

  print(hitContents)
end)
mild dragon
fiery moat
upper fern
valid sequoiaBOT
#

studio** You are now Level 3! **studio

fiery moat
#

let me add comments to it and see if i can get it right

#

kinda like a test

upper fern
#

Alright

fiery moat
#
local Part = script.Parent -- This makes the object the script is in the parent
local DBs = {} -- not sure
local DBTime = 0.1 -- the time of the DB

local function Hitbox(Char) -- creates the Hitbox function
    local overlapParams = OverlapParams.new() -- unsure
    
    local CF,Size = Char:GetBoundingBox() -- unsure
    local hitContents = game.Workspace:GetPartBoundsInBox(CF,Size,overlapParams) -- not fully sure
    for i,v in pairs(hitContents) do
        print(v.Name.." has touched the part") -- RightHand has touched the part prints in console what happened
    end
end

Part.Touched:Connect(function(hit)
    if not hit.Parent:FindFirstChild("Humanoid") then return end
    if table.find(DBs,hit.Parent) ~= nil then return end
    table.insert(DBs,hit.Parent)
    Hitbox(hit.Parent)
    task.wait(DBTime)
    table.remove(DBs,DBs[hit.Parent])
end)
#

I'm assuming you can create another script to take the reported parts and do something with them

#

for example apply damage

#

or maybe change scale

upper fern
#

you can do it in this script

#

if you want to make the enemy take damage or something you'd just add this

fiery moat
#

My end goal after I understand all of this is to create a server system that gives every player a hitbox for registering attacks

#

im a long way from that though

#

and i need to learn all this first

upper fern
#
local Part = script.Parent
local DBs = {}
local DBTime = 0.1
local Damage = 10

local function Hitbox(Char)
    local overlapParams = OverlapParams.new()
    
    local CF,Size = Char:GetBoundingBox()
    local hitContents = game.Workspace:GetPartBoundsInBox(CF,Size,overlapParams)
    for i,v in pairs(hitContents) do
        print(v.Name.." has touched the part") -- RightHand has touched the part
        v.Parent.Humanoid.Health -= Damage
        return
    end
end

Part.Touched:Connect(function(hit)
    if not hit.Parent:FindFirstChild("Humanoid") then return end
    if table.find(DBs,hit.Parent) ~= nil then return end
    table.insert(DBs,hit.Parent)
    Hitbox(hit.Parent)
    task.wait(DBTime)
    table.remove(DBs,DBs[hit.Parent])
end)```
#

thats all i added

upper fern
fiery moat
upper fern
#

Mhm

fiery moat
#

wow thats not that bad

upper fern
#

depending on what you put at the variable top

fiery moat
#

I think i'm starting to slowly grasp it

upper fern
#

Yeah, if this was me actually making a hitbox system, I'd make a modulescript and call it to get the actual character then deal damage.

fiery moat
#

Right now I'm learning the basics and asking for help when something doesn't work

#

Luckily people here are very helpful and nice so it's helping me a lot

#

Ty all bte

#

Btw

mild dragon
# fiery moat This works but I need to dissect this so i can understand how to make it myself
  1. The .Touched event: this is an event that fires whenever a part collides with something else because of physics. This is how you can detect when something, for example, a hitbox is touched. the :Connect may be a little confusing, but it just means that whenever that part is touched, the code inside the connected function will run. One more thing: the touched event helpfully provides the object that touched your part in its parameters, so you can check that part. Here's an example of it:
local Part = script.Parent -- the path to the Part you want to check if its touched. In this case, put the script inside the part

Part.Touched:Connect(function(otherPart) -- run the code inside this function whenever the part is touched. Also, get the otherPart object that touched the part
  print(otherPart.Name) -- output the name of the part that touched
end)
  1. workspace:GetPartsInBounds() - I would personally recommend workspace:GetPartsInPart(object) as you don't have do specify the bounds and can just use the hitbox part. Basically, this returns a table of all the parts that are inside of that part. Here's an example
local part = script.Parent -- you know the drill

local touching_parts = workspace:GetPartsInPart(part) -- set touching_parts equal the parts inside the hitbox
print(touching_parts) -- output the touching parts, it should look something like this(if there are any parts)
--[[
{
[1] = random_part,
[2] = another_part
]]
mild dragon
fiery moat
mild dragon
#

I'm pretty sure

fiery moat
#

I.e a invisible hitbox brick attached to a torso or something like that

mild dragon
upper fern
#

yeah it does

fiery moat
#

Wow this helps a lot like a lot lot

valid sequoiaBOT
#

studio** You are now Level 4! **studio

fiery moat
#

I see how I can do a very simple hitbox now

#

Tysm

fiery moat
#

I can think of many uses not even hitbox related

#

Seems like a very powerful tool

upper fern
#

yea u can do lots with it

#

teleportation,killbricks and more

fiery moat
#

Ok when I get back from town I will try and apply what I have learned

#

Brb

upper fern
#

Good luck :)

mild dragon
#

if you're making things like bullets with it

fiery moat
#

Nothing as fast as a bullet