'attempt to index nil with `GetRankInGroup`' 的 Roblox 错误

Roblox Error with 'attempt to index nil with `GetRankInGroup`'

我正在制作一个运行演示文稿的黑板。当您单击 SurfaceGUI 上的按钮时,它应该正在播放演示文稿。演示功能有效。但是,当它检查其玩家等级时,它会说出错误(在标题中)。我知道 game.Players.LocalPlayer 只能在 LocalScripts 中使用。但我改为使用 (function(player),因为我认为这是唯一可行的方法。这可能有点难以理解,所以我会拍一些错误和脚本的照片。

local groupid = 6554102


local rankid = 205


local wait_time = 5


local faces_text = {
    first = "Left, Face - Turn left 90 degree";
    second = "Right, Face - Turn right 90 degrees";
    third = "About, Face - Turn 180 degrees";
    fourth = "Left, Incline - Turn left 45 degrees";
    fifth = "Right, Incline - Turn right 45 degrees";
    sixth = "Control, eyes - Follow the host using your eyes";
    seventh = "Center, Face - Face the host";
}


local rules_text = {
    first = "1. Soldiers never abuse guns, guns are only for combat, using guns for other reasons or abusing will result in being arrested.";
    second = "2. Soldiers never let civilian's trespass, only allied personnel are allowed into base.";
    third = "3. Only go into your divisions area, never into other division's areas.";
    fourth = "4. Never go into places your not suppose to go into unless authorized to.";
    fifth = "5. Tanks, trucks, and halftracks are for combat or for driving long distances only.";
}


local ranks_text = {
    first = "[PVT] Private";
    second = "[LCPL] Lance Corporal";
    third = "[CPL] Corporal";
    fourth = "[SGT] Sergeant";
    fifth = "[SSGT] Staff Sergeant";
    sixth = "[SGT-MAJ] Sergeant Major (Obtainable by Gamepass)";
    seventh = "[OCAD] Officer Academy Cadet ";
    eight = "Allied Personnel (BSM Diplomats)";
    ninth = "[LT] Lieutenant";
    tenth = "[CPT] Captain";
    eleventh = "[MAJ] Major";
    twelth = "[LTCOL] Lieutenant Colonel";
    thirteenth = "[COL] Colonel";
    fourteenth = "[GS] General Staff";
    fifteenth = "[FM] Field Marshal";
    sixteenth = "[PM] Prime Minister";
    seventeenth = "[GG] Governor General";
    eighteenth = "[HM] His Majesty";
}


local board = script.Parent
local ChalkGUI = board.ChalkGui
local StartFrame = ChalkGUI.StartFrame
local FacesFrame = ChalkGUI.FacesFrame
local RulesFrame = ChalkGUI.RulesFrame
local RanksFrame = ChalkGUI.RankFrame
local buttons = StartFrame.buttons

buttons.faces.MouseButton1Click:Connect(function(player)
    if player:GetRankInGroup(groupid) >= rankid then
    
    StartFrame.Visible = false
    
    FacesFrame.Visible = true
    
    FacesFrame.text.Text = faces_text.first
    
    wait(wait_time)
    
    FacesFrame.text.Text = faces_text.second
    
    wait(wait_time)
    
    FacesFrame.text.Text = faces_text.third
    
    wait(wait_time)
    
    FacesFrame.text.Text = faces_text.fourth
    
    wait(wait_time)
    
    FacesFrame.text.Text = faces_text.fifth
    
    wait(wait_time)
    
    FacesFrame.text.Text = faces_text.sixth
    
    wait(wait_time)
    
    FacesFrame.text.Text = faces_text.seventh
    
    wait(wait_time)
    
    FacesFrame.Visible = false
    
    StartFrame.Visible = true
    else
        
    end
end)

buttons.ranks.MouseButton1Click:Connect(function(player)
    if player:GetRankInGroup(groupid) >= rankid then
    
    StartFrame.Visible = false
    
    RanksFrame.Visible = true
    
    RanksFrame.text.Text = ranks_text.first
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.second
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.third
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.fourth
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.fifth
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.sixth
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.seventh
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.eight
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.ninth
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.tenth
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.eleventh
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.twelth
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.thirteenth
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.fourteenth
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.fifteenth
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.sixteenth
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.seventeenth
    
    wait(wait_time)
    
    RanksFrame.text.Text = ranks_text.eighteenth
    
    wait(wait_time)
    
    RanksFrame.Visible = false
    
    StartFrame.Visible = true
    else
        
    end
end)

buttons.rules.MouseButton1Click:Connect(function(player)
    if player:GetRankInGroup(groupid) >= rankid then
    
    StartFrame.Visible = false
    
    RulesFrame.Visible = true
    
    RulesFrame.text.Text = rules_text.first
    
    wait(wait_time)
    
    RulesFrame.text.Text = rules_text.second
    
    wait(wait_time)
    
    RulesFrame.text.Text = rules_text.third
    
    wait(wait_time)
    
    RulesFrame.text.Text = rules_text.fourth
    
    wait(wait_time)
    
    RulesFrame.text.Text = rules_text.fifth
    
    wait(wait_time)
    
    RulesFrame.Visible = false
    
    StartFrame.Visible = true
    else
        
    end
end)

根据错误消息,您正在尝试使用 GetRankInGroup.

索引一个 nil 值

在两行中你这样做:

player:GetRankInGroup(groupId) 其中 player 是一个零值。

当触发 MouseButton1Clicked 事件时,将调用您连接的函数。

buttons.faces.MouseButton1Click:Connect(function(player)
    if player:GetRankInGroup(groupid) >= rankid then
-- ...

player 究竟应该从哪里来?调用事件处理程序时不提供。请参考Roblox手册。

https://developer.roblox.com/en-us/api-reference/class/GuiButton

您会在“活动”下找到

RBXScriptSignal MouseButton1Click ( ) Fired when the mouse has fully left clicked the GUI button

注意括号是空的。

将此与例如进行比较:

RBXScriptSignal MouseButton1Down ( int x , int y ) Fired when the mouse is in the left mouse down state on the GUI object

在这里你可以连接一个function (x,y)