Can someone explain to me why, when I attempt to do print(table.unpack(TouchedTable)) after the hitbox.touched event, it prints in console, simply ’ - '? I am not sure how this is happening.
local HitboxModule = {}
function HitboxModule.GenerateHitbox(Origin, Owner, Size, Orientation, Lifetime, HitMultiple)
local Hitbox = Instance.new("Part", workspace.Debris)
Hitbox.Anchored = true
Hitbox.CanCollide = false
Hitbox.Transparency = 0.5
Hitbox.Position = Origin.Position
Hitbox.CastShadow = false
Hitbox.Size = Size
Hitbox.Orientation = Orientation
Hitbox.Color = Color3.fromRGB(255,50,50)
local CanTouch = true
local TouchedTable = {}
Hitbox.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild('Humanoid') and Hit.Parent ~= Owner and not table.find(TouchedTable, Hit.Parent) and CanTouch then
if HitMultiple then
CanTouch = true
else
CanTouch = false
end
table.insert(TouchedTable, Hit.Parent)
Hitbox:Destroy()
end
end)
print(table.unpack(TouchedTable))
game.Debris:AddItem(Hitbox, Lifetime)
return TouchedTable
end
return HitboxModule
This does not seem to do anything. However if I attempt to print the table from within the Hit event, it oddly prints out what would be the correct output. Something else worthy of note is that it also works as intended when I only insert Hit (not Hit.Parent) into the table
Instead of waiting until anything is hit, the module returns and prints the touched table right away, NOT waiting for anything to touch it.
Either you could:
A: Use a bindable event instead of a return statement. Fire it from the module to the sever, passing Hit.Parent
B: Yield the function until any collision has been registered and only then return the Hit.Parent. Lets say… by waiting UNTIL the hitbox does not exist anymore.
( In your place, i’d probably go with the first option )