Ciao,
I have a loading script where the player camera will be pointing a position until loading finished.
The problem is the following;
The camera type changes to scriptable, but the cframe isn’t applied. It is applied howhere when i put a wait(0.1) before.
Is the problem the player not loaded yet? or the camera? or the part with the cframe?
Part of the script that changes the camera:
local camera = workspace.CurrentCamera
local NewCamera = UI1.Camera
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = NewCamera.CFrame
repeat wait() until game.Players.LocalPlayer ~= nil
repeat wait() until game.Players.LocalPlayer.Character ~= nil
-- this is waits when player and player character is loaded
repeat
camera.CameraType = Enum.CameraType.Scriptable
until camera.CameraType == Enum.CameraType.Scriptable
repeat
camera.CFrame = NewCamera.CFrame
until camera.CFrame == NewCamera.CFrame
-- makes loop for camera type and CFrame and ends if it's done
if it is a localscript you don’t need to wait for player and also you can do game.Players.CharacterAdded:Wait() (and if you do it your way make sure to use task.wait() instead of wait())
I know that the solution has already been given, but I just want to clarify.
I said that you have to wait for the character to load because when the character first spawns, workspace.CurrentCamera’s CameraType gets set to Custom, and it will override your code if you don’t wait for that to happen. So, you really just need to wait for that property to change, then set it to Scriptable and change the Camera’s CFrame.
local camera = workspace.CurrentCamera
camera:GetPropertyChangedSignal("CameraType"):Wait()
-- Then set the CameraType and CFrame here.
local camera = workspace.CurrentCamera
print("First:", camera.CameraType.Name, camera.CameraSubject)
camera:GetPropertyChangedSignal("CameraType"):Connect(function()
print("Type changed to "..camera.CameraType.Name)
end)
camera:GetPropertyChangedSignal("CameraSubject"):Connect(function()
print("Subject changed to "..camera.CameraSubject.Name)
end)
[DEFAULT OUTPUT]
First: Fixed nil
Subject changed to Humanoid
Type changed to Custom
Humanoid is going to be applied right after it comes to existence. Moments later, camera type is going to switch.
If we add a server script to disable autmatic character loading, the type would remain Fixed and subject would remain nil until the manual spawn.
The scripts in StarterCharacterScripts, StarterPack and StarterGui. By default, anything in those containers gets cloned into respective Character, Backpack and PlayerGui (unless manually specified otherwise).
local player = game:GetService("Players").LocalPlayer
local camera = workspace.CurrentCamera
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
if camera.CameraType == Enum.CameraType.Fixed then
camera:GetPropertyChangedSignal("CameraType"):Wait()
end
camera.CameraType = Enum.CameraType.Scriptable
The scripts in ReplicatedFirst and StarterPlayerScripts only run once. Character update doesn’t natively doesn’t refresh them, which means you’d have to hook a function to CharacterAdded with the same logic.
I highly suggest the above way of waiting for character. wait() is the inferior ancestor of task.wait(), and this sometimes called polling approach can be replaced with event based waiting and a single event.
Update @Roller_Bott I’m sorry, I didn’t realize you already mentioned CharacterAdded:Wait() in your post above. Didn’t mean to steal your suggestion.