Character Script not working

My goal is to make a script that assigns a character to a player based on their rank in my group.

Here is the code

local Player = nil

function character()
	local player = Player
	
	local groupid = 15545437
	local role = player:GetRoleInGroup(groupid)
	
	local animate = player.Character.Animate:Clone()
	
	local team = player.Team
	local teamname = player.Team.Name

	local item = game.ReplicatedStorage:FindFirstChild(teamname)
	local char = item:Clone()
	char.Parent = game.Workspace
	char.Name = player.Name

	player.Character = char
	animate.Parent = char
	player.Character.RankHat.Handle.Gui.Frame.Text.Text = role
end

function PlayerAdded(player)
	Player = player
	local groupid = 15545437
	local role = player:GetRankInGroup(groupid)
	local Teams = game:GetService("Teams")
	
	
	if role >= 2 and role <= 7 then
		player.Team = Teams["[C] Combatants"]
		character()
		
	elseif role >= 8 and role <= 11  then
		player.Team = Teams["[B] Operations"]
		character()
	
	elseif role >= 251 and role <= 252  then
		player.Team = Teams["[A] Administration"]
		character()
	
	elseif role >= 253 and role <= 255  then
		player.Team = Teams["[X] Command"]
		character()
	
	elseif role <= 1 then
		player.Team = Teams["[D] Pre-Applicant"]
		character()
		
	end
	
	
end




game.Players.PlayerAdded:Connect(PlayerAdded)

This is a very sloppy script as some of you can probably tell already, and I am facing various errors regarding animations, respawning, etc. (Any support is appreciated)

But my main problem is that the character assignment does not work for the final rank in the script, being “[D] Pre-Applicant”.

I have tried looking for any spelling mistakes and found none, I am also willing to say that I am a bit out of my element as this is easily the most complicated script i’ve ever written.

If you can notice any obvious mistakes, please let me know as I am pretty much desperate here.

Edit : Changed some of the code back to what the original script had

Hi there, I must admit I’m not well versed with changing the player’s character from the one you spawn with normally. However, I did some playing around and got it to work semi-well, I think. Here’s the modified main script

function character(player : Player, role : number, oldChar : Model?)
	local playerCharacter = oldChar or player.Character or player.CharacterAdded:Wait()
	local animate = playerCharacter:WaitForChild("Animate"):Clone()

	local team = player.Team
	local teamname = player.Team.Name
	local item = game.ReplicatedStorage:FindFirstChild(teamname)
	local char = item:Clone()
	char.Parent = game.Workspace
	char.Name = player.Name

	player.Character = char
	animate.Parent = char
	player.Character.RankHat.Handle.Gui.Frame.Text.Text = role
	local humanoid : Humanoid = char:WaitForChild("Humanoid")
	humanoid.Died:Once(function()
		task.wait(game.Players.RespawnTime)
		print("Respawning!")
		character(player, role, char)
		char:Destroy()
	end)
end

function PlayerAdded(player : Player)
	local groupid = 15545437
	local role = 1
	local Teams = game:GetService("Teams")

	if role >= 2 and role <= 7 then
		player.Team = Teams["[C] Combatants"]
		character(player, role)
	elseif role >= 8 and role <= 11  then
		player.Team = Teams["[B] Operations"]
		character(player, role)
	elseif role >= 251 and role <= 252  then
		player.Team = Teams["[A] Administration"]
		character(player, role)
	elseif role >= 253 and role <= 255  then
		player.Team = Teams["[X] Command"]
		character(player, role)
	elseif role <= 1 then
		player.Team = Teams["[D] Pre-Applicant"]
		character(player, role)
	end
end

game.Players.PlayerAdded:Connect(PlayerAdded)

I changed some of the formatting to my style of coding, so feel free to change it back to how you like it.

There were a lot of things I changed, but one of the main things is that having a player variable on the outside of the functions can cause problems when multiple players join in game.

Another big thing I added was :WaitForChild() calls within the functions so no errors would pop up about not being able to find certain things within the character.

I added a respawn logic (not my best work but, it works) within the character function. I also changed what you pass to the character function so you don’t have to set it all within the function itself

This is important: With just the modifications to the main script, there is still some issues when respawning. The biggest being that the camera does not latch onto the new humanoid. Given that the script is a server script, we can’t set the camera directly with the main script

So, I created a new script (Yes a script, not a local script) with the property “RunContext” set to client and added it to each of the models you use for custom characters within their Humanoids
Here’s the script:

workspace.CurrentCamera.CameraSubject = script.Parent
workspace.CurrentCamera.CameraType = Enum.CameraType.Custom

Very simple but it solved the camera issue when respawning.

Sorry for the long post, but I had fun trying to figure out how to get your code running and working! I hope this helps, let me know if you have any more questions or if my code does not work.

I got ServerScriptService.Character:5: attempt to index nil with ‘Character’ - Server - Character:5

No more error but I am still testing

Thanks for the help and I am satisfied with your support, but I was hoping that you could help with serverside animations