Why are my RemoteEvent only executed by the last player to enter?

Currently, I am making a list of players who want to play in the next game or not (Queues), in this system the user to press a physical object runs a RemoteEvent that adds it to a list of the queue, after running this RemoteEvent it runs another one that notifies all players in the game and updates the list in the GUI of all players, my error is that for some reason, the update of the list and the addition of the list only is executing it the last player that enters to the game, when the list executes FireAllClients only this one is receiving it and not the rest, here I leave a part of my code so that my problem is understood.

JoinScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local JoinQueueEvent = ReplicatedStorage:WaitForChild("JoinQueueEvent")

local clickDetector = Instance.new("ClickDetector") 
clickDetector.MaxActivationDistance = 100
clickDetector.Parent = script.Parent

clickDetector.MouseClick:Connect(function()
	print("clicked")
	JoinQueueEvent:FireServer()
end)

CreateQueue:

local ReplicatedStorage = game:GetService("ReplicatedStorage");
local Player = game:GetService("Players")

local JoinQueueEvent = ReplicatedStorage:WaitForChild("JoinQueueEvent");
local UpdateQueueEvent = ReplicatedStorage:WaitForChild("UpdateQueueEvent")

local queue = {}
local minPlayers = 3 
local maxPlayers = 32 
local countdownTime = 10

local function updateQueue()
	UpdateQueueEvent:FireAllClients(queue)
end

local function addUser(player)
	for _, queuedPlayer in ipairs(queue) do
		if queuedPlayer == player then
			print("Ya esta en la cola")
			return
		end
	end
	if #queue >= maxPlayers then
		return
	end

	table.insert(queue, player)

	updateQueue()
end

JoinQueueEvent.OnServerEvent:Connect(function(player)
	print("new user :3")
	addUser(player)
end)

GUIScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UpdateQueueEvent = ReplicatedStorage:WaitForChild("UpdateQueueEvent")

local screenGui = script.Parent

local function UpdateGUI(players)
	print(players)
	for _, child in ipairs(screenGui:GetChildren()) do
		child:Destroy()
	end

	for i, usuario in ipairs(players) do
		local imagenPerfil = Instance.new("ImageLabel")
		imagenPerfil.Size = UDim2.new(0, 100, 0, 100)
		imagenPerfil.Position = UDim2.new(0, i * 110, 0, 0)
		imagenPerfil.Image = "rbxthumb://type=Avatar&id=" .. usuario.UserId .. "&w=150&h=150"
		imagenPerfil.Parent = screenGui
	end
end

UpdateQueueEvent.OnClientEvent:Connect(UpdateGUI)

Creo que pasa porque tienes que limpiar la tabla cuando ya se ha añadido al usuario

Since the script is contained directly in the ScreenGui, the loop will end up destroying the script, preventing the code in the rest of the function from running.

Consider adding an additional conditional statement to make sure you don’t delete the LocalScript upon updating the GUI:

Example Revision

local function UpdateGUI(players)
	print(players)

	for _, child in ipairs(screenGui:GetChildren()) do
        local scriptCheck = child:IsA("Script")
        if not scriptCheck then
		    child:Destroy()
        end
	end

    -- Continue
1 Like

Thanks, I didn’t know what the error was since yesterday, you helped me a lot!

1 Like

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