Getting an error, whilst script is working perfectly fine?

Im getting an error on a line, which still works even with the error, but how can i fix so that the error does not show up?

this is the line:

game.Players.PlayerAdded:Connect(onPlayerAdded(game.Players.LocalPlayer.Name))

i tried putting the whole script in a pcall but it still shows the error,
which is:

Attempt to connect failed: Passed value is not a function

the function it connects is:

local function onPlayerAdded(player)
	camera:MoveTo(game.Workspace.part1)
	camera:PointTo(game.Workspace.part2)
	camera:SetFOV(70)
end

You’re calling :Connect and inputting the output of the function which i can see that its nil.
the proper way to connect functions is as the following:

game.Players.PlayerAdded:Connect(onPlayerAdded) -- let the function be referenced instead of called

it still doesn’t work though. i’ve tried a bunch of different ways but it only works if i put :Connect(onPlayerAdded()) with ()) at the end

and it always has the error if i put ())

Thats because you’re supposed to reference the function and not calling it directly

so how do i do it and make it work aswell?

do what i have told you and whats making you not able to make this work without the ()

i did do it, but then the function doesnt seem to get called at all

can you show me how your script is structured?

sure, this is with your fix:

wait(1)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CameraUtil = require(ReplicatedStorage.CameraUtil)
local functions = CameraUtil.Functions
local shakePresets = CameraUtil.ShakePresets

local cameraInstance = workspace.CurrentCamera
local camera = CameraUtil.Init(cameraInstance)

local function onPlayerAdded(player)
	print("a")
	camera:MoveTo(game.Workspace.part1)
	camera:PointTo(game.Workspace.part2)
	camera:SetFOV(70)
end

game.Players.PlayerAdded:Connect(onPlayerAdded)




Is this a localscript? if so, you can just use the LocalPlayer instead of using PlayerAdded

it is a localscript, but what do you mean, where to use localplayer?

-- ...

local Player = game.Players.LocalPlayer

local function onPlayerAdded() -- use the Player variable
	camera:MoveTo(game.Workspace.part1)
	camera:PointTo(game.Workspace.part2)
	camera:SetFOV(70)
end

onPlayerAdded()
1 Like

thanks, is the player always gonna be loaded when the function runs?

I don’t exactly know if it does but you can do game.Loaded:Wait() to be sure

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.