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

Am I addressing the players properly?

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)

7 comments

0

The team of all the players on team "Allies" only changes to "Enemies" when "Player1" is the player that joins the server. When exactly do you want the players on "Allies" to switch to "Enemies"?

0

I want them to change when the message "!force change" is written in chat.

0

You want everyone in the game to change teams when "!force change" is written in chat?

0

this should work, but youre attempting some weird stuff with that event

0

I just want to make sure I know exactly what you want so I can give you an accurate answer.

0

You also have your code organized kind of awkwardly. I don't see a need for 2 PlayerAdded events.

0

This is exactly what I want to happen: When the said Player1 uses the "command" !force change, only the players on the Allies team should get moved to the Enemies team.

1 answer

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!

2 comments

0

Thanks alot it worked, I knew it had something to do with the end of the script and the fact that I didn't get the "Teams". Thank you once again!

0

Glad to help.

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