Help with 'Passed value is not a function'

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a system where I can pass an object and have it run a function based upon the value passed/

  2. What is the issue? Include screenshots / videos if possible!
    Error is “Passed value is not a function”

Main Script,

local module = require(script.ModuleScript)
local items = game.Workspace:WaitForChild('ScriptableObjects')
local DisappearObjects = items.DisappearObjects:GetChildren()


module.objConnection(DisappearObjects, module.Disappear)

Module Script,

local module = {}

function module.Disappear(obj)
	obj.CanCollide = false
	wait(3)
	obj.CanCollide = true
end

function module.objConnection(obj, fun)
	for i,v in pairs(obj) do
		v.Touched:Connect(fun(v))
	end
end

return module

You’re trying to call the function when passing the function off as an argument in the event.

Not sure if this is causing the error but you should remove the arguments (the (v) part) as it is not needed. Just put the function name

Edit: SORRY WRONG REPLY i mean to reply to the op

v.Touched:Connect(function()
    fun(v)
end)

By just doing fun(v) in the :Connect()ion, you’re calling the function instantly. You need to wrap it within another higher order function in order to use it as a callback.

1 Like