Universal Table?

I want to make a code joining system where you can join other players’ clans who are not in your same server. A player would create a code then the player and code would be added to a table. The key would be the player’s ID and the value would be the code. Like this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local classes = {}

local function addClass(player, success)

  local playerId = player.UserId --Get the players UserId

  classes[playerId] = success --Add them to the tabel along with the code

    for key, value in pairs(classes) do

        print(key, value)

    end

end

ReplicatedStorage.AddClass.OnServerEvent:Connect(addClass)

Would I have to make a universal table so other players can join, or a I trying to do this all wrong?

You may want to look into the Messaging Service for this.

I’ve thought about doing this, but what if the owner of the clan is offline. It won’t let them join from my understanding. Please correct me if I’m wrong.

What do you mean by clan? Do you mean a group? Or is it an in game feature?

Yeah an in game feature. Clan/group/party whatever you wanna call it. Something similar to google classroom. You use a code to join a class and the creator of that code is the leader/teacher. I hope I’m making sense, please tell me if you have any more questions.

You can make a seperate datastore for data about classes, here the key would be the code of the clan and the value would be a table containing UserIds of all members. When you want to check if a code is valid you’d request the datastore with the code as a key and check whether there is a stored value. Getting all the members of clan is also just retrieving their UserIds.

local Classes = game:GetService("DataStoreService")

-- Creating a class
local code = ... -- Get the code in whatever way
members = Classes:GetAsync(code)
if not members then
    Classes:SetAsync(code, {Owner.UserId})
else
    -- Tell user the code is taken, class codes must be unique
end

-- Joining a class
local code = ...
members = Classes:GetAsync(code)
if members then
    Classes:UpdateAsync(code, function(members)
        table.insert(members, Player.UserId) -- Player being the one joining
        return members
    end)
end

-- Leaving a class(?)
local code = ...
Classes:UpdateAsync(code, function(members)
    index = table.find(members, Player.UserId)
    table.remove(members, index)
    return members
end)

-- Members of class
local code = ...
members = Classes:GetAsync(code) or {}

One thing to think about is exceeding data request limits etc, you can add some sort of cooldown for joining clans but you’ll definitely have to work this out yourself.

2 Likes

Hey, thanks so much! I was wondering about how you would know who the owner of the class is?

I noticed it said Owner.UserId, but isn’t that the same as doing Player.UserId? Would I just see who the first person in the table is?

I wrote Owner just to show whose UserId is being inserted.

You can check the first UserId in the list but if you’re better off using a dictionary so you can add other information if needed and maintain readable code.

-- When you create the class
Classes:SetAsync(code, {Owner = Owner.UserId, Members = {})

-- Adding a member
Classes:UpdateAsync(code, function(class)
    table.insert(class.Members, Player.UserId) -- Player being the one joining
    return class
end)
1 Like