Roblox Lua无法访问modulescript函数

Roblox Lua无法访问modulescript函数,lua,roblox,Lua,Roblox,我一直在尝试将ModuleScript连接到服务器脚本,但它抛出了错误:Workspace。脚本:6:尝试调用字段“check3”(一个nil值) 模块脚本 local GunStats = {} local part = workspace:WaitForChild('Mosin'):WaitForChild("Union") local billboard = workspace:WaitForChild('BillboardPart'):WaitForChild('BillboardGui

我一直在尝试将ModuleScript连接到服务器脚本,但它抛出了错误:
Workspace。脚本:6:尝试调用字段“check3”(一个nil值)

模块脚本

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("Black") 
    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("IN RANGE")

                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("OUT OF")
                billboard.Enabled = false
            end
        end
    end
end)
return GunStats

这个问题是因为在玩家加入之后,gunStats对象上才定义
gunStats.check3()
函数。我将重新构造您的ModuleScript,以便立即定义
GunStats.check3()

--[[ define all your helper functions up here ... ]]

local GunStats = {}

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

   -- do the checks
end

return GunStats

这个问题是因为在玩家加入之后,gunStats对象上才定义
gunStats.check3()
函数。我将重新构造您的ModuleScript,以便立即定义
GunStats.check3()

--[[ define all your helper functions up here ... ]]

local GunStats = {}

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

   -- do the checks
end

return GunStats