How to fix a 2D camera that sometime won't work?!

The Problem
I am working on a basic 2D based game and the script is sometimes not going into 2D instead it stays on 3D.
And when you die the camera will be focusing on nothing and not the character.

The Error

The Script

It would be helpful if you can help me because it’s one big of a problem for a 2D game that I can’t fix.

Assign these values using :WaitForChild and when the character actually loads.

The script isn’t run again when the character respawns. You can change that using the CharacterAdded event.

1 Like

If Instance:FindFirstChild cannot find a child with a given name, it returns nil. So the issue seems to be, it was not loaded when :FindFirstChild was called.

You can use :WaitForChild to wait for a child.

local rootPart = character:WaitForChild("HumanoidRootPart")

Also, repeat wait() until player.Character?

Don’t reinvent the wheel, player.CharacterAdded exists, and you can :Wait() for it to fire.

local character = player.Character or player.CharacterAdded:Wait() -- obliterate that loop u had pls

Basically, if the character is loaded already, character is assigned to player.Character, if not, that means it’s nil and isn’t loaded yet, so :Wait() for player.CharacterAdded to fire.

4 Likes

Thanks a lot, @incapaxx and @UltimateRaheem. This is helpful.

Okey, never mind! I had the answer to my question

1 Like