ROBLOX | 参数1缺失或为nil?

我在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)

(我得到的错误是:参数1缺失或为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)
点赞
用户8076767
用户8076767

在你的代码部分中:

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.CharacterPLAYER.Name 的使用,使用它的参数 PLAYER 就像它是一个 Player 对象一样。你传递了一个字符串并尝试像一个 Player 对象一样使用它。

修复这个问题的一个方法是将 Player 对象传递给 KICK(),而不是一个字符串。例如:

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

这个修订找到与用户名 'game.Players.' .. PLAYER 相对应的 Player 对象,然后将 传递给 KICK()。现在,虽然这纠正了你代码中更明显的一个问题,但似乎并不完全能够解决你提到的问题,即 "参数1丢失或空值",但是这还不确定。如果你做出这些更改,它是否可以正常工作?

2017-05-31 09:01:15
用户7275671
用户7275671

你正在传递一个字符串并尝试获取其名称,这是错误的。

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

你想将玩家对象/用户数据传递给函数,而不是字符串。

由于 "game.Players.PLAYER" 的名称是 nil,因此错误回复为 "参数缺失或为 nil"。

2018-10-30 01:02:09