I am trying to change the team of every player who's in the "Allies" team to the "Enemies" team but I'm not sure what I'm doing wrong here, or I'm still learning so it may actually be a simple mistake but I can't seem to find it. This is the script:
local Players = game:GetService("Players") game.Players.PlayerAdded:Connect(function(player) function onChatted(msg, recipient, speaker) local c = game.Players:GetPlayers() local source = string.lower(msg) if (msg == "!force change") and player.Name == "Player1" then for i = 1,#c do if c[i].Team == "Allies" then c[i].Team = "Enemies" end end end end end) function onPlayerEntered(newPlayer) newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) end game.Players.PlayerAdded:connect(onPlayerEntered)
Your code is organized awkwardly and difficult for me to read, so I can't give you the exact reason why your code isn't working, but here's a script that I wrote, assuming this script is in ServerScriptService and you have two teams inside of "Teams" service, "Allies" and "Enemies":
local Players = game:GetService("Players") local Teams = game:GetService("Teams") Players.PlayerAdded:Connect(function(player) if player.Name == "Player1" then -- only create the "Chatted" event for the player(s) you want to be able to use it. player.Chatted:Connect(function(msg) if msg == "!force change" then local players = Players:GetChildren() for i = 1, #players do local plr = players[i] if plr.Team == Teams.Allies then plr.Team = Teams.Enemies end end end end) end end)
Let me know if this script doesn't work the way you want it to. Hope this helps!