Universal Module Touched Function Problem

So I made this functon in a module a script that takes a part and how much damage you want to do when it is touched but it keeps printing “attempt to index nil with connect”

local hit  = {}

local debounce = false
function hit.DamagePart(part,damage)
	part.Touched:Connect(function(hit)
		if hit and hit:FindFirstChild("Humanoid") and hit.Parent ~= "Toriel" and not debounce then
			debounce = true
			local hum = hit:FindFirstChild("Humanoid")
			hum:TakeDamage(damage)
			wait(1)
			debounce = false
		end
	end)
end


return hit

That means part is nil. Try debugging the module and see what values it’s receiving.

The script doesn’t know what “part” is, as you passed it in as a parameter in the function. You need to call the function later on in the script, therefore you can define what the part actually is.

-- in the ModuleScript
local hit = {}

local debounce = false
function hit.DamagePart(part,damage)
	part.Touched:Connect(function(hit)
		if hit and hit:FindFirstChild("Humanoid") and hit.Parent ~= "Toriel" and not debounce then
			debounce = true
			local hum = hit:FindFirstChild("Humanoid")
			hum:TakeDamage(damage)
			wait(1)
			debounce = false
		end
	end)
end

return hit

-- in the server-sided script where you are calling the functions in the module
hit.DamagePart(game.Workspace.Part,50) -- both arguments are changeable due to the ModuleScript

This is not the most convenient way if you are going to have a lot of DamageParts, and you should probably look into using for loops to make it more convenient, as it can be annoying to call this function for each part.