【发布时间】: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”。
【问题讨论】: