How do i make gangs system?

I think i should add a value in datastore somehow

1 Like

So, if it is a table in the datastore, you could insert the gang name into the datastore by doing:

gangds:SetAsync("key", table.insert(gangds:GetAsync("key"), name))

Typing out a long reply, give me 20min.

Ohh then i should get a player text from textbox and make it in datastore

1 Like

Wait how do i get which text player wrote in textbox?

pass it along with the remote event.

event:FireServer(textbox.Text)

and receive it like this:

event.OnServerEvent:Connect(function(plr, name)

Breeh but i alr have a on server event with Gang Remote event

Just add it as another variable on the OnServerEvent

Hmmm wait, but it will fireserver with textbox text nil, because by default is nil , i need after player entered a name and clicked enter(idk)

local Gangs = {}

local DS = game:GetService("DataStoreService")
local HTTP = game:GetService("HttpService")

local Store = DS:GetDataStore("Gangs_1")

local ActiveGangs = {}
local GangMethods = {} --- Using Metatbles for this.
GangMethods.__index = GangMethods


--- Core with gang internals.

function GangMethods:AddPlayer(PlayerToAdd)
	assert(not PlayerToAdd, "No player given. Cant add nil value.")
	table.insert(self.Players, PlayerToAdd.UserId)
end

function GangMethods:RemovePlayer(PlayerToRemove)
	assert(not PlayerToRemove, "Cannot remove nil player.")
	
	local Player_Index = table.find(self.Players, PlayerToRemove.UserId)
	table.remove(self.Players, Player_Index)
end

function GangMethods:Upgrade() -- Idk how you wanna handle this
	-- Do upgrade stuff here
	print(self.Upgrades)
end


--- All other Core stuff here.

function DefaultGangInfo(Owner, GangName) -- Default Gang Info
	return {
		
		Owner = Owner;
		Name = GangName;
		GangID = HTTP:GenerateGUID();
		
		Players = {}; -- by userid
		Upgrades = {
			
			["Health"] = 1; -- Upgrade Name Level.
		};
		
	}
end

function CheckGangExist(GangName)
	return Store:GetAsync(GangName) and true or false
end

function Gangs.Create(Owner, GangName) -- When player wants to create a new Gang.
	assert(not GangName, "No name given! Cannot create a Gang with no name!")
	assert(not Owner, "Cannot create gang without owner.")
	
	local GangInfo
	
	if not CheckGangExist(GangName) then -- Check if Gang exists.
		GangInfo = DefaultGangInfo(Owner, GangName)
		
		Store:UpdateAsync(Owner.UserId, function(Old)
			return GangInfo -- save new gang to Gang list.
		end)
	end
	return setmetatable(GangInfo, GangMethods)
end


function Gangs.Load(Owner)
	local GangData = Store:GetAsync(Owner.UserId)
	local GangMeta 
	
	if GangData then
		GangMeta = setmetatable(GangData, GangMethods)
		ActiveGangs[GangData.GangID] = GangMeta
	end
	return GangMeta
end


return Gangs
1 Like

My god, that looks so hard omg

Its not to complex, just read the code.

Lemme try that tommorow my brain is already tired from all of that ty

1 Like

If that is hard for you, learn OOP. It will help you understand.

I was trying to figure out a way to not use OOP but it would require a lot of extra memory usage. So, this solution that was the most efficient was this choice, lol.

@PhantomGame_Youtube I have a link to a post I made regarding the theory behind OOP. I will post it below:

Thanks for sharing my faction system!