Error: Attempt to connect failed: Passed value is not a function

I am trying to pass a parameter from an event to a function but it is giving me a couple of errors including

Attempt to connect failed: Passed value is not a function

:arrow_forward: attempt to call a nil value (x306)

Code:

local plr = game.Players.LocalPlayer
local camera = workspace.CurrentCamera

local RepS = game:GetService("ReplicatedStorage")
local RS = game:GetService("RunService")
local BallModule = require(RepS:WaitForChild("BallModule"))

local function updateCameraPos(ball, camera)
	print(ball) -- These print absolutely fine.
	print(camera)
end
plr.CharacterAdded:Connect(function(char)

	local ball = BallModule.CreateBall()
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CameraSubject = ball
	camera.CFrame = ball.CFrame
	wait(1)
	
	-- Where things get murky..
	RS.RenderStepped:Connect(updateCameraPos(ball, camera))
end)

Output:

  20:00:46.000  Part  -  Client - LocalScript:9
  20:00:46.001  Camera  -  Client - LocalScript:10
  20:00:46.001  Attempt to connect failed: Passed value is not a function  -  Studio
  20:00:46.001  Stack Begin  -  Studio
  20:00:46.001  Script 'Players.DisastrousFate.PlayerScripts.LocalScript', Line 19  -  Studio - LocalScript:19
  20:00:46.001  Stack End  -  Studio
  20:00:46.017   ▶ attempt to call a nil value (x306)  -  Client
  20:00:51.374  Disconnect from ::ffff:127.0.0.1|55637  -  Studio
  20:05:24.734  Baseplate auto-recovery file was created  -  Studio - C:/Users/alexh/Documents/ROBLOX/AutoSaves

What is the cause of this and how can i fix it?

does it affect your script in any way?

So instead of passing a function with brackets in (like how you would call it), :Connect expects a function to be passed without brackets.
Like so:

RS.RenderStepped:Connect(updateCameraPos)

Now, if you want to add arguments, you will have to add them to the Connect function well technically it’s a method as its a function of an instance :wink: instead.
Just like this:

RS.RenderStepped:Connect(updateCameraPos, ball, camera)

Now the function passed to Connect (technically known as a callback) will run with the args ball and camera.

4 Likes

clearly it is causing it to not run as it errors

this line no good

should look like

RenderStepped:Connect(updateCameraPos)

wait you could pass the args like that?

indeed my programmer friend more text sorry

1 Like

Thanks For The Knowledge, This will be useful sometime

1 Like

Never tried it, but from my experience from doing functions with params in pcalls, you could probably do:

RS.RenderStepped:Connect(updateCameraPos, ball, camera)

glad i could help! yet again more chars for limit

you can in fact! i explained it in my answer above

wait but where would delta time be…

RS.RenderStepped:Connect(updateCameraPos, ball, camera)

Should look like this since the arguments are necessary (otherwise nil will be printed).

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local camera = workspace.CurrentCamera

local RepS = game:GetService("ReplicatedStorage")
local RS = game:GetService("RunService")
local BallModule = require(RepS:WaitForChild("BallModule"))

local function updateCameraPos(ball, camera)
	print(ball) -- These print absolutely fine.
	print(camera)
end

local ball = BallModule.CreateBall()
camera.CameraType = Enum.CameraType.Scriptable
camera.CameraSubject = ball
camera.CFrame = ball.CFrame
task.wait(1)

RS.RenderStepped:Connect(updateCameraPos, ball, camera)

You don’t need the CharacterAdded event by the way, as long as the local script is placed inside the StarterCharacterScripts folder it’ll execute each time the player’s character reloads/is added/respawns etc.

I’ve also replaced wait() for task.wait() for accuracy.

You’re telling Roblox to connect with nil, because putting parenthesis runs the function, and since the function returns nothing, it errors because it thought the function would return another function to actually connect to RenderStepped. You’ll have to replace this line with:

RS.RenderStepped:Connect(function()
	updateCameraPos(ball, camera)
end)

Thanks alot, i never knew that!

1 Like