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 8 years ago
local lplayer = game.Players.LocalPlayer
repeat wait() until lplayer.Character
local Players = game:GetService("Players")
local target = lplayer.Character.Torso

local cam = workspace.CurrentCamera

cam.CameraSubject = target
cam.CameraType = "Scriptable"
cam.CoordinateFrame = CFrame.new(30,30,30)

local angle = 0


for i,v in pairs(lplayer.Character.Torso:GetChildren()) do
    if v:IsA("Part") and v.Name == "Blocky" then
        local newtarget = lplayer.Character.Torso.Blocky
        while wait() do
            cam.CoordinateFrame = CFrame.new(newtarget.Position) * CFrame.Angles(0, angle, 0) * CFrame.new(0, 0, 5)
            angle = angle + math.rad(1)
        end
    end
end

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 — 8y

1 answer

Log in to vote
0
Answered by
XAXA 1569 Moderation Voter
8 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.

local Players = game:GetService("Players")
local lplayer = Players.LocalPlayer
repeat wait() until lplayer.Character
local Torso = lplayer.Character:WaitForChild("Torso")
local Blocky = Torso:WaitForChild("Blocky") -- it's good practice (read: necessary) to chain WaitForChild() for each descendant.

local cam = workspace.CurrentCamera

cam.CameraSubject = Torso
cam.CameraType = "Scriptable"

local angle = 0
while wait() do
    cam.CoordinateFrame = CFrame.new(Blocky.Position) * CFrame.Angles(0, angle, 0) * CFrame.new(0, 0, 5)
    angle = angle + math.rad(1)
end
Ad

Answer this question