Roblox 错误,“尝试使用 `GetRankInGroup` 索引 nil”

时间:2021-07-23 17:02:03

标签: lua roblox

我正在制作一个运行演示文稿的黑板。当您单击 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)

1 个答案:

答案 0 :(得分:0)

根据错误消息,您尝试使用 GetRankInGroup 索引一个 nil 值。

在两行中你这样做:

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

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 ( ) 当鼠标完全 左键单击 GUI 按钮

注意括号是空的。

将其与例如:

<块引用>

RBXScriptSignal MouseButton1Down ( int x , int y ) 当 鼠标在 GUI 对象上处于鼠标左键按下状态

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