ModuleScript 被要求并且可以工作,但是功能不起作用(Roblox)
ModuleScript is getting required and working, but the functions don't work (Roblox)
所以基本上,我正在制作一款游戏,按下 E 键即可选择 weapon/item,并且我有 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(防止用户通过触摸拾取 weapon/item。):
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"。
嗨,假设 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)
所以基本上,我正在制作一款游戏,按下 E 键即可选择 weapon/item,并且我有 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(防止用户通过触摸拾取 weapon/item。):
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"。
嗨,假设 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)