Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Camera not resetting to the player?

Im really new to scripting in roblox studio and I need alot of help. Im using a code to set the camera to a part for a main menu backround. Im trying to make the script so when i press a GUI button it resets the camera angle back to the player. Here is my code so far:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera

repeat wait()
    Camera.CameraType = Enum.CameraType.Scriptable
until Camera.CameraType == Enum.CameraType.Scriptable
Camera.CFrame = workspace.CameraPart.CFrame

(For the Camera going to the part, in my local script for my entire main menu)

script.Parent.MouseButton1Click:Connect(function()
	workspace.CurrentCamera.CameraSubject = player.Character.Humanoid
end)

(For the GUI Button Press, its in the local script inside of the textbutton)

I get this error when i press the GUI button:

15:24:41.104 - Players.TheWinterSoilder551.PlayerGui.Main Menu.Sidebar.Play.Play Script:3: attempt to index nil with 'Character'

I really dont know how to fix it.

4 comments

0

Mind putting the code in code blocks?

0

Again im new, how do i do that?

0

Try editing then clicking the lua button during editing.

0

There you go

1 answer

Your error is just returning that it's not finding the player's character, so I believe you can fix the first script with this:

local plr = game.Players.LocalPlayer
local Character = plr:FindFirstChild("Character")
local Camera = workspace.CurrentCamera

repeat wait()
    Camera.CameraType = Enum.CameraType.Scriptable
until Camera.CameraType == Enum.CameraType.Scriptable
Camera.CFrame = workspace.CameraPart.CFrame

And for the other script, which could also be returning the error, try using this:

script.Parent.MouseButton1Click:Connect(function()
workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer:FindFirstChild("Character"):FindFirstChild("Humanoid")
end)

For some reason, it split into two lines... try placing "workspace.CurrentCamera.CameraSubect = " and the 'game.Players.LocalPlayer:WaitForChild("Character"):FindFirstChild("Humanoid")' into one line.

8 comments

0

ok thank you

0

now the camera doesnt go to the part i want it to, it goes to the player automatically

0

Now im getting this error: 17:15:44.633 - Infinite yield possible on 'Players.TheWinterSoilder551:WaitForChild("Character")'

0

I edited the thing, see if the 'Infinite yield' still shows up.

0

ok

0

oh new error: 17:36:41.065 - Players.TheWinterSoilder551.PlayerGui.Main Menu.Sidebar.Play.Play Script:2: attempt to index nil with 'FindFirstChild'

0

Is 'Play Script' a localscript? Because it isn't finding the LocalPlayer for some reason.

0

Yea it is

This discussion is preserved as it was. New answers, comments, and votes are closed.