How to make GetPartBoundsInBox() keep listening to see if something is in the part?

I want to make GetPartBoundsInBox() keep listening to see if something is there like a .Touched event would.

People I have asked told me to use while loops. I have tried many ways to use them but it doesn’t seem to work. So I’d like some code examples if possible.

Hitbox is welded to the HumanoidRootPart so the hitbox would work while you are moving around.

Here is the script

module.StandardHit = function(Hitbox,Character,Damage,StunTime)
    
    local HumanoidRoot = Character.HumanoidRootPart
    
    local HitList = {}
    
    local overlapParams = OverlapParams.new()
    local HitContents = game.Workspace:GetPartBoundsInBox(Hitbox.CFrame,Hitbox.Size,overlapParams)

    for i, v in pairs(HitContents) do
        if v.Parent ~= Character then
            local EnemyCharacter = v.Parent
            local EnemyHumanoid = EnemyCharacter:FindFirstChild("Humanoid")

            if EnemyHumanoid then
                if not HitList[EnemyHumanoid] then
                    HitList[EnemyHumanoid] = true
                        DamageModule.Attacked(Character,EnemyHumanoid,Damage,StunTime)
                end
            end
        end
    end
end

Thanks!

One way to continuously update the HitContents for any changes is to put the contents of your for loop inside a while loop that continuously checks for changes and updates HitContents if there are any.

module.StandardHit = function(Hitbox,Character,Damage,StunTime)

local HumanoidRoot = Character.HumanoidRootPart
local HitList = {}
local overlapParams = OverlapParams.new()
local HitContents = game.Workspace:GetPartBoundsInBox(Hitbox.CFrame,Hitbox.Size,overlapParams)

while wait() do -- continuously check for changes
    local newContents = game.Workspace:GetPartBoundsInBox(Hitbox.CFrame,Hitbox.Size,overlapParams)
    
    if newContents ~= HitContents then -- if there are changes
        HitContents = newContents -- update HitContents
        for i, v in pairs(HitContents) do -- loop through the new HitContents
            if v.Parent ~= Character then
                local EnemyCharacter = v.Parent
                local EnemyHumanoid = EnemyCharacter:FindFirstChild("Humanoid")
                if EnemyHumanoid then
                    if not HitList[EnemyHumanoid] then
                        HitList[EnemyHumanoid] = true
                            DamageModule.Attacked(Character,EnemyHumanoid,Damage,StunTime)
                    end
                end
            end
        end
    end
end

end

1 Like

If your trying to do something like .touched with it constantly running after .touched is fired until nothing is touching the part anymore, then you could try a script I made for a bullet :

local debounce = false
script.Parent.Touched:Connect(function()
    if debounce == false then
        debounce = true
        local notValidTarget = true
        repeat
            notValidTarget = true
            for I,hit in script.Parent:GetPartBoundsInBox(your parameters here) do
                if hit.Name == “enemy” then
                    notValidTarget = false
                    —do something
                end
            end
            task.wait()
        until notValidTarget
        debounce = false
    end
end)

The point of the script was to avoid using .Touched because it has many issues. I’ve figured something out where I used RunService to actively update the hitboxes.

1 Like