Hello I’m trying to make a main menu but in this main menu there is a camera part and I’m trying to make it so that when a player presses a button the camera switches to another camera. However when I do that the camera doesn’t switch and instead gives me this “Attempt to index nil with CFrame” bug.
Here’s my code:
script.Parent.Activated:Connect(function()
local wait_time = 0.001
local CurrentCamera = workspace.CurrentCamera
local part = workspace:WaitForChild("CharacterCreateCamera", wait_time)
wait(0.001)
CurrentCamera.CameraType = Enum.CameraType.Scriptable
CurrentCamera.CFrame = part.CFrame
game.Lighting.TimeOfDay = 6
game.Players.LocalPlayer.PlayerGui.ScreenGui.Main.SelectSave.Main.Visible = false
game.Players.LocalPlayer.PlayerGui.ScreenGui.Main.ButtonHolders.Visible = false
end)
Any idea’s on how to fix this issue? According to the output, the bug is coming from Line 7.
Also, I’m not amazing with scripting, I’m just a beginner.
The CharacterCreateCamera is the Camera Part. I just checked and it’s not anchored, allow me a moment to test and see if anchoring it changes anything.
Well what I’m trying to do is switch the camera’s to show different “zones” I guess. In doing so, I would need to need quite a bit of room to make each zone, so each zone is spread out. Let me show you:
script.Parent.Activated:Connect(function()
local wait_time = 0.001
local CurrentCamera = workspace.CurrentCamera
local part = workspace:WaitForChild("CharacterCreateCamera", wait_time)
wait(0.001)
CurrentCamera.CameraType = Enum.CameraType.Scriptable
CurrentCamera.CFrame = part.CFrame
game.Lighting.TimeOfDay = 6
game.Players.LocalPlayer.PlayerGui.ScreenGui.Main.SelectSave.Main.Visible = false
game.Players.LocalPlayer.PlayerGui.ScreenGui.Main.ButtonHolders.Visible = false
game.Players.LocalPlayer.RequestStreamAroundAsync(Vector3.new(174.94, 78.1, -1030.9))
end)
I’m not quite sure if this is how it’s supposed to look with the RequestStreamAroundAsync thing, however, I’m still getting the attempt to index nil error on Line 7
first teleport the player to the spot where the part is because right now it’s just not streamed in, so the reference is nil. Do this BEFORE you use waitforchild
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local cameraFunction = ReplicatedStorage.StartCamera
cameraFunction.OnServerInvoke = function(player)
player:RequestStreamAroundAsync(workspace.CharacterCreateCamera.Position)
return true
end
Client script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Lighting = game:GetService("Lighting")
local localPlayer = Players.LocalPlayer
local cameraFunction = ReplicatedStorage:WaitForChild("StartCamera")
local mainGui = localPlayer.PlayerGui.ScreenGui.Main
local currentCamera = workspace.CurrentCamera
script.Parent.Activated:Connect(function()
cameraFunction:InvokeServer()
local part = workspace:WaitForChild("CharacterCreateCamera")
currentCamera.CFrame = part.CFrame
currentCamera.CameraType = Enum.CameraType.Scriptable
Lighting.TimeOfDay = 6
mainGui.SelectSave.Main.Visible = false
mainGui.ButtonHolders.Visible = false
end)
I put the server script in the Script in ReplicatedStorage and the Client script in the CharacterCreateCamera local script in the SaveSlot1 Text button