I am working on a script that changes the entire character to a custom one depending on their team and for some reason, it is not working.
I assume the problem has something to do with the StarterCharacter not being set early enough but I am not sure.
One solution I have thought of is to have the character be set when the player changes their team, not when they respawn but I do not want to go through with that until I get people’s input on the problem.
local characterFolder = script.Characters
game.Players.PlayerAdded:Connect(function(player)
local teamName = player.Team.Name
if teamName ~= "Choosing" then
for i, character in pairs(characterFolder:GetChildren()) do
if character.Name == teamName then
local clonedChar = character:Clone()
clonedChar.Name = "StarterCharacter"
clonedChar.Parent = game:GetService("StarterPlayer")
end
end
end
end)
local characterFolder = script.Characters
game.Players.PlayerAdded:Connect(function(player)
player:GetPropertyChangedSignal("Team"):Connect(function(team)
if team.Name ~= "Choosing" then
for i, character in pairs(characterFolder:GetChildren()) do
if character.Name == team.Name then
local clonedChar = character:Clone()
player.Character = clonedChar
end
end
end
end)
end)
It’s possible that the issue with your script is related to the timing of when the StarterCharacter is being set. One way to verify if this is the issue would be to print out the StarterCharacter property of the player’s character to see if it’s being set correctly.
However, another possible issue with the script is that it’s only checking for the player’s team name once, when they first join the game. If the player changes teams later on, their character may not be updated with the correct StarterCharacter. To address this, you could move the team check into a function that gets called whenever the player’s team changes.
Here’s an example of what the updated script could look like:
local characterFolder = script.Characters
local function setStarterCharacter(player)
local teamName = player.Team.Name
if teamName ~= "Choosing" then
for i, character in pairs(characterFolder:GetChildren()) do
if character.Name == teamName then
local clonedChar = character:Clone()
clonedChar.Name = "StarterCharacter"
player.Character = clonedChar
break -- only set the character once
end
end
end
end
-- set starter character when player first joins
game.Players.PlayerAdded:Connect(function(player)
setStarterCharacter(player)
end)
-- set starter character whenever player changes team
game.Teams.TeamChanged:Connect(function(team)
for _, player in ipairs(team:GetPlayers()) do
setStarterCharacter(player)
end
end)
In this updated script, the setStarterCharacter function is called whenever a player’s team changes, which ensures that their StarterCharacter is always set correctly. Additionally, the function sets the player’s character directly using the player.Character property, which should ensure that the StarterCharacter is set as early as possible.
local characterFolder = script.Characters
local function setStarterCharacter(player)
local teamName = player.Team.Name
if teamName ~= "Choosing" then
for i, character in pairs(characterFolder:GetChildren()) do
if character.Name == teamName then
local clonedChar = character:Clone()
clonedChar.Name = "StarterCharacter"
player.Character = clonedChar
break
end
end
end
end
game.Players.PlayerAdded:Connect(function(player)
setStarterCharacter(player)
end)
game.Players.PlayerAdded:Connect(function(player)
local team = player.Team
player:GetPropertyChangedSignal("TeamColor"):Connect(function()
for _, player in ipairs(team:GetPlayers()) do
setStarterCharacter(player)
end
end)
end)
Based on the code you have provided, it looks like you are on the right track in terms of setting the custom character based on the player’s team. However, you are correct that the issue may be related to the timing of when the StarterCharacter is set.
One potential issue could be that the script is trying to access the StarterPlayer service before it has loaded, causing the cloned character to not be properly assigned. To address this, you could wrap the code in a while loop that waits until the StarterPlayer service is available before attempting to set the custom character. Here is an example of how you could modify your code to do this:
local characterFolder = script.Characters
game.Players.PlayerAdded:Connect(function(player)
local teamName = player.Team.Name
if teamName ~= "Choosing" then
for i, character in pairs(characterFolder:GetChildren()) do
if character.Name == teamName then
local clonedChar = character:Clone()
clonedChar.Name = "StarterCharacter"
-- Wait until the StarterPlayer service is available
while not game:GetService("StarterPlayer") do
wait()
end
clonedChar.Parent = game:GetService("StarterPlayer")
end
end
end
end)
Alternatively, you could consider setting the custom character when the player changes their team, as you mentioned. This may be a more straightforward solution, as it avoids any potential timing issues related to the StarterPlayer service.
Here is an example of how you could modify your code to set the custom character when the player changes their team instead of when they respawn:
local characterFolder = script.Characters
-- Function to set the player's character based on their team
local function setPlayerCharacter(player)
local teamName = player.Team.Name
if teamName ~= "Choosing" then
for i, character in pairs(characterFolder:GetChildren()) do
if character.Name == teamName then
local clonedChar = character:Clone()
clonedChar.Parent = player
player.Character = clonedChar
end
end
end
end
-- Set the player's character when they join the game
game.Players.PlayerAdded:Connect(function(player)
setPlayerCharacter(player)
end)
-- Set the player's character when they change their team
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
setPlayerCharacter(player)
end)
end)
This code defines a function setPlayerCharacter that sets the player’s character based on their team. The function is called when a player joins the game, and is also connected to the CharacterAdded event so that it is called again when the player changes their team.
Note that in this example, the custom character is parented to the player instead of the StarterPlayer service. This should ensure that the character is properly set regardless of any timing issues related to the StarterPlayer service.
This error usually occurs when you try to set the character again while it’s still being set. In the code I provided earlier, setPlayerCharacter is called when a player joins the game and again when the player’s character is added. This can cause an infinite loop if the function is called too many times in a row.
To fix this, you can add a check to see if the player’s character has already been set before calling setPlayerCharacter again. Here’s an updated example:
local characterFolder = script.Characters
-- Set the player's character based on their team
local function setPlayerCharacter(player)
local teamName = player.Team.Name
if teamName ~= "Choosing" and not player.Character then
for i, character in pairs(characterFolder:GetChildren()) do
if character.Name == teamName then
local clonedChar = character:Clone()
clonedChar.Parent = player
player.Character = clonedChar
end
end
end
end
-- Set the player's character when they join the game
game.Players.PlayerAdded:Connect(function(player)
setPlayerCharacter(player)
end)
-- Set the player's character when they change their team
game.Teams.TeamChanged:Connect(function()
for _, player in ipairs(game.Players:GetPlayers()) do
setPlayerCharacter(player)
end
end)
In this updated example, the setPlayerCharacter function now checks if the player already has a character before setting it again. Additionally, instead of using the CharacterAdded event to detect team changes, we now use the TeamChanged event on the Teams service. This event is fired whenever a player changes teams, so we can update all players’ characters at once.
The TeamChanged event is not actually a member of the Teams service. Instead, you can use the PlayerChangedTeam event on the Player object to detect when a player changes teams.
game.Players.PlayerAdded:Connect(function(player)
player:GetPropertyChangedSignal("Team"):Connect(function()
for _, player in ipairs(game.Players:GetPlayers()) do
setPlayerCharacter(player)
end
end)
end)