Hi all. So I'm having an issue. Let me explain what the script below is trying to do.
- An admin says :team1 player1,player2,player3
- The script takes the players selected, if the player's name was shortened or not capitalized correctly in the chat, it finds the correct player and returns it
- once the correct player is found, the code changes the player's team
Now this code works perfectly fine except for line 41 which is:
game.Players.playerName.TeamColor = game.Teams['Blue'].TeamColor
where I get this error:
02:56:14.627 - playerName is not a valid member of Players
When I take line 41 out, the code runs seamlessly, except that it doesnt team the player.
I put in the print at line 40 to check what the variable playerName is, and if I return a userdata from the findPlayer function, it prints a brickcolor that corresponds to the team color, and if I return the player's name as a string from the findplayer function, the print at line 40 returns nil, which proves the variable playerName is a string.
However, in both circumstances I get the same error, which is what I posted above.
Any help trying to address this issue would be greatly appreciated. I can go into more detail or clarify things if needed.
Thanks!
admins = {"Cubes4Life", "Player", "Player1", "trebor76"}
function isAdmin(name)
for i,v in pairs(admins) do
if name:lower() == v:lower() then return true end
end
return false
end
function findPlayer(name)
for i, player in ipairs(game.Players:GetPlayers()) do
local plr = player.Name:sub(1,string.len(name))
local plyr = plr:lower()
if name:lower() == plyr then
print(player.Name.. ' WAS FOUND YAYYY!!!')
return player
end
end
end
function TeamPlacer(players, team)
for player in string.gmatch(players,"%w+") do
print(player)
local playerName = findPlayer(player)
if team == 'TeamOne' then
game.Players.playerName.TeamColor = game.Teams['Red'].TeamColor
elseif team == 'TeamTwo' then
print(playerName.TeamColor)
game.Players.playerName.TeamColor = game.Teams['Blue'].TeamColor
end
end
end
game.Players.PlayerAdded:connect(function(player)
if isAdmin(player.Name) then
player.Chatted:connect(function(message)
if message:sub(1, 7) == ":team1 " then
local players = message:sub(8)
TeamPlacer(players, 'TeamOne')
elseif message:sub(1, 7) == ":team2 " then
local players = message:sub(8)
TeamPlacer(players, 'TeamTwo')
end
end)
end
end)
