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

How to fix "Invalid argument #2 (string expected, got nil) error?

Asked by 3 years ago

The error happens on the teamPlayer function: "local newTeam = playerFrame[team]" On line 94

local playersService = game:GetService('Players');
local localPlayer = playersService.LocalPlayer;
local localName = localPlayer.Name;

local leaderboardGUI = script.Parent;
local mainFrame = leaderboardGUI.mainFrame;
local playersFrame = mainFrame.playersFrame;
local listLayout = playersFrame.UIListLayout;
local nameLabel = mainFrame.nameLabel;
local rankStatusLabel = mainFrame.rankStatusLabel;
local prisonTimeStatusLabel = mainFrame.prisonTimeStatusLabel;
local playerFrame = script.playerFrame:Clone();
script.playerFrame:Destroy();
local teamFrame = script.teamFrame:Clone();
script.teamFrame:Destroy();

local players = {};

local replicatedStorage = game:GetService('ReplicatedStorage');
local networkEvent = replicatedStorage.networkEvent;
local networkFunction = replicatedStorage.networkFunction;

local newUDim2 = UDim2.new;
local tabKeyCode = Enum.KeyCode.Tab;
local coroutineWrap = coroutine.wrap;

game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false);

local function resizePlayersFrame()
    local totalY = 0;
    for i, v in next, playersFrame:GetChildren() do
        if v:IsA('ImageLabel') and v.Visible then
            totalY = totalY + v.Size.Y.Offset + 3;
        end;
    end;
    playersFrame.CanvasSize = newUDim2(0, 0, 0, totalY);
end;

listLayout:GetPropertyChangedSignal('AbsoluteContentSize'):Connect(function()
    resizePlayersFrame();
end);

for i, v in next, game:GetService('Teams'):GetChildren() do
    local teamName = v.Name;
    local teamFrame = teamFrame:Clone();
    teamFrame.Name = teamName;
    teamFrame.nameLabel.Text = teamName;
    teamFrame.ImageColor3 = v.TeamColor.Color;
    teamFrame.Visible = false;
    teamFrame.LayoutOrder = i - 1;
    teamFrame:GetPropertyChangedSignal('Visible'):Connect(function()
        resizePlayersFrame();
    end);
    teamFrame.Parent = playersFrame;
end;

local function addPlayer(name, premium, rank, prisonTime)
    local playerFrame = playerFrame:Clone();
    playerFrame.nameLabel.Text = name;
    playerFrame.premiumImage.Visible = premium;
    playerFrame.rankStatusLabel.Text = rank;
    --playerFrame.prisonTimeStatusLabel.Text = prisonTime;
    playerFrame.Parent = playersFrame;
    return playerFrame;
end;

local function updatePlayer(name, status, data)
    if not players[name] then return; end;
    players[name][1][status .. 'Label'].Text = data;
end;

local function removePlayer(name)
    if not players[name] then return; end;
    local oldTeam = players[name][2];
    players[name][1]:Destroy();
    players[name] = nil;
    if oldTeam then
        local found = false;
        for i, v in next, players do
            if v[2] == oldTeam then
                found = true;
                break;
            end;
        end;
        if not found then
            playersFrame[oldTeam].Visible = true;
        end;
    end;
end;

local function teamPlayer(name, team)
    if not players[name] then return; end;
    local oldTeam = players[name][2];
    local newTeam = playersFrame[team];
    if not newTeam.Visible then
        newTeam.Visible = true;
    end;
    players[name][1].LayoutOrder = newTeam.LayoutOrder;
    players[name][2] = team;
    if oldTeam then
        local found = false;
        for i, v in next, players do
            if v[2] == oldTeam then
                found = true;
                break;
            end;
        end;
        if not found then
            playersFrame[oldTeam].Visible = false;
        end;
    end;
end;

for i, v in next, playersService:GetPlayers() do
    coroutineWrap(function()
        local name, premium, rank, prisonTime, team = networkFunction:InvokeServer('GetPlayerData', v);
        if name then
            if not players[name] then
                players[name] = {addPlayer(name, premium, rank, prisonTime, team)};
                teamPlayer(name, team);
            end;
        end;
    end)();
end;

networkEvent.OnClientEvent:Connect(function(event, ...)
    if event == 'NewPlayer' then
        local name, premium, rank, prisonTime, team = ...;
        if players[name] then return; end;
        players[name] = {addPlayer(...)};
        teamPlayer(name, team);
    elseif event == 'UpdatePlayer' then
        if ({...})[1] == localName then 
            local name, status, data = ...;
            if status == 'prisonTimeStatus' then
                prisonTimeStatusLabel.Text = data;
            elseif status == 'rankStatus' then
                rankStatusLabel.Text = data;
            end;
        end;
        updatePlayer(...);
    elseif event == 'TeamPlayer' then
        teamPlayer(...);
    elseif event == 'RemovePlayer' then
        removePlayer(...);
    end;
end);

if not localPlayer:IsDescendantOf(game) then
    localPlayer:GetPropertyChangedSignal('Parent'):Wait();
end;

nameLabel.Text = localPlayer.Name;
rankStatusLabel.Text = localPlayer:GetRoleInGroup(3151317);
prisonTimeStatusLabel.Text = localPlayer:WaitForChild('leaderstats'):WaitForChild('Prison Time').Value;

game:GetService('UserInputService').InputBegan:Connect(function(input, onGUI)
    if onGUI then return; end;
    if input.KeyCode == tabKeyCode then
        leaderboardGUI.Enabled = not leaderboardGUI.Enabled;
    end;
end);

1 answer

Log in to vote
0
Answered by
amanda 1059 Moderation Voter
3 years ago

In your teamPlayer function, it is expecting two arguments, a "name" and a "team" based on it's definition:

local function teamPlayer(name, team)
    -- . . .
end

The error is indicating that no value or a nil value is being passed to the team parameter.

I've investigated your code, and it appears that you are ultimately getting this team, which is a string, from the server. You have remote events and remote functions that pass varieties of information which in some cases is being funneled directly into the teamPlayer func.

This means, it's time for you to start debugging. Check on the server-side scripts, locate the portion where you acquire/create the data you are sending to the client for the purpose of the function I named above. Double check variables, print out values, and make sure that the "team" actually exists in the variable you think before sending it to the client.

Ad

Answer this question