So I have a module script in replicated storage called CustomClass.

Here is the module script:

local CustomClass = {}
CustomClass.__index = CustomClass

CustomClass.WeaponSlotButton = game.ReplicatedStorage.GuiAssets.CustomClass.WeaponSlotButton

function CustomClass:Update()
	
end

function CustomClass:New(player, ClassSelection, Info, i)
	self = setmetatable(self, CustomClass)
	
	if Info then
		self.Name = Info.Name
	else
		self.Name = "CustomClass1"
	end
	print(self.Name)
	self.WeaponSlotButton = CustomClass.WeaponSlotButton:Clone()
	self.WeaponSlotButton.Name = self.Name
	self.WeaponSlotButton.Text = "CustomClass "..i
	self.WeaponSlotButton.Parent = ClassSelection
	
	self.WeaponSlotButton.MouseButton1Click:Connect(function() CustomClass:ClassSelected(player, self.WeaponSlotButton) end)
end

local CustomClassInfo = game:GetService("ReplicatedStorage").PlayerData:WaitForChild("CustomClassInfo")

function CustomClass:ClassSelected (player, ClassSelected)
	-- Took Out the gui stuff so it's shorter and easier to read 
	-- Fetch Custom Class Information
	local CustomClassData = CustomClassInfo:InvokeServer(ClassSelected.Name)
end

return CustomClass

I have a local script that when I press AddCustomClass it creates a new custom class with the WeaponSlotButton as the button you select to go into the weapon class. I wanted to do oop so I wouldn't have to have a script in every button when I cloned it. It works fine.

Here's the local script:

			local function CreateNewClass()
				CustomClassData = NewCustomClass:InvokeServer(player)
				CustomClassData["CustomClass"..CustomClassData.CustomClassNumber] = CustomClass:New(player, 
																					ClassSelection,CustomClassData["CustomClass"..CustomClassData.CustomClassNumber], 
																					CustomClassData.CustomClassNumber)								
			end
			
			AddCustomClass.MouseButton1Click:Connect(CreateNewClass)

Here is the module script in serverscriptservice that Handles events and data. Sorry I figured having it all would make it easier to understand what the tables look like:

-- Replicated Storage
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	local PlayerData = ReplicatedStorage.PlayerData
	
-- Services
	local ContextActionService = game:GetService("ContextActionService")
	local DataStoreService = game:GetService("DataStoreService")
	local PlayerDataStore = DataStoreService:GetDataStore("TestDataStoreBR3")
	
	-- Tables
		local module = {}
		
		local CustomClass = {
			Name = nil,
			Primary = "Pistol",
			Secondary = {},
			Tactical = nil,
			Lethal = nil,
			Perk = nil,
		}
--------------------------------------------------------------------------------------------------------
-- Custom Loadouts/Custom Classes
--------------------------------------------------------------------------------------------------------
	-- Remote Functions
		local NewCustomClass = PlayerData.NewCustomClass
		local CustomClassInfo = PlayerData.CustomClassInfo
		
		-- New Custom Class
		NewCustomClass.OnServerInvoke = function(player)
			-- Increment Number of Classes
			module[""..player.UserId].CustomClasses.CustomClassNumber = module[""..player.UserId].CustomClasses.CustomClassNumber + 1
			
			-- Create a table to hold the data 
			local NewClass
			NewClass = CustomClass
			NewClass.Name = "CustomClass"..module[""..player.UserId].CustomClasses.CustomClassNumber
			
			-- Create a new class inside module and make it equal to NewClass
			module[""..player.UserId].CustomClasses[""..NewClass.Name] = NewClass
			
			return module[""..player.UserId].CustomClasses
			end
			
		-- Get players Classes Table
		CustomClassInfo.OnServerInvoke = function(player, CustomClassName)
			module[""..player.UserId].CustomClasses[CustomClassName].Name = CustomClassName
			return module[""..player.UserId].CustomClasses[CustomClassName]
		end	
	

--------------------------------------------------------------------------------------------------------
-- Player Loads In
--------------------------------------------------------------------------------------------------------
	-- New Character
	local function NewCharacterData(player, data)
		module[""..player.UserId] = {
				CustomClasses = {
					CustomClassNumber = 0
				},
				FriendsList = {
				}
			}
	end
	
	-- Adds PlayerData To module
		local CreateFriendsList = PlayerData.CreateFriendsList
		
		function module.AddPlayerData(player)
			local playerData = PlayerDataStore:GetAsync(""..player.UserId)
			if  playerData then
				module[""..player.UserId] = playerData		
			else
				NewCharacterData(player)
			end
		end
		
	-- Player Loads in and retrieves data
	local RetrievePlayerData = PlayerData.RetrievePlayerData
	
	RetrievePlayerData.OnServerInvoke = function(player)
		return module[""..player.UserId]
	end
--------------------------------------------------------------------------------------------------------
-- Player leaves
--------------------------------------------------------------------------------------------------------	
	-- Save Player Data
	function module.SavePlayerData(player)	
		local Table = module[""..player.UserId]
		print(game:GetService("HttpService"):JSONEncode(module[""..player.UserId].CustomClasses))
		PlayerDataStore:SetAsync(""..player.UserId, Table)
	end
	
return module

PROBLEM :

I get this output where when I create a new class the classes name is changed to the new number of classes: Here's the output from MyFunctions module script when I save the player data at the end and print it out:

 {
 -- Name should be CustomClass1
"CustomClass1":{"Primary":"Pistol","Secondary":[],"Name":"CustomClass2"},

"CustomClass2":{"Primary":"Pistol","Secondary":[],"Name":"CustomClass2"},

"CustomClassNumber":2}

The weird thing is self.Name is only printed out once when I add a class. Where the print statment is in CustomClass:New

This works when I create only one new button. So for example if I only hit AddCustomClass once I would get:

"CustomClass1":{"Primary":"Pistol","Secondary":[],"Name":"CustomClass1"},

Also the name of the button stays the same. So the button CustomClass1 in the PlayerGui would still be CustomClass1 when CustomClass2 was created.

1 comment

0

This is just a draft so I haven't made it very useful other than allowing me to make multiple buttons that I can click without putting scripts in each one.