Help with CameraSubject

Help with spectate GUI

Hello, I am currently making a spectate gui in my game. However, whenever you spectate, your camera just locks in place. The GUI still shows the name of the player you are trying to spectate, though. Also, it still prints the name of the player you are trying to spectate. The only thing not working is the CameraSubject. Any help is appreciated.

Here is the code I am using to set the camera:

local camera = workspace.CurrentCamera
--pt is a table of players

function nextPlayer(current:number, pt)
	if #pt <= 1 then return end
	clickSound:Play()
	local plrNum = current + 1
	if not pt[plrNum] then
		repeat
			plrNum -= 1
		until
			pt[plrNum]
	end
	local nextPlayer = pt[plrNum]
	local currentPlayer = players:FindFirstChild(nextPlayer)
	if currentPlayer.Name == players.LocalPlayer.Name then nextPlayer(current, pt) return end
	if not currentPlayer then return end
	if currentPlayer.Team == game.Teams.Dead then
		exitSpectate()
		return
	end
	setTeamBgColour(currentPlayer)
	camera.CameraType = Enum.CameraType.Scriptable
	print(currentPlayer.Name)
	camera.CameraSubject = currentPlayer.Character
	print(camera.CameraSubject.Parent.Name.."'s "..camera.CameraSubject.Name)
	plrName.Text = "@"..camera.CameraSubject.Parent.Name
	plrTeam.Text = "Team: "..currentPlayer.Team.Name
	camera.CameraType = Enum.CameraType.Track
	fixTransparency(camera.CameraSubject)
	script.Parent.MainFrame.Visible = true
end

Try something like this;

function nextPlayer(current:number, pt)
    if #pt <= 1 then return end
    clickSound:Play()
    local plrNum = current + 1
    if not pt[plrNum] then
        repeat
            plrNum -= 1
        until
            pt[plrNum]
    end
    local nextPlayer = pt[plrNum]
    local currentPlayer = players:FindFirstChild(nextPlayer)
    if currentPlayer.Name == players.LocalPlayer.Name then nextPlayer(current, pt) return end
    if not currentPlayer then return end
    if currentPlayer.Team == game.Teams.Dead then
        exitSpectate()
        return
    end
    setTeamBgColour(currentPlayer)
    camera.CameraSubject = currentPlayer.Character
    camera.CameraType = Enum.CameraType.Track -- Set CameraType after setting CameraSubject
    print(currentPlayer.Name)
    plrName.Text = "@"..camera.CameraSubject.Parent.Name
    plrTeam.Text = "Team: "..currentPlayer.Team.Name
    fixTransparency(camera.CameraSubject)
    script.Parent.MainFrame.Visible = true
end

ps. This changes the CameraSubject before changing the CameraType.

1 Like

wait, but wouldn’t I need the CameraType to be Enum.CameraType.Scriptable before setting camera.CameraSubject? or does that just not apply to CameraSubject?

Changing that didn’t work. I tried it without Enum.CameraType.Scriptable, then with. Thinking it might be a problem to do with the current variable.

Update: added a print statement which has told me that the current variable is going too high.
I have changed some of the code:

if not pt[plrNum] then
    print("Player not found;")
    plrNum = 1
end

update2:
Since it is sometimes working and sometimes not, the current variable might just be jumping too far out of the pt table. I’ve added a debounce to the script to ensure it only goes up by one each time.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.