Add multiple players to a table

I’ve made a code that enters a player that touches a part to a table. I’m very new to this sort of stuff and experimented using what I read about tables. I’m wondering if I’m doing this right, or if there’s a better way to do this.

Local script:

local rStorage = game:GetService("ReplicatedStorage") 
local plrJoinEvent = rStorage:WaitForChild("playerJoinEvent")

local plr = game:GetService("Players").LocalPlayer

local joinPart = game.Workspace.joinPart
local inQueue = false

while true do
	wait()
	
	joinPart.Touched:Connect(function(hit) -- Call function when player touches part
		if not inQueue and hit.Parent == plr.Character then -- If not already in a queue and if the player character touched the part
			plrJoinEvent:FireServer() -- Fire remote event
			inQueue = true -- Player is in queue
		end
	end)

end

Server script:

local rStorage = game:GetService("ReplicatedStorage")
local plrJoinEvent = rStorage:WaitForChild("playerJoinEvent")

local queue = {}

plrJoinEvent.OnServerEvent:Connect(function(player) -- When remote event is fired, add player who fired it to queue
	
	if #queue < 4 then -- If there is still room in the queue, add player
		table.insert(queue, player)
		print(#queue)
	end
	
	if #queue == 4 then -- if queue is full, teleport players and empty queue
		for place,plr in pairs(queue) do
			
			if place and plr then
				print("teleport "..plr.Name)
			end
			
		end
		
		queue = {} -- Empty queue
	end
end)

I appreciate all answers, thanks in advance.

1 Like

Why do you want to use a local script and a server script? You can just handle the touched event in the server script without needing a client script. Other than that, its pretty good.

1 Like

Continuing off with @lightningstrike30451 's post, yes you can integrate this entire system into the server for no client exploitation, also here’s a tip for a table player debounce

local joinPart = game.Workspace.joinPart
local queue = {}

joinPart.Touched:Connect(function(hit)
    local model = hit:FindFirstAncestorOfClass("Model")
    local player = game:GetService("Players"):GetPlayerFromCharacter(model)
	if player and not queue[player] and #queue < 4 then 
	   table.insert(queue, player)
       if #queue == 4 then -- if queue is full, teleport players and empty queue
		  for place,plr in pairs(queue) do			
			  if place and plr then
				print("teleport "..plr.Name)
			  end			
		  end
		
		  table.clear(queue) -- Empty queue
	   end              
	end
end)
1 Like

Hey, thanks for the reply. I didn’t think about handling it all in the server script, thanks for the tip

Hello, thanks for the help, I’ll make sure to test it out

1 Like