Problem with StarterCharacter: randomization

I want the startercharacter to be randomized every time the player respawns.
No console errors, just doesn’t work.
Here’s my code (it is in StarterPlayerScripts):

game.Players.PlayerAdded:connect(function(plr)
	local Characters = game:GetService("ReplicatedStorage").Players:GetChildren()[math.random(1,3)]

	if game:GetService("StarterPlayer"):FindFirstChild("StarterCharacter") then
		game:GetService("StarterPlayer").StarterCharacter:Destroy()
	end

	Characters.Name = "StarterCharacter"
	Characters.Parent = game:GetService("StarterPlayer")
	plr.CharacterRemoving:connect(function(char)
		Characters = game:GetService("ReplicatedStorage").Players:GetChildren()[math.random(1,3)]

		if game:GetService("StarterPlayer"):FindFirstChild("StarterCharacter") then
			game:GetService("StarterPlayer").StarterCharacter:Destroy()
		end
		Characters.Name = "StarterCharacter"
		Characters.Parent = game:GetService("StarterPlayer")
	end)
end)
3 Likes

Bumping my thing for help.

1 Like

Set the character by doing plr.Character = character

1 Like

Hm? Where do i put this?

1 Like

Put it in the PlayerAdded listener.

1 Like

I don’t know how. I’m a new coder, so I’m not sure here to put it.

1 Like
game.Players.PlayerAdded:connect(function(plr)
	local character = plr.CharacterAdded:Wait()
	
	local Character = game:GetService("ReplicatedStorage").Players:GetChildren()[math.random(1,3)]:Clone()
	Character.PrimaryPart.CFrame = character.PrimaryPart.CFrame
	Character.Parent = workspace

	plr.Character = Character
end)
1 Like

I cannot see the map anymore at all.

1 Like

Nevermind. I can. The script just doesn’t work lol.

1 Like

@bytesleuth r u here?

1 Like

Bumping post for help again.

1 Like

Make sure the script is in a Script (not local), and that it is in the ServerScriptService (not StarterPlayerScripts).

1 Like

Players is not a valid member of ReplicatedStorage “ReplicatedStorage”

1 Like

Make a Folder in Replicated storage and name it “Players” then put your starterCharacters in the folder

1 Like

Error in fifth line: attempted to index nil with CFrame.

5th line:

Character.PrimaryPart.CFrame = character.PrimaryPart.CFrame
1 Like

Do your StarterCharacters have a set PrimaryPart?

1 Like

Why should they? They’re startercharacters. They’re playermodels.

1 Like
character:GetPropertyChangedSignal("PrimaryPart"):Wait()
Character.PrimaryPart.CFrame = character.PrimaryPart.CFrame
1 Like

PrimaryPart is not a valid property name.

1 Like

On the Model of the Characters, Set the Primary part to the Root of the character. As shown below.
C__Users_hcree_OneDrive_Desktop_FreightTrainRunaway.rbxl - Roblox Studio 8_1_2023 1_55_00 PM (2)

This will help when Animating or moving them.

1 Like