Camera not changing subject

Very simple issue, yet I can’t find my way around it. In my game, players are locked to first person , however when they die I want to change the camera subject to their killer. I have confirmed the killer & creator tag exists, yet the camera does not change its subject. Here is the code:

function module.DeathCam()
	local humanoid : Humanoid = char.Humanoid
	
	humanoid.Died:Connect(function()
		local tag = humanoid:FindFirstChild("creator")
		
		if tag then
			local killer = tag.Value
			plr.CameraMode = Enum.CameraMode.Classic
			workspace.CurrentCamera.CameraSubject = killer.Character.Humanoid
		end
	end)
	
	plr.CharacterAdded:Connect(function()
		plr.CameraMode = Enum.CameraMode.LockFirstPerson
		workspace.CurrentCamera.CameraSubject = char.Humanoid
	end)
end

This outputs no errors.

what defines this char variable? shouldn’t you have it as a variable on CharacterAdded and connect to the humanoid once the character spawns?

LocalPlayer.CharacterAdded:Connect(function(char)
	print(char) -- LastingSunset<Model>
	-- get the humanoid here then listen to the humanoid death for the killer tag
end)
1 Like

Variables are defined at the top of the module, sorry I forgot to add them.

local players = game:GetService("Players")
local plr : Player = players.LocalPlayer
local char : Model = plr.Character or plr.CharacterAdded:Wait()
1 Like

the character can and will become nil if the player ever resets, if this isn’t run under something like CharacterScripts where it wouldn’t matter since the script will re-run every time a character spawns. (this may or may not be the case i am just warning you regardless)

i still think you should remove that char variable and move it inside the function to get it from the character added connection, then get the humanoid from there.
make sure it also runs the function itself if the character already exists

questions:

  • what creates that “creator” InstanceValue? possible desync could cause the tag to be created after death meaning the client wont see it as soon as they die and they will not get it after since it only runs once
1 Like

In my hit detection system I incorrectly defined the object value as the player that had been killed instead of the killer. I fixed it, however, would you be able to find the flaw in this code that is stopping the CharacterAdded signal from firing, in order to reset the camera?

function module.deathCam(player : Player)
local char : Model = player.Character or player.CharacterAdded:Wait()
local humanoid : Humanoid = char.Humanoid
local cam = workspace.CurrentCamera
local connection
local RS = game:GetService("RunService")

	player.CharacterAdded:Connect(function() -- SIGNAL NOT FIRING
		print("its working")
		if cam.CameraType ~= Enum.CameraType.Custom then 
			cam.CameraType = Enum.CameraType.Custom
			cam.CameraSubject = char.Humanoid
			player.CameraMode = Enum.CameraMode.LockFirstPerson
		end
	end)

humanoid.Died:Connect(function()
	local endPos = char.HumanoidRootPart.Position + Vector3.new(0, 5, 0)
	local creator = humanoid:FindFirstChild("creator")
	
	
	if creator ~= nil and creator.Value ~= nil then
		local deathPos : Vector3 = char.Head.Position
		local killer : Player = creator.Value
		local killerChar : Model = killer.Character
		player.CameraMode = Enum.CameraMode.Classic	
		cam.CameraType = Enum.CameraType.Watch
		cam.CameraSubject = killerChar.Humanoid
		
		
				
				
				
		
	end
end)

The print statement is not in the output.

1 Like

what’s the point in connecting to CharacterAdded if you already have the char variable? you don’t need the signal imo since i’m assuming this module script is required in a local script in CharacterScripts

1 Like

nah its in gui my b i forgot to mention that. still i don’t understand why the characteradded wouldn’t fire

1 Like

well i’m pretty certain if you reset the signal will fire, if the character is already spawned in and then you connect to the CharacterAdded event, it will not fire until the character spawns in again.

some people have a wrapper method like this:

local function OnCharacterAdded(Character)
	...
end
if LocalPlayer.Character then OnCharacterAdded(LocalPlayer.Character) end
LocalPlayer.CharacterAdded:Connect(OnCharacterAdded)

so that will fire immediately if the character already exists, as well as firing again when the character spawns in.

1 Like

no the CharacterAdded signal literally never fires even when the character respawns after dying. in this specific instance

1 Like

that’s odd. i’m gonna use my 5 brain cells left and assume that it’s because the gui that the script is in has ResetOnSpawn enabled causing it to never end up getting it since you already wait for it. there is no need to do CharacterAdded.

the script itself runs when the character spawns in so there isn’t a need to listen for it, you already have the correct character model and it will always be updated since the whole script re-executes

4 Likes