我的代码在我的模块脚本 Roblox 中找不到函数
My code cant find a function in my module script Roblox
我在 Roblox 中编写 2D 游戏引擎是为了好玩,但我遇到了 运行 问题。
看来我的代码找不到我引用的函数。
我的输出是:
Players.SpookyDervish.PlayerGui.SpookyEngine.SpookyEngine:31: attempt to call a nil value
我有 3 个模块脚本和一个主脚本。
这是我的浏览器 window:
主脚本:
local gui = script.Parent
local spookyEngine = require(gui:WaitForChild("SpookyEngine"))
spookyEngine.Init()
spookyEngine:CreateObject("TestObject", Vector2.new(0, 0), Vector2.new(50, 50), "rbxassetid://183598555", Color3.fromRGB(85, 170, 255))
wait(2)
spookyEngine:Transform("test", Vector2.new(50, 50), Vector2.new(50, 50), 0)
我的 SpookyEngine 模块:
local object = require(script:WaitForChild("Object"))
local input = require(script:WaitForChild("Input"))
local gui = script.Parent
local spookyEngine = {}
function spookyEngine.Init()
spookyEngine.screen = Instance.new("Frame", gui)
spookyEngine.screen.Name = "Screen"
spookyEngine.screen.Position = UDim2.new(0.5, 0, 0.5, 0)
spookyEngine.screen.Size = UDim2.new(1.25, 0, 1.25, 0)
spookyEngine.screen.AnchorPoint = Vector2.new(0.5, 0.5)
spookyEngine.screen.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
spookyEngine.screen.BorderSizePixel = 0
spookyEngine.screen.BorderColor3 = Color3.fromRGB(0, 0, 0)
object.Init()
spookyEngine.objects = object.objects
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
print("INFO: Initialized SpookyEngine!")
end
function spookyEngine:CreateObject(name, pos, size, sprite, colour)
object.New(name, pos, size, sprite, colour)
end
function spookyEngine:Transform(object, pos, size, rotation)
object:Transform(nil, nil, nil, nil)
end
return spookyEngine
我的对象模块:
local spookyEngine = script.Parent
local gui = spookyEngine.Parent
local Object = {}
Object.__index = Object
function Object.Init()
local Objects = Instance.new("Folder", gui.Screen)
Objects.Name = "Objects"
Object.objects = Objects
end
function Object.New(name, pos, size, sprite, colour)
local newObject = {}
setmetatable(newObject, Object)
local position = UDim2.new(0.5, pos.X, 0.5, pos.Y)
local objectSize = UDim2.new(0, size.X, 0, size.Y)
local newObjectInstance = Instance.new("Frame", Object.objects)
newObjectInstance.Name = name
newObjectInstance.Position = position
newObjectInstance.Size = objectSize
newObjectInstance.BackgroundColor3 = colour
newObjectInstance.AnchorPoint = Vector2.new(0.5, 0.5)
newObjectInstance.BorderSizePixel = 0
if sprite ~= nil then
local objectSprite = Instance.new("ImageLabel", newObjectInstance)
objectSprite.Size = UDim2.new(1, 0, 1, 0)
objectSprite.Name = "Sprite"
objectSprite.Image = sprite
objectSprite.BackgroundTransparency = 1
objectSprite.BackgroundColor3 = colour
newObjectInstance.BackgroundTransparency = 1
end
newObject.Name = name
newObject.Position = position
newObject.Size = objectSize
newObject.Sprite = sprite
newObject.Colour = colour
newObject.Instance = newObjectInstance
return newObject
end
function Object:Transform(object, pos, size, rotation)
object = tostring(object)
if Object.objects:FindFirstChild(object) then
else
warn("ERROR: Cant find object with name: '"..object.."'")
end
end
return Object
如有任何帮助,我们将不胜感激!
你的错误指向这一行:
function spookyEngine:Transform(object, pos, size, rotation)
object:Transform(nil, nil, nil, nil)
end
调用者...
spookyEngine:Transform("test", Vector2.new(50, 50), Vector2.new(50, 50), 0)
因为您有一个名为 object
的函数参数,它隐藏了您用来定义模块的 object
局部变量。所以你传入的字符串就是被使用的字符串。所以你实际上是在尝试执行
string.Transform("test", nil, nil, nil, nil)
lua 字符串库没有名为 Transform
的函数,因此当您尝试索引该函数时,它返回为 nil
。这就是您收到错误 attempt to call a nil value
.
的原因
由于您打算使用模块中的 Object:Transform
函数,因此简单的解决方法是重命名函数参数以避免名称冲突。尝试类似的东西:
function spookyEngine:Transform(obj, pos, size, rotation)
object:Transform(obj, pos, size, rotation)
end
我在 Roblox 中编写 2D 游戏引擎是为了好玩,但我遇到了 运行 问题。 看来我的代码找不到我引用的函数。
我的输出是:
Players.SpookyDervish.PlayerGui.SpookyEngine.SpookyEngine:31: attempt to call a nil value
我有 3 个模块脚本和一个主脚本。
这是我的浏览器 window:
主脚本:
local gui = script.Parent
local spookyEngine = require(gui:WaitForChild("SpookyEngine"))
spookyEngine.Init()
spookyEngine:CreateObject("TestObject", Vector2.new(0, 0), Vector2.new(50, 50), "rbxassetid://183598555", Color3.fromRGB(85, 170, 255))
wait(2)
spookyEngine:Transform("test", Vector2.new(50, 50), Vector2.new(50, 50), 0)
我的 SpookyEngine 模块:
local object = require(script:WaitForChild("Object"))
local input = require(script:WaitForChild("Input"))
local gui = script.Parent
local spookyEngine = {}
function spookyEngine.Init()
spookyEngine.screen = Instance.new("Frame", gui)
spookyEngine.screen.Name = "Screen"
spookyEngine.screen.Position = UDim2.new(0.5, 0, 0.5, 0)
spookyEngine.screen.Size = UDim2.new(1.25, 0, 1.25, 0)
spookyEngine.screen.AnchorPoint = Vector2.new(0.5, 0.5)
spookyEngine.screen.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
spookyEngine.screen.BorderSizePixel = 0
spookyEngine.screen.BorderColor3 = Color3.fromRGB(0, 0, 0)
object.Init()
spookyEngine.objects = object.objects
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
print("INFO: Initialized SpookyEngine!")
end
function spookyEngine:CreateObject(name, pos, size, sprite, colour)
object.New(name, pos, size, sprite, colour)
end
function spookyEngine:Transform(object, pos, size, rotation)
object:Transform(nil, nil, nil, nil)
end
return spookyEngine
我的对象模块:
local spookyEngine = script.Parent
local gui = spookyEngine.Parent
local Object = {}
Object.__index = Object
function Object.Init()
local Objects = Instance.new("Folder", gui.Screen)
Objects.Name = "Objects"
Object.objects = Objects
end
function Object.New(name, pos, size, sprite, colour)
local newObject = {}
setmetatable(newObject, Object)
local position = UDim2.new(0.5, pos.X, 0.5, pos.Y)
local objectSize = UDim2.new(0, size.X, 0, size.Y)
local newObjectInstance = Instance.new("Frame", Object.objects)
newObjectInstance.Name = name
newObjectInstance.Position = position
newObjectInstance.Size = objectSize
newObjectInstance.BackgroundColor3 = colour
newObjectInstance.AnchorPoint = Vector2.new(0.5, 0.5)
newObjectInstance.BorderSizePixel = 0
if sprite ~= nil then
local objectSprite = Instance.new("ImageLabel", newObjectInstance)
objectSprite.Size = UDim2.new(1, 0, 1, 0)
objectSprite.Name = "Sprite"
objectSprite.Image = sprite
objectSprite.BackgroundTransparency = 1
objectSprite.BackgroundColor3 = colour
newObjectInstance.BackgroundTransparency = 1
end
newObject.Name = name
newObject.Position = position
newObject.Size = objectSize
newObject.Sprite = sprite
newObject.Colour = colour
newObject.Instance = newObjectInstance
return newObject
end
function Object:Transform(object, pos, size, rotation)
object = tostring(object)
if Object.objects:FindFirstChild(object) then
else
warn("ERROR: Cant find object with name: '"..object.."'")
end
end
return Object
如有任何帮助,我们将不胜感激!
你的错误指向这一行:
function spookyEngine:Transform(object, pos, size, rotation)
object:Transform(nil, nil, nil, nil)
end
调用者...
spookyEngine:Transform("test", Vector2.new(50, 50), Vector2.new(50, 50), 0)
因为您有一个名为 object
的函数参数,它隐藏了您用来定义模块的 object
局部变量。所以你传入的字符串就是被使用的字符串。所以你实际上是在尝试执行
string.Transform("test", nil, nil, nil, nil)
lua 字符串库没有名为 Transform
的函数,因此当您尝试索引该函数时,它返回为 nil
。这就是您收到错误 attempt to call a nil value
.
由于您打算使用模块中的 Object:Transform
函数,因此简单的解决方法是重命名函数参数以避免名称冲突。尝试类似的东西:
function spookyEngine:Transform(obj, pos, size, rotation)
object:Transform(obj, pos, size, rotation)
end