Camera Manipulation Script

My script isn’t working and I have no idea why, this is supposed to run at the beginning of the game, no errors or anything. Anyone know why? (I tested it and the print runs)

local TweenService = game:GetService("TweenService")
local Camera = game.Workspace.CurrentCamera
local CameraPosition = game.Workspace.Beginning

Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = CameraPosition.CFrame
print("Done")
1 Like

It’s a local script, right? 30 char

1 Like

Yeah a local script in starter gui.

You need to include something like a wait(2) at the very beginning of the script since running a script right when the server starts can sometimes result in the script erroring out, because sometimes the script will run BEFORE the server is actually fully loaded, so things like stuff in workspace may not be fully loaded in yet.

Edit: It may take longer than 2 seconds to load, it all depends on your game’s size and the client’s speed.

3 Likes

Isn’t there a event whenever the client loads? Can’t remember it off the top of my head

Typically for scripts that I have in ServerScriptService, I use:

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character) 
            --Whatever you need to do here
    end)
end)

I’ve never used it in StarterGui before, but you can try it out and see if that will fix your problem.

1 Like

When your character loads into the game, I’m pretty sure that the camera is reset so that it focuses on the character. Either wait for the character to load using .CharacterAdded or use .RenderStepped to overwrite the camera on each frame until you no longer need it.

1 Like

Ok I will try it out, I swear I thought there was an event to check if all the assets on the client have loaded, maybe I am just imagining lol.

1 Like

The one problem is that every time a player/character loads, the camera will change

You can run characteradded on the client, that’s all you should need, not any other hacky method such as wait() or characterappearanceloaded

1 Like

Well there is a game:IsLoaded() function.

repeat wait() until Cam.CameraType == Enum.CameraType.Scriptable

Should help

1 Like

That was what I was thinking of, but that wouldn’t work so never mind lol.

I don’t believe you can use playeradded or characteradded functions in a local script, and have them function the same way, since the script will load after the player, causing it to not fire until their character is loaded a second time, or until another player loads the game.

Now, you could achieve it with just using waitforchild on a part of the character, or any methods listed above.

1 Like

The wait() works, but there is no way to predict how long it takes for the player to get in.

You can always detect when your character is added on the client, even if you cant due to yielding (wait functions in the script prior to the line being run) you can always check if the character already is in game and run a given function.

@Bloxrrey use Player.CharacterAdded in a localscript

This did not work :confused: unless I did it incorrectly.

I did that and it did not work, same thing no errors.

Did you set it up like this?

local TweenService = game:GetService("TweenService")
local Camera = game.Workspace.CurrentCamera
local CameraPosition = game.Workspace.Beginning

Camera.CameraType = Enum.CameraType.Scriptable; 
repeat wait() until Cam.CameraType == Enum.CameraType.Scriptable;
Camera.CFrame = CameraPosition.CFrame
print("Done")

No, let me try that out :slight_smile:

Edit: did not work still