You can create a script either inside the part the player touches to change the team or inside SSS (ServerScriptService) if the player’s team doesn’t change through a part.
IF IT CHANGES VIA A PART
Create a script inside the part
script.Parent.Touched:Connect(function(hit) -- When the part is touched it will trigger a function
if hit.Parent:FindFirstChild("Humanoid") then -- If the parent of the part it touched has a Humanoid, so it's a player (Because the parts are touched by the player's body parts)
local FindCharacter = game:GetService("ServerStorage"):FindFirstChild("Test_Char") -- Here you will find the character you want to clone. The Test_Char is a test name I used to see if the script will work you can change it
if FindCharacter then -- If the character exists
local plr = game:GetService("Players"):WaitForChild(hit.Parent.Name) -- We will find the player
local char = plr.Character or plr.CharacterAdded:Wait() -- Then find their character (so we can config it)
local NewCharacter = FindCharacter:Clone() -- Copy the team's character
plr.Character = NewCharacter -- apply the team's character to the player's character
NewCharacter.Parent = workspace -- spawn the player with the new character
end
end)
Note: You will need to create different parts for each team
IF IT CHANGES VIA SOMETHING ELSE
Create a script in ServerScriptService
local Team1 = game.Teams.Team1 -- You can change the teams , these are for testing purposes
local Team2 = game.Teams.Team2 -- Same here
local function Change_Character(plr, Team) -- It's better to have a function to organise things
local FindCharacter = game:GetService("ServerStorage"):FindFirstChild(tostring(Team)) -- Same as the previous script but this time the Character has the team's name
if FindCharacter then -- If we find the character
local char = plr.Character or plr.CharacterAdded:Wait() -- We define the player's character
local NewCharacter = FindCharacter:Clone() -- Clone the character from SS
plr.Character = NewCharacter -- Apply the character to the player's character
NewCharacter.Parent = workspace -- Spawn the player with the new character
end
end
game.Players.PlayerAdded:Connect(function(plr) -- When a player joins, it will trigger a function
Change_Character(plr, plr.Team) -- Fire the function with the player and the player's team to ensure that once they spawn they already have the team's character
plr:GetPropertyChangedSignal("Team"):Connect(function() -- If the player changes Team
Change_Character(plr, plr.Team) -- We will fire the function again to change the Character
end)
end)
Note: If you change the player’s team locally it will not change their character since its form a client !
Hope this helped you, if you have any other questions let me know!
Would the alternative way work with a team change GUI? Here’s the path that the player takes on the GUI:
Left clicking on one of the teams (local script in the GUI), it makes a remote event fired and makes a script (ServerScriptService) change that player’s team. The script has a function.
Do I put the Change_Character function into the already-existing function and the PlayerAdded function outside?
Inside the Gui Button you can add a local script like this:
local Team1 = game.Teams.Team1
local Team2 = game.Teams.Team2
local event = game.ReplicatedStorage.teamchanging
local client_plr = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
if client_plr.Team == Team1 then
client_plr.Team = Team2
event:FireServer(Team2)
else
client_plr.Team = Team1
event:FireServer(Team1)
end
end)
With this script you check whether the player is Team1 or Team2 and make them the opposite team.
Now in the Server Script:
add the following lines:
local event = game.ReplicatedStorage.teamchanging -- You can put this in the top with the other variables
event.OnServerEvent:Connect(function(clientplr, clientteam)
Change_Character(clientplr, clientteam)
end)
With this addition, once the event is fired from the client with the client’s team, it will change ther character.
It’s hard for me to decipher what I’m supposed to do. It’s also my fault because I forgot to mention that there are multiple buttons… (4 teams)
Since you didn’t get my LocalScript yet:
local screengui = script.Parent
local army = screengui.armyteam
local peasant = screengui.peasantteam
local ranger = screengui.rangerteam
local royal = screengui.royalteam
local event = game.ReplicatedStorage.teamchanging
local armycolor = BrickColor.new("Teal")
local peasantcolor = BrickColor.new("Brown")
local rangercolor = BrickColor.new("Sea green")
local royalcolor = BrickColor.new("New Yeller")
army.MouseButton1Click:Connect(function()
event:FireServer(armycolor)
end)
peasant.MouseButton1Click:Connect(function()
event:FireServer(peasantcolor)
end)
ranger.MouseButton1Click:Connect(function()
event:FireServer(rangercolor)
end)
royal.MouseButton1Click:Connect(function()
event:FireServer(royalcolor)
end)