Troubles when assigning/using teams utilizing scripts & UI

Hey there :wave:

I am working on a “game control” UI panel in my game which controls many systems of the game. On this panel I have a few buttons that assign members of a specific team, to another team.

For example, I have a local script inside a TextButton which when clicked kills the players of a specified team. Another example I have a TextButton which when clicked switches all players to another team. Pretty simple stuff, but troublesome as it seems.

One thing I noticed is that I was in the Black Team, and used a TextButton to change to the Blue Team. I then used another button to change to the Red Team and when I press the “Kill Blue Team” button, my character died, even though they were in the Red Team.

I suspect this might have to do with my choices of using local/server scripts, so if that’s the case, please do provide some input on how I can fix them.

Thanks in advance!

Post your scripts

This would be the case, if you are trying to change the player’s team inside of a local script, only the client can see the team change, and it won’t be replicated to the server.

Create a RemoveEvent and a server script and call it whenever you want the server to change a player’s team.

The biggest issue I am experiencing right now is with the following script,

player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	player.TeamColor = BrickColor.new("Bright red")
end)

This is a server script located under a TextButton.

I see, you’re right! I switched some of my scripts from local to server and they are working well, except the one I mentioned in a previous reply which I suspect will require a RemoteEvent like you mentioned. I’ll work into that. Thanks!

Not sure if this is relevant anymore, but server scripts cannot retrieve a local player and cannot detect when a player presses a UI button.
Instead, use a local script to detect the button press, then fire a RemoteEvent. Something like this:

local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

script.Parent.MouseButton1Click:Connect(function()
    RemoteEvent:FireServer("Team color goes here")
end)

In a server script, you can then detect when the event is fired and change the player’s team accordingly:

local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

RemoteEvent.OnServerEvent:Connect(function(player, teamColor))
    player.TeamColor = BrickColor.new(teamColor)
end)