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

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

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

✓ Accepted answer

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.

3 comments

0

The Camera glitches up, It stays in the same Pos.

0

But anyways thx bro.

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"

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