【问题标题】:ModuleScript is getting required and working, but the functions don't work (Roblox)ModuleScript 越来越需要并且可以正常工作,但是这些功能不起作用(Roblox)
【发布时间】:2023-03-21 22:22:01
【问题描述】:

所以基本上,我正在制作一款使用 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”。

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    嘿嘿,假设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)
    

    【讨论】:

    • 感谢您解决我的问题,非常感谢 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 2021-06-14
    • 2021-12-05
    • 1970-01-01
    • 2012-01-02
    • 2016-06-18
    • 2017-02-25
    相关资源
    最近更新 更多