【问题标题】:ROBLOX | Argument 1 missing or nil?机器人 |参数 1 缺失或为零?
【发布时间】:2017-10-22 21:45:21
【问题描述】:

我在lua方面没有太多经验,所以我在这里寻求帮助。我目前正在为一个名为 ROBLOX 的游戏制作脚本,但我在脚本中遇到了一个问题,它来自这里的这个小部分

me.Chatted:connect(function(msg)
    if string.sub(msg,1,5) == "!kick" then
        local PLAYER = (''.. string.sub(msg,6))
        KICK('game.Players.PLAYER')
    end
end)

(我得到的错误是:Argument 1 missing or nil

我有点不知所措,但这是我的脚本的其余部分..

local me = game.Players.LocalPlayer

function KICK(PLAYER)
   spawn(
      function()
         local function SKICK()
            if 
               PLAYER.Character 
               and PLAYER.Character:FindFirstChild('HumanoidRootPart') 
               and PLAYER.Character:FindFirstChild('Torso') 
            then
               local SP = Instance.new('SkateboardPlatform', PLAYER.Character) 
               SP.Position = Vector3.new(1000000, 1000000, 1000000) 
               SP.Transparency = 1
               PLAYER.Character.HumanoidRootPart.CFrame = SP.CFrame
               PLAYER.Character.Torso.Anchored = true
            end
         end
         spawn(
            function()
               repeat 
                  wait()
                  if PLAYER ~= nil then
                     SKICK()
                  end
               until not game:GetService('Players'):FindFirstChild(PLAYER.Name)
               if not game:GetService('Players'):FindFirstChild(PLAYER.Name) then
                  print('REMOVED ' .. PLAYER.Name)
               end
            end
         )
      end
   )
end

那么这就是错误发生的地方

me.Chatted:connect(function(msg)
    if string.sub(msg,1,5) == "!kick" then
        local PLAYER = (''.. string.sub(msg,6))
        KICK('game.Players.PLAYER')
    end
end)

【问题讨论】:

  • 没有行号或其他信息吗?我懒得在你所有的代码中搜索可能的错误。到目前为止你做了什么?您可以打印所有变量以检查其中哪些是 nil
  • 你使用'game.Players.PLAYER'作为字符串,你应该使用它作为一个对象:game.Players[PLAYER],不带引号。
  • 请正确缩进您的代码。

标签: lua roblox


【解决方案1】:

在这部分代码中:

local PLAYER = (''.. string.sub(msg,6))
KICK('game.Players.PLAYER')

您在更改代码时似乎犯了一些语法错误。它应该是这样的:

local PLAYER = string.sub(msg,6)
KICK('game.Players.' .. PLAYER)

除此之外也没有任何意义。您将字符串'game.Players.' .. PLAYER 传递给函数KICK(),但KICK() 使用它的参数PLAYER 就好像它是一个Player 对象,正如您在使用PLAYER.CharacterPLAYER.Name 中看到的那样。你传入一个字符串并尝试像Player 对象一样使用它。

解决此问题的一种方法是将实际的 Player 对象传递给 KICK() 而不是字符串。例如:

local PLAYER = string.sub(msg,6)
KICK(game:GetService('Players'):FindFirstChild('game.Players.' .. PLAYER))

此修订版找到与用户名'game.Players.' .. PLAYER 对应的 Player 对象,然后将 that 传递给KICK()。现在,虽然这解决了您的代码中一个更明显的问题,但它似乎并不能完全解决您引用的问题,“Argument 1 missing or nil”,但您永远不知道。如果您进行这些更改,它是否可以正常工作?

【讨论】:

    【解决方案2】:

    (抱歉这么晚才回复)

    我在这个脚本上看到的第一个问题是它是一个 LocalScript,或者至少看起来是这样的,因为你做了game.Players.LocalPlayer。问题在于 LocalScripts 根本无法踢出玩家,或以任何方式伤害他们。这就是为什么大多数管理员/踢命令使用服务器端系统,换句话说,使用脚本而不是本地脚本。


    我强烈建议您将其转换为 ModuleScript 或 Script,这样您可以更轻松地使用它。你知道吗?我会为你做的,所以不用担心。


    首先,我需要知道您的 ROBLOX 名称。假设是 John Doe,现在,如果您的用户名不同(这当然很有可能),您只需将 John Doe 更改为您的 ROBLOX 用户名。

    让我们开始吧!


    把它变成一个ModuleScript

    起初听起来可能是一项艰巨的任务,有时也可能如此,但由于这个脚本非常简单,所以没问题。

    首先,在名为“MainModule”的 ModuleScript 中,粘贴此代码(如果不是 John Doe,请将 John Doe 更改为您的 ROBLOX 用户名。)

    请注意,使用 ModuleScripts 在 Script Builders 之类的地方非常有用,甚至在您自己的游戏中!

    还要注意,这个 ModuleScript 要想正常工作,只能通过脚本运行,不能通过 LocalScripts 运行。

    local username = "John Doe";
    
    local connected = false;
    local Players = game:service("Players");
    local p = nil;
    function kick(name)
        for _,v in pairs(Players:GetPlayers())do
            if v.Name == name then
                v:Kick("You've been kicked.");
            end;
        end;
    end;
    
    for _,v in pairs(Players:GetPlayers())do
        if v.Name == username then
            p = v;
        end;
    end;
    
    Players.PlayerAdded:connect(function(plr)
        if plr.Name == username then
            p = plr;
        end;
    end;
    
    Players.PlayerRemoving:connect(function(plr)
        if plr.Name == username then
            p = nil;
            connected = false;
        end;
    end;
    
    coroutine.wrap(function()
        while wait()do
            local r,e = ypcall(function()
                if p ~= nil then
                    if not connected then
                        connected = true;
                        p.Chatted:connect(function(msg)
                            if msg:sub(1,string.len("kick"))then
                                local other = tostring(msg:sub(string.len("kick ")));
                                local search = other:lower();
    
                                kick(search);
                            end;
                        end);
                    end;
                end;
            end);
            if e then
                warn(":ERROR: "..tostring(e));
            end;
        end;
    end)();
    return nil;
    

    应该为模块做到这一点! 要执行此代码,您必须按照以下步骤操作:

    首先,在 Workspace 中创建一个 ModuleScript(这是在 Roblox Studio 中完成的),现在,在 ModuleScript 中,粘贴我刚刚编写的代码。现在,将此 ModuleScript 从“ModuleScript”重命名为“MainModule”。现在像发布模型一样发布此脚本。

    之后,您会看到脚本有一个 ID(例如,12345678)。此 ID 位于模型的 URL 中。这个ID是你需要的。复制 ID,然后将 ID 粘贴到这个小脚本 require(<enter ID here, replacing me>); 中的 parentesys 之间。它应该有点像require(12345678);

    如果您愿意在 Script Builder 中执行 kick 脚本,您只需在聊天或命令栏中输入以下内容:c/ + require(); 我告诉过你这样做.它应该看起来有点像c/require(12345678);。在此之后,只需按 Enter,您的 kick 命令脚本就会运行!踢球员所需要做的就是说“踢”和他们的名字!请注意,这不区分大小写,但您必须写下玩家的全名。

    例子:

    • 踢 LegendOfDarknees
    • 踢左腿膝盖
    • 踢 LEGENDoFdARKNEES

    如果您在运行模块后在游戏聊天中说出这些示例中的任何一个,脚本将尝试踢出名为“LegendOfDarknees”的玩家(就是我!),但是,如果玩家不在服务器上,脚本不会做任何事情。

    另外,请注意脚本将永远在服务器中运行! (或直到服务器关闭)。这意味着您可以尝试踢自己或重新加入,您仍然可以踢球员!

    (请不要滥用)


    把它变成一个脚本(对游戏很有用)

    有两种方法,一种是按照我在把它变成ModuleScript中说的方法,你所要做的就是在Workspace中创建一个脚本,在脚本中,只需输入require(<ID of the module>);。它应该看起来有点像require(12345678);。脚本会自动执行模块,无需手动激活。

    现在,有一种替代方法,这不是我最喜欢的,因为利用者更容易获取此脚本。

    为此,您只需在 Workspace 中创建一个脚本。现在,在脚本中粘贴以下代码:

    local username = "John Doe";
    
    local connected = false;
    local Players = game:service("Players");
    local p = nil;
    function kick(name)
        for _,v in pairs(Players:GetPlayers())do
            if v.Name == name then
                v:Kick("You've been kicked.");
            end;
        end;
    end;
    
    for _,v in pairs(Players:GetPlayers())do
        if v.Name == username then
            p = v;
        end;
    end;
    
    Players.PlayerAdded:connect(function(plr)
        if plr.Name == username then
            p = plr;
        end;
    end;
    
    Players.PlayerRemoving:connect(function(plr)
        if plr.Name == username then
            p = nil;
            connected = false;
        end;
    end;
    
    coroutine.wrap(function()
        while wait()do
            local r,e = ypcall(function()
                if p ~= nil then
                    if not connected then
                        connected = true;
                        p.Chatted:connect(function(msg)
                            if msg:sub(1,string.len("kick"))then
                                local other = tostring(msg:sub(string.len("kick ")));
                                local search = other:lower();
    
                                kick(search);
                            end;
                        end);
                    end;
                end;
            end);
            if e then
                warn(":ERROR: "..tostring(e));
            end;
        end;
    end)();
    

    它可能看起来与模块一非常相似,因为它们是相同的代码,唯一的区别是这个模块最后没有return nil;。这是因为这个脚本不必向执行它的脚本返回一些东西,而 ModuleScript 是强制的,否则,ModuleScript 将不会运行。


    所以,这应该可以回答您的问题。希望大家看完,因为篇幅很长,读起来可能会觉得无聊或沮丧,但这次,它包含了很多重要的信息,所以我强烈建议你阅读它。

    再次抱歉,这么晚才回复,我之前无法回复。我希望我不会太晚。

    如果您在我通过此答案发送给您的代码中遇到任何错误/故障/错误,请随时在此答案的 cmets 中告诉我,如果您需要更多信息,我可以编辑答案,或评论您的发帖给你更多信息。希望这能回答你所有的问题,还有更多。

    祝你现在和未来的项目好运!

    【讨论】:

    • 这非常彻底和有用!我想这对于未来的访问者来说将是一个非常宝贵的资源。
    【解决方案3】:

    您正在传递一个字符串并试图为其命名,这很糟糕。

    您应该将KICK('game.Players.PLAYER') 更改为KICK(game:GetService("Players")[PLAYER])

    您想将播放器对象/用户数据传递给函数而不是字符串。

    由于“game.Players.PLAYER”.Name 为 nil,因此错误回复“argument missing or nil”

    【讨论】:

      猜你喜欢
      • 2018-07-14
      • 2020-09-20
      • 2011-12-15
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 2015-06-01
      相关资源
      最近更新 更多