#Instance function bug? (maybe)

12 messages · Page 1 of 1 (latest)

lucid skiff
#

Either this is a bug or my bad code, but when trying to run :test() on the frame returned, it says it isn't a valid member of Frame. How do I fix this (note: it also doesn't appear in the code editor)

function funcs:createCanvas()
    local canvas = Instance.new("Frame")
    canvas.Size = UDim2.new(1,0,1,0)
    canvas.AnchorPoint = Vector2.new(.5,.5)
    canvas.Position = UDim2.new(.5,0,.5,0)
    canvas.Parent = game.Players.LocalPlayer.PlayerGui.AppGui
    function canvas:test()
        print("hi")
    end
    return canvas
end

Full code just includes local funcs = {} and return funcs at the top and bottom

zenith pulsar
#

wth is :test()

lucid skiff
#

im just trying to add a function to the frame

#

because i want to add functions and also allow them to change frame properties from the same object

flint garnet
#

Or objects created from those classes

#

You need to wrap canvas in a table, then you can add functionality to that table.

flint garnet
flint garnet
# lucid skiff how do i wrap it in a table
function funcs:createCanvas()
    local canvas = Instance.new("Frame")
    canvas.Size = UDim2.new(1,0,1,0)
    canvas.AnchorPoint = Vector2.new(.5,.5)
    canvas.Position = UDim2.new(.5,0,.5,0)
    canvas.Parent = game.Players.LocalPlayer.PlayerGui.AppGui

    local canvasObject = { Canvas = canvas }
    function canvasObject:test()
        print("hi")
    end
    return canvasObject
end

local myCanvas = funcs:createCanvas()
-- Accessing "canvas" instance from returned table
myCanvas.Canvas.Size = UDim2.fromScale(1, 1)
myCanvas:test()
#

You could setup some metatable trickery to index the canvas stored inside the table before indexing the table itself so you can kinda of just treat the table as the object itself