How do I fix my ghost team respawn script?

I want to create a ghost respawn script that changes the player’s team to ghost and respawns them to a spectator area so they cannot respawn and I can allow the game to end. I think I may have messed up on defining the local terms (humanoid, player, something else?) so I looked at some other scripts to try and change it though whenever it seems like it would work, something stops me. I changed the terms around and even added more but I am still confused as it just says
Stack Begin
Script ‘ServerScriptService.GhostRespawnScript’, Line 3
Stack End
in my output. Here is my code in ServerScriptService

local playerModel = game.StarterPlayer.StarterCharacterScripts
local player = game.Players.LocalPlayer
local humanoid = playerModel:WaitForChild("Humanoid")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local teamGhost = Teams.Ghost

if humanoid.died then
	wait(1)
	local function play(player)
		player.Team = teamGhost
		player.TeamColor = teamGhost.TeamColor
		player:LoadCharacter()
	end
end

note that I may have defined more terms then I needed to when I was trying to troubleshoot

You need to get you player from the died event instead of the variables.
You shouldn’t have multiple player variables,
Also you never call your function.

Hi, I noticed a couple of issues that are occurring here.

1.) humanoid.Died is an event, meaning that you’ll have to change:

if humanoid.Died then
--condition will never fail because humanoid.Died is an event that will always exist on Humanoid
end

to

humanoid.Died:Connect(function()
--will run code inside of this block when the player dies
end)

2.) the “play” function inside of your main block is unnecessary since you can just define the block without it so you don’t have to worry about calling it. Change:

wait(1)
local function play(player)
	player.Team = teamGhost
	player.TeamColor = teamGhost.TeamColor
	player:LoadCharacter()
end

To:

wait(1)
player.Team = teamGhost
player.TeamColor = teamGhost.TeamColor
player:LoadCharacter()

So your final code should look something like this.

humanoid.Died:Connect(function()
    wait(1)
    player.Team = teamGhost
    player.TeamColor = teamGhost.TeamColor
    player:LoadCharacter()
end)

HOWEVER:
This will only change the Player’s team on their screen only because it is a localscript. I recommend converting this into a serverscript to change the player’s team on the server so that it will replicate to the other clients.

Hope this helped!