Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can someone help with my code for camera manipulation?

Asked by 5 years ago
local plr = game.Players.LocalPlayer
local char = plr.Character
local camera = workspace.CurrentCamera
local Model = workspace:FindFirstChild("For thumbnail")
local main = Model.MainCam

local char = plr.Character
    repeat wait()
        camera.CameraType = Enum.CameraType.Scriptable
    until camera.CameraType == Enum.CameraType.Scriptable
camera.CFrame = main.CFrame


I get error: ServerScriptService.Camera:2: attempt to index local 'plr' (a nil value)

0
hey, since the script is in SeverScriptService, It will be a normal script. you can't do game.Players.LocalPlayer in a normal script tonyv537 95 — 5y
0
what are you trying to do as well, just keep the camera like that, or do you have to press a button to make it go back to normal tonyv537 95 — 5y
0
1. LocalPlayer doesn't exist on the server, only on the client. 2. The server doesn't share cameras with the players, therefore camera manipulation should be done on the client Rare_tendo 3000 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Hello,

One way you can achieve your camera manipulation is by putting a Local Script into StarterPlayerScripts

-- Variables
local plr = game.Players.LocalPlayer -- local player
local char = plr.Character -- local players's character
local cam = game.Workspace.CurrentCamera
local Model = game.Workspace["For thumbnail"] -- change this to wherever your model is
local main = Model.CamPart -- and change this to your cam part

-- Local function

function CamManipulation()
    wait()
    cam.CameraType = 'Scriptable' -- cam type
    cam.CFrame = main.CFrame -- the place you want the cam to be at
    wait(5) -- add how long you want it to be like that
    cam.CameraType = 'Custom' -- normal
    cam.CameraSubject = char -- normal
end

-- call Function
plr.CharacterAdded:Connect(CamManipulation)



0
why do you put a wait in the function and use Enums when possible instead of strings for setting a property User#23365 30 — 5y
0
also you dont need to set the player's CameraSubject to their character since Custom automatically does that User#23365 30 — 5y
0
and why do u have a comment saying its a local function when its a global one. use local functions User#23365 30 — 5y
0
use plr.Character or plr.CharacterAdded:Wait() when setting a variable of the player's character since their character may not have loaded when the script ran User#23365 30 — 5y
0
dude just relax and let him have his answer ihatecars100 502 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I am not 100% sure what you are need from this code as you have not explain what you are doing. Please check here for details of what is useful to include in a question.

The Camera is only relivant to the client and it is not shared with anyone else. Your error ServerScriptService.Camera:2: attempt to index local 'plr' (a nil value) is explaining that on line 2 of that script the variable named plr holds a nil value. This is because LocalPlayer only exists on the client. A LocalScript runs on the players device meaning that LocalPlayer will always be that player and can never logicly be any other.

You are asking the server to access LocalPlayer but a server manages all players so has no idea of what a local player is.

Back to the code....

When a player joins one of the servers tasks is to replicate the game world to the player. This is not done at the same time so your local script can run before the things needed are loaded. You can use WaitForChild to yeild (make the code pause at this function) while this child name not present. When this child name is added you can safely run the code you need.

Lastly search for the event that does the task you need. Never use loops when there are events available to you. CharacterAdded should be the event you use to change the camera upon player spawn.

Example:-

local plr = game:GetService("Players")
local cam = workspace.CurrentCamera

-- wait for items to load into the game before you can use them
local model = workspace:WaitForChild("For thumbnail"):Wait("MainCam")

-- setup camera function
local function setupCamera()
    -- you correctly used enums which is very good
    cam.CameraType = Enum.CameraType.Scriptable
    cam.CFrame = model.CFrame
end

-- connect event
plr.CharacterAdded:Connect(setupCamera) -- adds the function setupCamera to the event call list
setupCamera() -- player may have spawned before the local script can run

Hope this helps

Answer this question