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

Help me with fixing a path issue?

Asked by 9 years ago

Hi all. So I'm having an issue. Let me explain what the script below is trying to do.

  1. An admin says :team1 player1,player2,player3
  2. 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
  3. 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)    

1 answer

Log in to vote
0
Answered by
Nickoakz 231 Moderation Voter
9 years ago

I don't think that this would work anyway.

game.Players.playerName.TeamColor = game.Teams['Red'].TeamColor

You can't just put a variable in between like that, BUT if you add something like this..

game.Players[playerName].TeamColor = game.Teams['Red'].TeamColor

Then you can?

But also,

return player
local playerName = findPlayer(player)
game.Players.playerName.TeamColor = game.Teams['Blue'].TeamColor

You're taking the long route. 1. Finding the player and returning its object. 2. Trying to go back again and search through players by using the name again.

Go ahead and try this.

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)
    local foundlist={}
    local list=game.Players:getChildren()
    for a=1,#list do
        if list[a]:sub(1,#name):lower()==name:lower() then
            table.insert(foundlist,list[a])
        end
    end
    return foundlist
end 

function teamPlayer(player)
    if team == 'TeamOne' then
        player.TeamColor = game.Teams['Red'].TeamColor
    elseif team == 'TeamTwo' then
        print(playerName.TeamColor)
        player.TeamColor = game.Teams['Blue'].TeamColor
    end
end 

function TeamPlacer(players, team)
    local list=game.Players:getChildren()
    for player in string.gmatch(players,"%w+") do
        local plrs = findPlayer(player)
        if #plrs>=1 then
            for a=1,#plrs do
                if team == 'TeamOne' then
                    plrs[a].TeamColor = game.Teams['Red'].TeamColor
                elseif team == 'TeamTwo' then
                    print(playerName.TeamColor)
                    plrs[a].TeamColor = game.Teams['Blue'].TeamColor
                end
            end
        elseif plrs==0 then
            print("No Players")
        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)    

I may have added more work, but you can always change it back to how you like to view it.

Ad

Answer this question