Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Am I addressing the players properly?

Asked by 5 years ago

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)
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"? MegaManSam1 207 — 5y
0
I want them to change when the message "!force change" is written in chat. Arkazard 0 — 5y
0
You want everyone in the game to change teams when "!force change" is written in chat? MegaManSam1 207 — 5y
0
this should work, but youre attempting some weird stuff with that event starmaq 1290 — 5y
View all comments (3 more)
0
I just want to make sure I know exactly what you want so I can give you an accurate answer. MegaManSam1 207 — 5y
0
You also have your code organized kind of awkwardly. I don't see a need for 2 PlayerAdded events. MegaManSam1 207 — 5y
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. Arkazard 0 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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!

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! Arkazard 0 — 5y
0
Glad to help. MegaManSam1 207 — 5y
Ad

Answer this question