For my games lobby I plan to have two ways to access menus. The player can either quickly access them through UI or going to an NPC and interacting with it, however, how can I effectively assign an NPC to open specific frames?
So far my method has been to tag an npc with the “NPC” tag and then loop through everything with that tag and using OOP make a new npc object. To assign a frame to a specific npc I have a table to store which npc should open what menu and then pass it as an argument when making the npc object.
The code below works fine, however, I’d like to learn about any better ways I could do this before I end up with a potentially bad system to work with.
local test = {
["StoreNPC"] = "Store",
}
for _, instance in pairs(CollectionService:GetTagged("NPC")) do
NPCTest.new(instance,test[instance.Name])
end
function NPC.new(model,frame)
local self = setmetatable({},NPC)
local function makeCollider(root)
local part = Instance.new("Part")
part.Shape = Enum.PartType.Ball
part.Size = Vector3.new(RADIUS,RADIUS,RADIUS)
part.Anchored = true
part.CanCollide = false
part.CFrame = model:FindFirstChild("HumanoidRootPart").CFrame
part.Transparency = 0.5
Util:WeldParts(part,root)
part.Parent = root
return part
end
self.model = model
self.root = model:FindFirstChild("HumanoidRootPart")
self.frame = frame
self.collider = makeCollider(self.root)
self.zone = Zone.new(self.collider)
self.zone.localPlayerEntered:Connect(function()
self:onColliderTriggered()
end)
return self
end
function NPC:onColliderTriggered()
local frameToOpen = CollectionService:GetTagged(self.model.Name)
print(frameToOpen)
end
return NPC