Search code examples
moduleluaroblox

My code cant find a function in my module script Roblox


I'm writing a 2D game engine in Roblox for fun, but I have run into an issue. It seems my code cant find the function I'm referencing.

My output says:

Players.SpookyDervish.PlayerGui.SpookyEngine.SpookyEngine:31: attempt to call a nil value

I have 3 module scripts and a main script.

Here is my explorer window: A screenshot of my explorer window

The main script:

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)

My SpookyEngine module:

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

My object module:

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

Any help would be appreciated!


Solution

  • Your error is pointing at this line :

    function spookyEngine:Transform(object, pos, size, rotation)
        object:Transform(nil, nil, nil, nil)
    end
    

    which is called by...

    spookyEngine:Transform("test", Vector2.new(50, 50), Vector2.new(50, 50), 0)
    

    Because you have a function argument named object, it shadows the object local variable that you used to define your module. So the string that you pass in is what gets used. So you are effectively trying to execute

    string.Transform("test", nil, nil, nil, nil)
    

    The lua string library does not have a function called Transform, so when you try to index the function, it comes back as nil. That is why you get the error attempt to call a nil value.

    Since you intended to use the Object:Transform function from your module, the easy fix is to rename the function argument to avoid the name collision. Try something like :

    function spookyEngine:Transform(obj, pos, size, rotation)
        object:Transform(obj, pos, size, rotation)
    end