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

Why is the Camera's Subject not a truss? [REPOSTED]

Asked by
woodengop 1134 Moderation Voter
9 years ago

Why wouldn't the Camera's Subject be focused on the truss? Here is the Code:

local cam=workspace.CurrentCamera
local button=game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("ScreenGui")
-- In Line 2 SH cut off :WaitForChild("ScreenGui")
game.Players.PlayerAdded:connect(function(plr)
    cam.CameraSubject=workspace:FindFirstChild("Truss")
end)

button.TextButton.MouseButton1Down:connect(function()
    cam.CameraSubject=plr.Character:WaitForChild("Humanoid")
end)

I want the Camera's Subject to go back to the Humanoid when the Gui is clicked.

This Error kept popping up in the Output

Please Respect that I am a Beginner.

1 answer

Log in to vote
2
Answered by
yumtaste 476 Moderation Voter
9 years ago

The variable "plr" is specific to the PlayerAdded function. By that, I mean "plr" can only be used inbetween game.Players.PlayerAdded:connect(function(plr) and end). I'll just rewrite your script for you and explain it, since it would be more work just to explain the errors and have you rewrite it. I don't mean to be rude in any way in this answer.

--let's try this script, eh?

local player = game.Players.LocalPlayer --defines player
local camera = workspace.CurrentCamera --defines camera
local button = player:FindFirstChild("PlayerGui"):FindFirstChild("ScreenGui"):FindFirstChild("TextButton") --defines button

camera.CameraSubject = workspace:FindFirstChild("Truss") --makes CameraSubject "Truss"

button.MouseButton1Click:connect(function() --button click event
camera.CameraSubject = player.Character:FindFirstChild("Humanoid") --makes CameraSubject "Humanoid"
end) --end, obviously

Notice how I removed the PlayerAdded event. I did that because it's not necessary. Keep in mind that this LocalScript has to be either directly or indirectly in StarterGui or StarterPack.

0
The Camera glitches up, It stays in the same Pos. woodengop 1134 — 9y
0
But anyways thx bro. woodengop 1134 — 9y
1
People often make the same mistake you just did. FindFirstChild() either returns the child, or nil. It's only useful because it works like a function, so it won't throw an error if its nil. It's only useful in if statements, though, because otherwise if it returns nil you will be saying "nil.MouseButton1Click" Perci1 4988 — 9y
Ad

Answer this question