Issue with camera location after player joining

Hello. I’ve made a simple localscript inside the StarterGUI with the following code:

local cam = workspace.CurrentCamera
wait(2)
cam.CameraType = Enum.CameraType.Scriptable
cam.CFrame = game.Workspace:FindFirstChild("MenuCameraPart").CFrame

However, it seems to work only some of the time?
Sometimes the camera goes to where its supposed to, other times the camera stays stationary at the players location but still doesnt follow the player like a normal camera.

Any help is greatly appreciated.

1 Like

The CurrentCamera property refreshes on the client everytime the player’s character is loaded I believe, so the CurrentCamera when a player joins won’t be the same when their character loads. Instead of adding an arbitrary wait(), use player.CharacterAdded:Wait() and see if that makes a difference.

1 Like

Now the camera seems to not change at all, just normal camera.

Here is the updated script.

local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
plr.CharacterAdded:Wait()
cam.CameraType = Enum.CameraType.Scriptable
cam.CFrame = game.Workspace:FindFirstChild("MenuCameraPart").CFrame
1 Like

Sorry, if this is being tested in Studio, sometimes Studio can be a bit wonky with the order things are done in, and LocalScripts can execute a lot later then expected. Running this in my own Studio Instance, it gets stuck on waiting for your character to be added since the LocalScript runs AFTER the game loads your character. An easy way to get around this is to just make a loop until the character loads. Usually you should avoid doing this but in this situation it should be fine.

local plr = game.Players.LocalPlayer
repeat task.wait() until plr.Character
local cam = workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Scriptable
cam.CFrame = game.Workspace:FindFirstChild("MenuCameraPart").CFrame
1 Like

This still doesnt seem to put the camera in the desired location.

1 Like

It worked fine for me, are you not getting any errors? Maybe it’s an issue with the part and not the script

1 Like

Could be.


image

Anything out of the ordinary here?

1 Like

From what I read, you want to make the camera focus a menu location.
Try changing

cam.CFrame = game.Workspace:FindFirstChild("MenuCameraPart").CFrame

to

cam.CameraSubject = workspace:FindFirstChild("MenuCameraPart")

You can additionally make the min/max zoom of a camera be a zero

Also, beware of StreamingEnabled, as it may not load some parts if your character is too far.

1 Like

Hey rasm,

This is the current code I’ve been using without problems.

	cam.CameraType = Enum.CameraType.Scriptable
	cam.CameraSubject = game.Workspace:FindFirstChild("MenuCameraPart")
	camera.CFrame = game.Workspace:FindFirstChild("MenuCameraPart").CFrame

I believe your problem was the fact you were using FindFirstChild this is due to the fact your code running before that part exists, using WaitForChild will stop your code until that part exists. I hope this helps!

1 Like

Like this?

local plr = game.Players.LocalPlayer
repeat task.wait() until plr.Character
local cam = workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Scriptable
cam.CameraSubject = workspace:FindFirstChild("MenuCameraPart")

cuz that doesnt seem to work.

1 Like

This seems to work. Thank you!

1 Like

It works.

However I am now running into a problem with resetting the camera back to the player after clicking play.

Code:

script.Parent.MouseButton1Click:Connect(function()
	game.Players.LocalPlayer.PlayerGui.MainMenu.Enabled = false
	workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
	game.SoundService.MouseClick:Play()
end)
1 Like

Hey again,

I’m glad my previous response helped out, and I’ve noticed your current problem. You’ve forgotten to add cam.CameraSubject

Heres how you’d set it back

cam.CameraType = Enum.CameraType.Custom
cam.CameraSubject = plr.Character:WaitForChild('Humanoid')

There most always will be a Humanoid but its always safe to wait for it. also do make sure to check if the player exists, else if the player presses play as they’ve reset or anything odd. its going to throw an error as Character doesn’t exist.

One thing to note if you set the CameraSubject to anything other than the humanoid in the player the character wont apply AutoRotate and zooming in to first person will also not automatically move the character. Hence why the best bet is Humanoid if your looking for normal gameplay, but feel free to experiment!

Superb!

It works very well. Thank you very much!

1 Like

Alr now im confused. I touched nothing, and it suddenly doesnt work anymore.

What happens is:
Click play and then the camera goes to 0, 0, 0 position. I dont know why, I dont have any scripts interfering with the script. It worked, then stopped working.

local plr = game.Players.LocalPlayer
repeat task.wait() until plr.Character
local cam = workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Scriptable
cam.CameraSubject = game.Workspace:FindFirstChild("MenuCameraPart")
cam.CFrame = game.Workspace:FindFirstChild("MenuCameraPart").CFrame

Seems you have touched something. As I’ve stated before you should replace FindFirstChild with WaitForChild

huh i literally copy/pasted your script, maybe i accidently did it in play mode and it didnt save. Thanks tho, ill try this again.

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