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
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
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 instead.
Just like this:
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: