Using tables to make a Party System

So I just scripted an unfinished party system and I want to make it so that the usernames of the players inside of the table, will be displayed on a GUI. How do I do this?

local swapfunc = false
local button = script.Parent.TextButton
local uis = game:GetService('UserInputService')
local repstorage = game:GetService('ReplicatedStorage')
local countdown = script.Parent.CountDown
local countdownTime = 60
local countingdown = true


button.MouseButton1Click:Connect(function()
	if swapfunc == false then
		swapfunc = true
		button.Text = "Leave Party"
		repstorage.JoinedGame:FireServer()

	else
		swapfunc = false
		button.Text = "Join Party"
		repstorage.LeftGame:FireServer()
	end
end)

local function startCountdown()
	countingdown = true
	for i = countdownTime, 0, -1 do
		print("Countdown: " .. i)
		countdown.Text = "Time left: " .. i .. " seconds" -- Update countdown display
		task.wait(1)
	end
	button.Text = "Join Party"
	swapfunc = false
	countingdown = false
end

local function checkifcountingdown()
	while countingdown == false do
		if countingdown == false then
			startCountdown()
		end
		task.wait(0.1)
	end
end

local TableUpdateEvent = repstorage:WaitForChild("TableUpdateEvent")

local function updateTextLabel(tbl)
	while task(0.1) do
		local str = table.concat(tbl, "\n")
		local List = script.Parent.PlayerList.List
		List.Text = str
	end
end

startCountdown()
checkifcountingdown()
TableUpdateEvent.OnClientEvent:Connect(updateTextLabel) 

--ServerScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TableUpdateEvent = ReplicatedStorage:WaitForChild("TableUpdateEvent")
local joinev = ReplicatedStorage.JoinedGame
local leftev = ReplicatedStorage.LeftGame
local PartyPlayers = {}

-- Function to send table data to clients
local function updateTableData()
	TableUpdateEvent:FireAllClients(PartyPlayers)
end

joinev.OnServerEvent:Connect(function(plr)
	print(plr.Name .. " has joined the game!")
	table.insert(PartyPlayers, plr.Name)
	updateTableData()
end)

leftev.OnServerEvent:Connect(function(plr)
	print(plr.Name .. " has left the game!")
	local indexToRemove
	for i, name in ipairs(PartyPlayers) do
		if name == plr.Name then
			indexToRemove = i
			break
		end
	end
	if indexToRemove then
		table.remove(PartyPlayers, indexToRemove)
	end
	updateTableData()
end)

Haven’t looked over the code but here’s something you can try

local PartyPlayers = {
usernameHere;
usernameHere;
usernameHere;
}

local GUI = -- path to the GUI

for index = 1, #users do
	local text = GUI.Text 
    GUI.Text = text ..users[index]
end

Basically the bottom part of the script will go through each user in the table and add their name to the GUI.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.