Roblox Lua 无法访问 modulescript 函数

我一直在尝试将一个 ModuleScript 连接到服务器 Script 上,但是它会抛出错误:Workspace.Script:6: attempt to call field 'check3' (a nil value)

ModuleScript

local GunStats = {}
local part = workspace:WaitForChild('Mosin'):WaitForChild("Union")
local billboard = workspace:WaitForChild('BillboardPart'):WaitForChild('BillboardGui')
local uis = game:GetService("UserInputService")
local Ekey = Enum.KeyCode.E
local check = false
local start = tick()

local function onpress(action1)
    if check then
        part.BrickColor = BrickColor.new("黑色")
    end
end

local function isKeydown(startTime)
    return uis:IsKeyDown(startTime) and startTime - tick() <= 5
end

local function Input(input, gameprocessed)
    if isKeydown(Ekey) then
        print("h")
    else
        print("n")
    end
    start = tick()
end

game.Players.PlayerAdded:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    local humRoot = character:WaitForChild("HumanoidRootPart")

    uis.InputBegan:Connect(Input)
    GunStats.check3 = function()
        while wait() do
            if (humRoot.Position - part.Position).Magnitude < 5 then
                print("在范围内")

                check = true
                billboard.Enabled = true
                repeat wait() until (humRoot.Position - part.Position).Magnitude >= 5
            elseif (humRoot.Position - part.Position).Magnitude > 5 and check then
                check = false
                print("不在范围内")
                billboard.Enabled = false
            end
        end
    end
end)
return GunStats

Server Script:

local players = game:GetService("Players")

local serverStorage = game.ServerStorage
local gunStats = require(serverStorage:WaitForChild("ModuleScript"))
game.Players.PlayerAdded:Connect(function(players)
    gunStats.check3(players)
end)
点赞
用户2860267
用户2860267

这个问题出现在 gunStats.check3() 函数在玩家加入之前并没有在 GunStats 对象中定义。我建议重新构建你的 ModuleScript,以便将 GunStats.check3() 定义在第一时间:

--[[ 在这里定义所有辅助函数... ]]

local GunStats = {}

function GunStats.check3(player)
    -- 访问 humanoid
    local character = player.Character or player.CharacterAdded:Wait()
    local humRoot = character:WaitForChild("HumanoidRootPart")

    -- 进行检查
end

return GunStats
2020-03-26 08:00:16