Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Team Change Script Not Working. What happend?

As before, I am making a game involving Team Change.

When I made a GUI with a script telling it to make a GUI visible, when I told it to change the players teams, the script crashed.

Variables(for Team Change):

local Player = game.Players.LocalPlayer
local Citizen = game.Players.LocalPlayer:FindFirstChild("Team"):FindFirstChild("Citizen")

for the Visible part:

local Confirm = game.Players.LocalPlayer:FindFirstChild("PlayerGui"):FindFirstChild("Team Selector").Confirmation

The function:

script.Parent.MouseButton1Click:connect(function()
	Confirm.Visible = true
	Player.Team = Citizen
end)

Put Together:

local Confirm = game.Players.LocalPlayer:FindFirstChild("PlayerGui"):FindFirstChild("Team Selector").Confirmation
local Player = game.Players.LocalPlayer
local Citizen = game.Players.LocalPlayer:FindFirstChild("Team"):FindFirstChild("Citizen")

script.Parent.MouseButton1Click:connect(function()
	Confirm.Visible = true
	Player.Team = Citizen
end)

When run, Like I said, It all crashes. What do I have to do?

2 answers

✓ Accepted answer

There may be some confusion, but it is okay to change TeamColor or Team property when assigning a player to a team. However, by changing the Team property, you must give the actual Instance of the Team and Team must be a part of the Team Service before either changes!

Looking at your code, local Citizen = game.Players.LocalPlayer:FindFirstChild("Team"):FindFirstChild("Citizen") seems strange. You should find a team named Citizen by looking through the TeamService. And again, keep in mind if you attempt to change TeamColor to a non-existent team's color, the player will be put in neutral or you get an error, depending on what you're doing.

See below on a script that should work, although I am unsure if changing team through localscripts will work, you may need to use Remotes to change the team. On the note of your crashes, I see nothing that should result in one, it may be a difference script you are not showing us.

local Confirm = game.Players.LocalPlayer:FindFirstChild("PlayerGui"):FindFirstChild("Team Selector").Confirmation -- unsure why you don't use FindFirstChild for Confirmation
local Player = game.Players.LocalPlayer
local Citizen =game:GetService("Teams"):FindFirstChild("Citizen")

script.Parent.MouseButton1Click:Connect(function() -- Connect should be capitalized, changes nothing but is desired
    Confirm.Visible = true
    Player.Team = Citizen
end)

3 comments

0

If there are issues, feel free to comment below! I believe this would work theoretically but again, a RemoteEvent may be needed

0

Thanks. This is the current script

1

THANK YOU SO MUCH! It worked Perfectly.

When you change the Team, you want to change the player's TeamColor. Look here and here! Not the team itself.

EXAMPLE (NOT BY ME):

function setTeam(player, teamName)
    player.TeamColor = game.Teams[teamName].TeamColor
    if player.Character then --Just in case the character doesn't exist for some reason
        player.Character:BreakJoints() -- Kills the players' character
    end
end

--How it's used
setTeam(game.Players.anth4598, "Blues")

--Changing everyone's team to "Reds"
for _, player in pairs(game.Players:GetPlayers()) do
    setTeam(player, "Reds")
end

Source

2 comments

0

???????????????

1

Inside of the roblox wiki, and I quote "Changing Player.TeamColor will cause Player.Team to switch to the Team with the corresponding Team.TeamColor" so as BennyBoiOriginal said, just change the TeamColor property of team.

This discussion is preserved as it was. New answers, comments, and votes are closed.