Passing a function with a table parameter to a module

  1. What do I want to achieve

    Fix this issue, detect the table in the module script
  2. What solutions have I tried
    Printing
    image
  3. Heres my code

Module Script

function module.PartsBoundinBox(player, cFrame, size, overlapParams, func, ignoreSelf)
	print("Used")
		local character = player.Character
		local humanoid = character.Humanoid
		local hrp = character.HumanoidRootPart
		
		local hitbox = workspace:GetPartBoundsInBox(cFrame, size)
		local alrHit = {}
		
		for i,v in pairs(hitbox) do
				if ignoreSelf == true then
				if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= player.Character then
						local target = v.Parent
						local targhum = target.Humanoid
						local targhrp = target.HumanoidRootPart
						local info = {
							targ = target,
						}
						
						func(info)
						print("Im a sigma")
				end
				
				elseif v.Parent:FindFirstChild("Humanoid") and ignoreSelf == false then
					local target = v.Parent
					local targhum = target.Humanoid
					local targhrp = target.HumanoidRootPart
					local info = {
					targ = target,
					}
					print(info)
					func(info)
					print("Reqiuem")
			end
		end
		
end

Server Script

game.Players.PlayerAdded:Connect(function(player)
		
		player.CharacterAdded:Connect(function(character)
			task.wait(1)
		local Hitbox = require(game.ServerScriptService.Modules.Hitbox)
		local function Hit(info)
				print(info)
		end
		Hitbox.PartsBoundinBox(player, player.Character.HumanoidRootPart.CFrame, Vector3.new(10,10,10),{}, Hit(info), false)
	end)
end)

I assume that this line is the erroring line

There’s a couple things that’s wrong with this. One, you’re calling this function (you added parenthesis in front of the function) in your method. What that means is that the “Hit” function is gonna be called at the same time as the PartsBoundinBox function is called + before the “func(info)” line is called (didn’t expect that huh).

Secondly, you’re called func(info) in your moduleScript like the function (AKA callback / Hit) provided in the parameter IS A function. It’s a value

ok I just added all of that jsut in case you wanted to know what’s wrong but basically just change it to this:

• The actual code if you don’t care about why it doesn’t work

Hitbox.PartsBoundinBox(player, player.Character.HumanoidRootPart.CFrame, Vector3.new(10,10,10),{}, Hit, false)

I just got rid of the () / parenthesis after “Hit” which makes it so that the function isn’t called and the parameter is an actual valid function and not a return value (your “Hit” function doesn’t return anything so it defaults to nil by the way)

yea i kinda skimmed over what u said i just changed the function into a variable thingy
image

1 Like

Did it work though? ya really just need to remove the parenthesis around the Hit call in the GetPArtsinblahblahblah call

Yes, it did infact work sigma a aa

1 Like