Need some advice on why this spectate isn't working correctly

Hey, basically I’ve created this spectate script.
It spectates completely fine, but I added an extension onto the script which should respectate once the person (you were spectating has died) and respawned.

The spectate function works completely fine as far as I’ve tested, there’s no errors it just doesn’t work.
The function takes the players name as the 1st argument.

This is in a local script in StarterPlayerScripts as well.

EDIT: Tried adding a wait before running the spectate function, but still doesn’t work either for some reason.

Here’s the script:

workspace.ChildAdded:Connect(function(char)
	local plr = char
	if game.Players:FindFirstChild(plr.Name) ~= nil then
		if plr.Name:lower() == tostring(spec):lower() then
			spectatezON(plr.Name)
		end
end
end)

SPECTATE FUNCTION

local function spectatezON(player)
	game.workspace.CurrentCamera.CameraSubject = game.Players:FindFirstChild(tostring(player)).Character:WaitForChild("Humanoid")
	game.workspace.CurrentCamera.CameraType = "Custom"
	game.workspace.CurrentCamera.CFrame = game.Players:FindFirstChild(tostring(player)).Character:WaitForChild("Head").CFrame
end
2 Likes

local workspace.ChildAdded:Connect(function(char)
local plr = char
if game.Players:FindFirstChild(plr.Name) ~= nil then
if plr.Name:lower() == tostring(spec):lower() then
spectateON(plr.Name)
end

Try this, I dont know if it will work, but I know when a script I have doesnt work, I am just missing the word “local” at the begining of the script. Not all scripts need the word local at the begining, but try it and see if it works. Also, get rid of the z in spectate on the last line like I did here.

local at the beginning causes the entire script to error unfortunately.
The Z in the spectate function is actually intended, the function is written like that as I use the word ‘spectate’ a bit in the rest of the script, so I added a Z to make sure I’m not making some sort of mistake and calling a different function etc.

Okay, that is all I can think of to get it to work, sorry I cant be of more help.

1 Like

I tested the code (just replaced the function call with a print statement and it works as it’s supposed to so it looks like there’s either a problem with the spectatezON function itself or the stored ‘spec’ player name isn’t correct.

Try this:

workspace.ChildAdded:Connect(function(char)
	local plr = game.Players:GetPlayerFromCharacter(char)
	if plr then
		if plr.Name:lower() == tostring(spec):lower() then
			spectatezON(plr.Name) -- Im not sure if this function takes in the players name or the player instance. I left it alone.
		end
	end
end)

Unfortunately never worked when I tried it, going to be adding my Spectate function to my original post.

May be the spectate function is playing up when it’s used with this.
I’m going to be adding that to my original post above.
I printed the spec, and also some “checkpoints” to see where the code gets to, it prints everything but doesn’t spectate.