Printing out contents of a table gives something unexpected, following a touched event

image
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

mind i ask why you would need to unpack the table to print it out? If you want to print out all of the contents then do a for loop.

Otherwise, if you ONLY need to see the table’s contents IN STUDIO you can just print out the table itself and open it up in the console!

Even when I loop through the table, it doesn’t print anything. I’m not unpacking it for any other purposes other than debugging.

For table.insert(TouchedTable, Hit.Parent), try changing the second argument to Hit.Parent.Name so it becomes a string then try printing

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

Okay, i think i found the problem!

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 )

1 Like

well that is unexpected indeed hm

both actually may have potential to work

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.