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

Why does the camera not focus and stays static?

Asked by 9 years ago
01local lplayer = game.Players.LocalPlayer
02repeat wait() until lplayer.Character
03local Players = game:GetService("Players")
04local target = lplayer.Character.Torso
05 
06local cam = workspace.CurrentCamera
07 
08cam.CameraSubject = target
09cam.CameraType = "Scriptable"
10cam.CoordinateFrame = CFrame.new(30,30,30)
11 
12local angle = 0
13 
14 
15for i,v in pairs(lplayer.Character.Torso:GetChildren()) do
View all 23 lines...

It doens't focus on "Blocky" which is a 2x2x2 cube that's child of Torso.

** Edit **Blocky is not a valid member of Part ** It's a local script **

0
Have you tried CFrame.new(0, 0, -5)? XAXA 1569 — 9y

1 answer

Log in to vote
0
Answered by
XAXA 1569 Moderation Voter
9 years ago

The problem in your script lies at line 16: You're assuming that Blocky is always created before the script runs, when in fact this isn't the case. According to a conversation we had, Blocky is added to the player immediately after spawning, and the script runs also immediately after spawning.

Solution

You don't even have to iterate through the player's Torso. To fix this, use WaitForChild("name"). WaitForChild("name") yields the current thread (pauses the script) until a child with a matching name is found. The child is then returned.

01local Players = game:GetService("Players")
02local lplayer = Players.LocalPlayer
03repeat wait() until lplayer.Character
04local Torso = lplayer.Character:WaitForChild("Torso")
05local Blocky = Torso:WaitForChild("Blocky") -- it's good practice (read: necessary) to chain WaitForChild() for each descendant.
06 
07local cam = workspace.CurrentCamera
08 
09cam.CameraSubject = Torso
10cam.CameraType = "Scriptable"
11 
12local angle = 0
13while wait() do
14    cam.CoordinateFrame = CFrame.new(Blocky.Position) * CFrame.Angles(0, angle, 0) * CFrame.new(0, 0, 5)
15    angle = angle + math.rad(1)
16end
Ad

Answer this question