'Attempt to index nil with character' error on spectate system

Hello, recently I’ve been developing a horror game and found a cool spectate system. The problem I’m occurring is when this line of code runs:

ApplySpectateCamera(CurrentSpectateGroup[CurrentIndex].Character.Humanoid) 

I get “Players.starstruxdev.PlayerGui.MainUI.Modules.DeathScreenManager:142: attempt to index nil with ‘Character’”. How can I fix this? I don’t know what to do.

Here is the full code for the system:

local function ApplySpectateCamera(CameraPos)
	if not CameraPos then
		 Spectating.ToggleTitle() 
		return 
	end 
	 Camera.CameraSubject = CameraPos 
	 SpectateGui.Controls.CurrentPlayer.Player_Name.Text = CameraPos.Parent.Name 
	 Camera.FieldOfView = 115 
	game.Lighting.SpectateCC.Saturation = -1.1 
	game.Lighting.SpectateCC.Brightness = 0.04 
	Tween(game.Lighting.SpectateCC, {
		Saturation = 0, 
		Brightness = 0
	}, 0.9, Enum.EasingStyle.Quint, Enum.EasingDirection.Out) 
	Tween(Camera, {
		FieldOfView = PlayerInfo.SpectatingFOV
	}, 0.6, Enum.EasingStyle.Quint, Enum.EasingDirection.Out) 
	 Spectating.HumanoidParentChange = CameraPos.Parent.AncestryChanged:Connect(Spectating.ToggleTitle) 
end 

local function NextPlayer()
	script.ui:Play()
	Spectating.CurrentIndex = (Spectating.CurrentIndex + 1 - 1) % #CurrentSpectateGroup + 1
	ApplySpectateCamera(CurrentSpectateGroup[CurrentIndex].Character.Humanoid)
end

local function PreviousPlayer()
	script.ui:Play()
	Spectating.CurrentIndex = (Spectating.CurrentIndex - 1 - 1) % #CurrentSpectateGroup + 1
	ApplySpectateCamera(CurrentSpectateGroup[CurrentIndex].Character.Humanoid)
end

1 Like

Enclose everything in a Player.CharacterAdded function

1 Like

Try printing out every index value before calling the function. Then print the value that should have a Character property

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