ModuleScript必不可少且可以正常运行,但功能不起作用(Roblox)

时间:2019-07-26 15:00:17

标签: lua roblox

因此,基本上,我正在使用Press E制作游戏来拾取武器/物品,并且我有2个脚本来处理所有需要完成的工作。但是,每当我尝试从ServerScript在ModuleScript内部运行一个函数时,它就无法正常工作。

我已经尝试过将打印函数放在ModuleScript的函数中,但似乎甚至无法打印到控制台。我还检查了两个脚本,以查看可能导致此问题的任何问题,但找不到。

两个脚本都还没有完全完成,但是做得足以让您有一个大致的认识。

ServerScript(主脚本):

print("[GameName]: ToolHandler is initializing, please wait...")
--Getting required services within the game for later use. This still works even if the names aren't original.
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local SoundService = game:GetService("SoundService")
local UserInputService = game:GetService("UserInputService")

--Getting required instances needed for the script to work.
local Tools = Workspace:WaitForChild("Tools")
local GameContent = ReplicatedStorage:WaitForChild("GameContent")
local GameModules = GameContent:WaitForChild("GameModules")
local GameEvents = GameContent:WaitForChild("GameEvents")
local DestroyTouchModuleScript = GameModules:WaitForChild("DestroyTouchModule")
local GetItemEvent = GameEvents:WaitForChild("GetItem")

--Getting children of any instance if needed.
local AllTools = Tools:GetChildren()

--Requiring Module Scripts if any.
local DestroyTouchModule = require(DestroyTouchModuleScript)

--ChildAdded functions
Tools.ChildAdded:connect(function(Child)
    if Child:IsA("Tool") then
        DestroyTouch({Child})
    end
end)

Workspace.ChildAdded:connect(function(Child)
    if Child:IsA("Tool") then
        Child.Parent = Tools
    end
end)

--Functions that are required for the script to run efficently.
function DestroyTouch(List)
    for I = 1, #List do
        local Handle = List[I]:WaitForChild("Handle")
        local Tool = Handle.Parent
        if Handle ~= nil then
            local TouchInterest = Handle:WaitForChild("TouchInterest")
            if not TouchInterest then
                DestroyTouchModule.DestroyTouch(Tool)
            end
        end
    end
end

-- Initializing done
DestroyTouch(AllTools)

print("[GameName]: ToolHandler has initialized successfully.")

ModuleScript(防止用户通过触摸来拾取武器/物品。):

local DestroyTouchModule = {}

print("DestroyModule: Initializing, please wait...")
function DestroyTouchModule.DestroyTouch(ToolRequired)
    print("a") --This is when I tried to see if it was a actual problem with the function or the code.
    local Handle = ToolRequired:WaitForChild("Handle")
    local TouchInterest = Handle:WaitForChild("TouchInterest")
    if Handle and TouchInterest then
        TouchInterest:Destroy()
        print("Tool: Successfully removed TouchInterest from Handle.")
    else
        warn("Tool: Handle or TouchInterest is missing, no work is required.")
    end

    Handle.ChildAdded:connect(function(Child)
        if Child:IsA("TouchTransmitter") then
            Child:Destroy()
        end
    end)
end

print("DestroyModule: Initialized with no issue.")

return DestroyTouchModule

可以预期的是,当函数运行时,我应该在控制台中看到“ a”,但我没有。实际上,我在函数内部的日志中看不到任何内容。

我在输出面板中看到的是: Output Image

请注意,控制台内没有任何“ a”。

1 个答案:

答案 0 :(得分:0)

Heyo,假设Child:IsA("Tool")正常工作,这应该是解决问题的简单更改。 DestroyTouch是DestroyTouchModule的功能,您只需要正确调用即可:

Tools.ChildAdded:connect(function(Child)
    if Child:IsA("Tool") then
        -- Use the DestroyTouchModule to add event listeners to the Child.
        DestroyTouchModule.DestroyTouch(Child)
    end
end)