Using a metatable, i can’t see to call an objects method:
Block = {}
Block.__index = Block
function Block.new()
local newblock = {}
setmetatable(newblock, Block)
newblock.part = Instance.new("Part")
newblock.part.Name="myPart"
newblock.part.Position = Vector3.new(-10,0,-10)
newblock.part.Parent = workspace
return newblock
end
function Block:ChangeColour()
self.Color = Color3.fromRGB(190, 183, 80)
end
return Block
-- server script:
local ServerStorage = game:GetService("ServerStorage")
local Block = require(ServerStorage.Block)
local block1 = Block.new()
local b=workspace:FindFirstChild("myPart")
-- this causes an error: ChangeColour is not a valid member of Part "Workspace.myPart"
b:ChangeColour()
First, this is bad practice since there might be multiple parts of the same name in the workspace.
local b=workspace:FindFirstChild("myPart")
It’s better to do this instead
local b = block1.part
Now you are certain you got the right part
Secondly
b:ChangeColour()
This is wrong, because you are referring to a part and then calling ChangeColour on it. This is not possible.
I think you meant this instead, but got a little confused:
block1:ChangeColour()
Edit:
After taking a even more thorough look, I realized you are changing self.Color. I get where this is coming from, but ‘self’ is referring to the newblock table, not the part. So do this instead:
function Block:ChangeColour()
self.part.Color = Color3.fromRGB(190, 183, 80)
end
The change colour function is arbitrary I accept the code in there was wrong, but I’m just trying to figure out how to get the the object in the table from the part so I can call the methods on the part.
And yes I could use block1 but you may not always have that reference - you may just have the part from a collision event for example.
how do I reference the wrapper object and hence its methods once I have the part?