Changing camera subject after a player dies for spectate

I am making a spectate feature for my game and when a player is spectating another player and that player dies I want the camera to automatically switch to where the player’s new charcater respawned. However all that happens is the camera stays in the same spot as where the character died. I think I am headed in the right direction, I made a spectate player variable and everytime a player switches to a new player to spectate that variable is filled with that player. And when that player has a new character added the camera switches to them but for some reason it is not working.

local camera = game.Workspace.CurrentCamera
local number = 1
local plr = game.Players.LocalPlayer
local SpectateButton = script.Parent.Parent.SpectateButton
local SpecPlr = plr

script.Parent.Next.MouseButton1Up:Connect(function()
	local Players = game.Players:GetPlayers()
	number = number + 1
	if Players[number] ~= nil then
		camera.CameraSubject = Players[number].Character.Humanoid
		SpecPlr = Players[number]
	end
	if Players[number] == nil then
		number = 1
		camera.CameraSubject = Players[number].Character.Humanoid
		SpecPlr = Players[number]
	end
end)


SpecPlr.CharacterAdded:Connect(function()
	camera.CameraSubject = SpecPlr.Character.Humanoid
	print("Died")
end)

The character added event isn’t even playing because I am not getting the print.

2 Likes

That isn’t the issue, characteradded event does connect to the player
https://developer.roblox.com/en-us/api-reference/event/Player/CharacterAdded
There is a different reason the event isn’t firing

wonder why i wrote that, while i already know about that
anyways,
try to set the CameraSubject to the Character’s HRP
or the event doesn’t even fire?
if so most likely there is a loop in your script preventing it from firing.

you can try to put the event in a seperated script.

Yea the issue is the event isn’t firing at all because I put a print in the function and it didn’t print anything, the script I posted is the whole thing

since your localscript is in a ScreenGui, most likely its because you have ResetOnSpawn set to false.

That’s true. However, by default this property is set to true. So unless he changed it, there is probably a different issue in the script.

Maybe this would work? It is a subtle difference, but lua can sometimes be picky.

If this doesn’t work, try adding a few additional prints in your code so we can see what is going on in the output.

local camera = game.Workspace.CurrentCamera
local number = 1
local plr = game.Players.LocalPlayer
local SpectateButton = script.Parent.Parent.SpectateButton
local SpecPlr = plr

local function Next()
	local Players = game.Players:GetPlayers()
	number = number + 1
	if Players[number] ~= nil then
		camera.CameraSubject = Players[number].Character.Humanoid
		SpecPlr = Players[number]
	end
	if Players[number] == nil then
		number = 1
		camera.CameraSubject = Players[number].Character.Humanoid
		SpecPlr = Players[number]
	end
end)


SpecPlr.CharacterAdded:Connect(Next)
script.Parent.Next.MouseButton1Click:Connect(Next)

I apologize in advance for any typos.

Let me know if you need more help :slightly_smiling_face:

1 Like

Yea this still doesnt work, The character added event isn’t firing at all

Maybe instead of CharacterAdded, you find when their humanoid’s health is changed or if their humanoid has died, and then put the function into action.

SpecPlr.Character.Humanoid.Died:Connect(Next)

Also, make sure you add in a wait to make sure the Character’s Humanoid is loaded in.

Can you describe what happens when the player you are spectating dies?

you could make the SpecPlr a global Variable.

I think the issue is that this script is in a local script, and I don’t think from a local script a player can get the values of another player’s character.