Camera Scripting

Hello Developers, I’ve been having some trouble recently creating a loading screen using Camera.
Here’s the problem:

Whenever the Player spawns in, their camera.Cframe is the CamPart (which should be happening) however, the problem begins when the player Clicks the button (to leave the loading screen) their Camera doesn’t switch back to normal. Here’s the script;

function Loading_Module.Activate()
    local cam = workspace.Camera
    local camPart = workspace.cam.CamPart
    local mouse = Player:GetMouse()

    local EntrBtn_ = PlayerGui:WaitForChild("LoadingScreen").BG.ButtonFrame.TextButton
    local SizeOneFrame = EntrBtn_.Parent.Parent.SizeFrameOne
    local SizeTwoFrame = SizeOneFrame.SizeFrameTwo
    local SizeThreeFrame = SizeTwoFrame.SizeFrameThree

    repeat
        wait()
        cam.CameraType = Enum.CameraType.Scriptable
    until 
        cam.CameraType == Enum.CameraType.Scriptable
    
    local maxTilt = 10
    game:GetService("RunService").RenderStepped:Connect(function()
        cam.CFrame = camPart.CFrame * CFrame.Angles(
        math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),
        math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),
        0
        )
    end)

    EntrBtn_.MouseButton1Click:Connect(function() -- Button to exit
        cam.CameraSubject = Player.Character.Humanoid
        cam.CameraType = Enum.CameraType.Fixed
        cam.CFrame = Player.Character.PrimaryPart.CFrame
    end)
end
  • And yes I have the module required & working in the normal LocalScript. *For whatever reason, this doesn’t work. I’ve tried other ways but couldn’t do it^ (If you know anyways to possibly resolve this, please reply!)

I believe this is because you are setting the camera subject to the humanoid, instead of the humanoidrootpart.

It is normal to set the subject to the Humanoid, I believe.

1 Like

Hmm, I believe i’ve already looked into that and played around with it however I’ll look into that!

This seems wrong- it would basically just wait() once?

Apologies I got mixed up, ignore my last post, try checking to see if the players character exists before setting the cameras cframe. Where are you spawning the character?

The player just spawns into the game, (I don’t have a spawn location)

I’ll see what removing does to that!

You should replace it with just cam.CameraType = Enum.CameraType.Scriptable, since I think you would want that when you activate your loading screen.

it’s supposed to be Custom enum type by default, ya goof

I’ll see about that, editing it currently.

I’ll try custom but I wouldn’t of made this post without trying all of those options. Unless I didn’t but I thought I did-

@bluebxrrybot I tried replacing the repeat with just the one line.
Same with @Skoliage, both ways didn’t resolve anything. It would seem like that would of done something but it appears it doesn’t even make a change.

did you make sure that that MouseButton1Click is actually activating?

Yep, it prints “Hello” each time it’s clicked.

I optimize the code a little, it should be easier to read and debug.

local ui = game.Players.LocalPlayer.PlayerGui:WaitForChild("LoadingScreen"):WaitForChild("BG")
local mouse = Player:GetMouse()
local camerapart = workspace.cam.CamPart
local camera = workspace.CurrentCamera

Loading_Module.Activated = false

function Loading_Module.Activate()
	Loading_Module.Activated = true

	camera.CameraType = Enum.CameraType.Scriptable
end

function Loading_Module.Deactivate()
	if Loading_Module.Activated then
		camera.CameraSubject = Player.Character.Humanoid
		camera.CameraType = Enum.CameraType.Fixed
		camera.CFrame = Player.Character.PrimaryPart.CFrame
		Loading_Module.Activated = false
	end
end

-- Deactivate Loading Screen

ui.ButtonFrame.TextButton.MouseButton1Click:Connect(Loading_Module.Deactivate)

-- Camera Mouse Tilt

local maxTilt = 10
game:GetService("RunService").RenderStepped:Connect(function()
	if Loading_Module.Activated then
		camera.CFrame = camerapart.CFrame * CFrame.Angles(
			math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),
			math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),
			0
		)
		camera.CameraType = Enum.CameraType.Scriptable
	end
end)

goofy ahh local cam should be game.Workspace.CurrentCamera

1 Like

I’ll try your way and see if it works!

Maybe it has to be? I’ll try that as well.

@Skoliage Doesn’t seem to be the problem :skull: