Sometimes it would run fine, but other times it just doesn’t seem to run at all. I’m assuming this is due to the camera not being fully loaded, but I may be wrong.
I think the camera gets set to custom again once your character is loaded. So maybe wait for the character to load? I’m not sure and i might be very wrong.
Your camera is loaded regardless when joining a game, there is no reliable way to detect when it is added.
Typically when you load in, the server must initiate first (in most MMORPG-oriented platforms such as ROBLOX at least) which means most server scripts in server script service will run before a player joins. However, there is no specific order of what is loaded first and what not.
Straight’s way might or might not work, it is not the most convenient way because wait() is unreliable, and even if you replace it with RunService:RenderStepped:Connect() or even task.wait() it’ll result in pooling, no guarantee that it’ll run or even be accurate at all.
Try prioritizing the camera in your client first by using content provider service. Use in a local script in ReplicatedFirst. Sample:
local Content_Provider = game:GetService("ContentProvider")
Content_Provider:PreloadAsync(workspace.Camera)
--- Rest of your code
The camera is always loaded before anything else. What’s more likely the case is that when the player’s character spawns, it overwrites whatever camera properties you have set. Instead, I would put that into a CharacterAdded event.
I don’t think that’s necessarily the case with camera if you set the camera type to scriptable right up front, at least not in a test that I just performed for the sake of this topic.
I’m going to emphasize the word “necessarily” because in the actual world it can be iffy when it comes to a platform with no specific loading sequence defined.
On another test after realizing I turned off Character Autoload, I think you are right.
I still think replicated first is reliable when it comes to prioritizing the script loading, this is what I have done to bypass player character loading which can vary based on computer power as some players take one second to load their character while some others with lower specs or bigger map.
-- LOCAL SCRIPT
local CP = game.ContentProvider
CP:PreloadAsync({workspace.Camera})
workspace.Camera.CameraType = Enum.CameraType.Scriptable
workspace.CurrentCamera.CFrame = CFrame.new(0,0,0)
-- More code
task.wait(10) -- test environment
game.ReplicatedStorage.LoadChar:FireServer('Ready')
I haven’t tested much with preloading, so that’s good to know about. However, from my experience, it seems that Gui won’t load until you use LoadCharacter(). This isn’t ideal when you want to have a title or loading screen unless you decide to use 3D Gui or something. This is why I simply prefer to either use CharacterAdded or put things in StarterCharacterScripts.
This happens to me too. I made a local script and put it inside StarterPlayerScripts and at first it worked perfectly fine, but the more things and features, this started occur more and more often until now, with the camera rarely working properly. I don’t connect this to adding more features, just putting it out there and hopping someone will find a way to fix it.
I ended up using @Jas_nRuski’s script minus the character loading part, and put it inside StarterCharacterScripts instead (because of what @MightyDantheman said about GUI’s). If anyone else reading was wondering, put this inside StarterCharacterScripts:
local ContentProvider = game:GetService("ContentProvider")
ContentProvider:PreloadAsync({workspace.Camera})
local camera = workspace.CurrentCamera
-- Whatever you want to do with the camera
@morisanero This should work because PreloadAsync will not run any code after it until done (Async)