How would I change a single players character in this script

I want to randomize the starter character of each player that joins the game.

It works at first but when the player dies it changes their character.

I’m not really sure how I would fix this.

local StarterCharacter = game.StarterPlayer.StarterCharacter
local Teams = game:GetService("Teams")



local CharacterAssets = {
	
	FaceDecals = {
		"http://www.roblox.com/asset/?id=7075412";
		"http://www.roblox.com/asset/?id=629933140";
		"http://www.roblox.com/asset/?id=255828374";
		"http://www.roblox.com/asset/?id=8560912";
		"http://www.roblox.com/asset/?id=110287880 ";
		"http://www.roblox.com/asset/?id=7131857";
		"http://www.roblox.com/asset/?id=66329994";
		"http://www.roblox.com/asset?id=478720454";
		"http://www.roblox.com/asset/?id=405706600";
	}
	--
	,Shirts = {
		"http://www.roblox.com/asset/?id=5278505376";
		"http://www.roblox.com/asset/?id=5230828058";
		"http://www.roblox.com/asset/?id=5103634376";
		"http://www.roblox.com/asset/?id=5103631170";
		"http://www.roblox.com/asset/?id=5038072916";
		"http://www.roblox.com/asset/?id=809740790";
		"http://www.roblox.com/asset/?id=3229564100";
		"http://www.roblox.com/asset/?id=2312441456";
		"http://www.roblox.com/asset/?id=4713882231";
		
	}
	--
	,Pants = {
		"http://www.roblox.com/asset/?id=5242202148";
		"http://www.roblox.com/asset/?id=5113695723";
		"http://www.roblox.com/asset/?id=218279985";
		"http://www.roblox.com/asset/?id=414289341";
		"http://www.roblox.com/asset/?id=414289922";
		"http://www.roblox.com/asset/?id=1307478812";
		"http://www.roblox.com/asset/?id=358710109";
		
	}
}


function Randomize(StarterCharacter)
	
	StarterCharacter:WaitForChild("Head").face.Texture = CharacterAssets.FaceDecals[math.random(1, #CharacterAssets.FaceDecals)]
	
	StarterCharacter:WaitForChild("Shirt").ShirtTemplate = CharacterAssets.Shirts[math.random(1, #CharacterAssets.Shirts)]
	
	StarterCharacter:WaitForChild("Pants").PantsTemplate = CharacterAssets.Pants[math.random(1, #CharacterAssets.Pants)]
	
end




game.Players.PlayerAdded:Connect(function(player)
	Randomize(StarterCharacter)
	
	player.Team = Teams.Lobby
end)
3 Likes

Add in a player.CharacterAdded connection inside the playeradded event

EDIT: instead of using whole links you could just write rbxassetid://
(it’s a bit shorter).

2 Likes

Screen Shot 2020-07-13 at 1.38.50 PM

1 Like

This might be a bug. According to https://developer.roblox.com/en-us/api-reference/event/Players/PlayerAdded, the PlayerAdded event should only fire when a player joins the game. However, it does say that in Solo Mode, PlayerAdded doesn’t work correctly. It says

This event does not work as expected in solo mode , because the player is created before scripts that connect to PlayerAdded run. To handle this case, as well as cases in which the script is added into the game after a player enters, create an OnPlayerAdded function that you can call to handle a player’s entrance.

An example of this “OnPlayerAdded” is this code:
local function OnPlayerAdded()
if Player.Character then
--Insert Function to handle players that already exist here
end
--Insert your randomization function you already have here
end
Connect that function to your game.Players.PlayerAdded:Connect() in place of Randomize(StarterCharacter)

I don’t know what else to say if none of this works.