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.
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.
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
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)
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?
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.