The issue could be due to a lack of understanding so I would appreciate detailed answers as I am fairly new to OOP.
To begin, let’s imagine we create a new part using our constructor, how would I then go about referencing that object & manipulating any values associated with it. under my current system I would need to set the values at first and cannot manipulate them further whatsoever as I have no way to accurately reference the created object.
local Tool = {}
Tool.__index = Tool
function Tool.new(player)
local NewTool = {}
setmetatable(NewTool, Tool)
NewTool.Owner = player
--Create Tool below here
local CreateInstance = Instance.new("Tool")
CreateInstance.Parent = player.Backpack
return NewTool
end
return Tool
local Players = game:GetService("Players")
--// Modules \\--
local ToolModule = require(script.Tool)
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local NewTool = ToolModule.new(player)
end)
end)
Ideally I’d be able to reference this new object and change any values whenever on the go, is this possible?
EDIT
To clarify, I am not meaning using a function to go Part:ChangeValue(…) I am talking about referencing the “Part”
Y’know how you call the function and set NewTool in your send script to it? Well, since you’re returning NewTool in that function, the variable is equal to the return value, which is the object itself, and that’s what you use to reference it.
I think I’m misunderstanding what I’m trying to do, the module script itself cannot save the data and store it for later right? I would need to then store that inside a table on the server script?
My original plan was to have the module script keep track of each item so I could simply require it and check that table like Module.Table1[1]
Are u saying I should then set the " NewTool " in my server script to a table? How would I then differentiate between this and the next " NewTool " that gets created when another player joins?
If I understand your question correctly, you’re trying to get the reference to a specific OOP object?
Usually what I’d do is add the object to a table in the same ModuleScript that has the Object.new function, something like this, where the table CreatedTools keeps track of all the tools:
local Tool = {}
Tool.__index = Tool
local CreatedTools = {}
function Tool.new(player)
local NewTool = setmetatable({}, Tool)
NewTool.Owner = player
local CreateInstance = Instance.new("Tool")
CreateInstance.Parent = player.Backpack
-- Add tool to the CreatedTools table
CreatedTools[NewTool.Owner] = NewTool
return NewTool
end
return Tool
Then I would add some kind of :GetTool() function:
local Tool = {}
Tool.__index = Tool
local CreatedTools = {}
function Tool:GetTool(Player) -- Get the specific tool by using the player
if CreatedTools[Player] then
return CreatedTools[Player]
end
end
function Tool.new(player)
local NewTool = setmetatable({}, Tool)
NewTool.Owner = player
local CreateInstance = Instance.new("Tool")
CreateInstance.Parent = player.Backpack
-- Add tool to the CreatedTools table
CreatedTools[NewTool.Owner] = NewTool
return NewTool
end
return Tool
Then to actually get the tool, you would use :GetTool(Player):
This is assuming you don’t already have access to the OOP object in a variable, and you need some way to get it.
-- Script A
local NewTool = ToolModule.new(Players.Player1)
-- Different Script, or a different scope
ToolModule:GetTool(Players.Player1)
Store the created objects in a dictionary where the keys are player UserIds, as they are guaranteed to be unique. In addition, you can add an Owner or UserId property in the objects created.
-- This table could be in your Tool module if you want other scripts to access it
-- or your main script
local toolStorage = {
[123456789] = obj1,
[123456788] = obj2
}