Joining table on UI Click?

Hello people of the DevForum! I have a question.
I’ve been trying to create a relatively simple queue system, but I have come across a problem.

Here’s some code I decided I’d start off from:

local PlayersToTeleport = {}
local playersinqueue = (#PlayersToTeleport)

TouchBrick.Touched:Connect(function(TouchedPart)
	if TouchedPart.Parent:FindFirstChild("Humanoid") then
		local Character = TouchedPart.Parent
		local Player = game.Players:GetPlayerFromCharacter(Character)
		local AlreadyInTable = false
		-- Now make sure the player isn't already in the table
		for _,OtherPlayer in next,PlayersToTeleport do
			if OtherPlayer == Player then
				AlreadyInTable = true
			end
		end
		if not AlreadyInTable then
		-- Add them to the table
		table.insert(PlayersToTeleport,Player)
		end
	end
end)

Its probably a really simple thing, but how would I go about making this but on the MouseButton1Click of a image/text button?
Thanks in advance to anyone that replies. :slight_smile:

3 Likes

You could use a remote event to fire the server to put the player in the queue.

Yeah but what I’m stumped on is how to modify this script to make that work.

1 Like
script.Parent.MouseButton1Click:Connect(function()
   game.ReplicatedStorage.RemoteEvent:FireServer()
end)

[quote="stqrrydev, post:1, topic:903947"]

local PlayersToTeleport = {}
local playersinqueue = (#PlayersToTeleport)

[/quote]

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr)
      

[quote="stqrrydev, post:1, topic:903947"]
 local AlreadyInTable = false
	-- Now make sure the player isn't already in the table
	for _,OtherPlayer in next,PlayersToTeleport do
		if OtherPlayer == plr then
			AlreadyInTable = true
		end
	end
	if not AlreadyInTable then
	-- Add them to the table
	table.insert(PlayersToTeleport,plr)
[/quote]

end)
1 Like

Oh sorry I did not quote your code quite right but try something like that :slight_smile:

Alright! ill let you know how it goes.

Ok it seems good, but I added

if playersinqueue ==  1 then --because if I was added to the queue there should be 1 player in there right?
    print ("lol")-- lol.
end

No errors. No output.

local script:
script.Parent.JoinFrame.Join.MouseButton1Click:Connect(function()

game.ReplicatedStorage.QueueRequest:FireServer()

end)

script in serverscriptservice:

local PlayersToTeleport = {}
local playersinqueue = (#PlayersToTeleport)

game.ReplicatedStorage.QueueRequest.OnServerEvent:Connect(function(plr)
local AlreadyInTable = false
for _,OtherPlayer in next,PlayersToTeleport do
if OtherPlayer == plr then
AlreadyInTable = true
end
end
if not AlreadyInTable then
table.insert(PlayersToTeleport,plr)
end
end)

if playersinqueue == 1 then
print(“lol”)
end

1 Like

if #PlayersToTeleport == 1 then (try that instead)

There is a function that allow you to see if the player is in the table faster.

table.find(TableName,plr)


Use this: 
if not table.find(TableName,plr) then
-- your script here
else
  print("Player Is Already In The Table")
end)

Thanks for replying but is the script I did above with Vorexted’s help gonna work?

also ill try the function you said.

Change

To:

if #PlayersToTeleport == 1 then

end

It’s not printing anything still :confused:

You should listen to Vorexted, what he/she say should work.

If you guys are stumped too ill just pay someone to do it because

it didn’t.
no offense Vorexted you helped a lot!!

Jumping in studio rn, I did something like this before but forgot. I am going to put your script in studio and see what wrong.

1 Like

OH WAIT. Put

Inside of the remote function block.

1 Like

Do it like this if you dont understand what a block is :slight_smile:

Thank you so much Vorexted!! That worked! can’t believe I didn’t think of that :man_facepalming:

1 Like

Nice! I guess I don’t have solve it then. Have a great rest of the day! :slight_smile:

2 Likes