How do i make this code execute on the serverside?

dont it has a mistake Im gonna check again

2 Likes
game.Players.PlayerAdded:Connect(function(plr)
	
		local queue = Instance.new("NumberValue")
		queue.Parent = plr
		local guiFrame = plr:WaitForChild("PlayerGui"):WaitForChild("QueueGui").QueueScreenFrame
		queueEvent.OnServerEvent:Connect(function()
		while task.wait(1) do
				local player = guiFrame.Parent.Parent.Parent
				queue.Value = queue.Value + 10
				guiFrame.QueueLabel.Text = queue.Value.. "/20 in queue"
				guiFrame.TutorialButton.Visible = false
				guiFrame.ShopButton.Visible = false
				guiFrame.QueueButton.Visible = false
				if queue.Value == 20 then
					task.wait(1)
					teleportService:Teleport(14453697332, player)
					queue.Value = 0
					guiFrame.QueueLabel.Text = queue.Value.. "/20 in queue"
					script.CountdownValue.Value = 30
					guiFrame.Countdown.Text = script.CountdownValue.Value
					guiFrame.Countdown.Visible = false
				end
			end
		end)
end)

This should work

try the one mainfizz7 sent u first. I edited mine it should work. just test mine after u test his

tried it, the script completely broke; nothing worked

If they’re supposed to wait for 20 seconds and you’re doing +10 for testing purposes here is how it should look like when fixed because everyting was weird there

local Queue = {}
game.Players.PlayerAdded:Connect(function(plr)
	Queue[plr] = 0
end)

queueEvent.OnServerEvent:Connect(function(plr)
	local guiFrame = plr:WaitForChild("PlayerGui"):WaitForChild("QueueGui").QueueScreenFrame
	guiFrame.TutorialButton.Visible = false
	guiFrame.ShopButton.Visible = false
	guiFrame.QueueButton.Visible = false
	while task.wait(1) do
		Queue[plr] +=  10
		guiFrame.QueueLabel.Text = Queue[plr].. "/20 in queue"
		
		if Queue[plr] == 20 then
			task.wait(1)
			teleportService:Teleport(14453697332, plr)
			Queue[plr] = 0
			guiFrame.QueueLabel.Text = Queue[plr].. "/20 in queue"
		end
	end
end)

I just edited mine again. Idk if it will work tho I still havent tested it

1 Like

tried that, same thing happened as this

2 Likes

can you tell me what is queue in script.queue? I wanna know for testing purposes. is it a part or something?

edit: also script.countdownvalue

2 Likes

tried it, it just made the game get really weird; the countdown becomes serversided but the queue doesnt, the countdown skips every 2 numbers

2 Likes

script.queue is a numbervalue that mainfizz7 explained that i dont actually need, countdownvalue is a numbervalue that is the countdown to the players being teleported once 10 (or in this case, 1) players join the queue

Do you want it to be a queue in the way of it being like a support queue (e.g. You’re #5 in queue.) or do you want it to be a queue where everyone joins the same queue and when there are 20 players in it, it will teleport everyone after the countdown ends?

I can make a complete new script for you.

a little bit like the second one, i want the countdown to start and appear once the queue reaches 10 players, and if the countdown ends, they all get teleported, unless 10 other players join the queue, because then they would all be teleported immediately

but yeah, everyone joins the same queue

Ah I see. Thanks for the information. I will make a new script since your script is sadly unreadable for me. I will try to explain it well.

alright, sorry for the bad script lol, yeah, im a beginner so i cant make good scripts yet lol

Don’t worry about it. I hope my script will be well explained. I might not finish it today though since I might have other plans.

Im done

local teleportService = game:GetService("TeleportService")
local play = {
	slot1 = "",
	slot2 = ""
}

game.Players.PlayerAdded:Connect(function(plr)
	print(plr)
	if true then
		local guiFrame = plr:WaitForChild("PlayerGui"):WaitForChild("QueueGui").QueueScreenFrame
		guiFrame.QueueButton.MouseButton1Click:Connect(function()
			if true then
				local player = guiFrame.Parent.Parent.Parent
				script.Queue.Value = script.Queue.Value + 10
				guiFrame.QueueLabel.Text = script.Queue.Value.. "/20 in queue"
				guiFrame.TutorialButton.Visible = false
				guiFrame.ShopButton.Visible = false
				guiFrame.QueueButton.Visible = false
				if script.Queue.Value == 20 then
					print(plr)
					play["slot2"] = plr
					task.wait(1)
					script.Queue.Value = 10
					guiFrame.QueueLabel.Text = script.Queue.Value.. "/20 in queue"
					script.CountdownValue.Value = 0
					guiFrame.Countdown.Text = script.CountdownValue.Value
					guiFrame.Countdown.Visible = false
				end
				if script.Queue.Value == 10 then
					if play["slot1"] == "" then
						play["slot1"] = plr
					end
					guiFrame.Countdown.Visible = true
					guiFrame.Countdown.Text = script.CountdownValue.Value
					for i = 1, 30 do
						if script.CountdownValue.Value == 0 then
							break
						end
						script.CountdownValue.Value = script.CountdownValue.Value - 1
						guiFrame.Countdown.Text = script.CountdownValue.Value
						task.wait(1)
					end
					if script.CountdownValue.Value == 0 then
						script.Queue.Value = 0
						guiFrame.QueueLabel.Text = script.Queue.Value.. "/20 in queue"
						script.CountdownValue.Value = 30
						guiFrame.Countdown.Text = script.CountdownValue.Value
						guiFrame.Countdown.Visible = false
						print(play)
						task.wait(0.5)
						for i, v in pairs(play) do
							if v ~= "" then
								teleportService:Teleport(14453697332, v)
							end
						end
					end
				end
			end
		end)
	end
end)

tell me if it doesnt work

The majority of this script does not need to be executed on the server, as most of it deals with a gui that exist on the client. If you don’t completely understand the client-server model, you can read an article by roblox here.

From my understanding, you are wanting to add players to a queue so that when it fills up, or the timer finishes counting down, it will teleport the players in the queue to the given place. If this is what is intended, you should really split this up into a client script and server script like @Fimutsuu mentioned. Client input (pressing buttons and stuff) can be handled in a client script while the queue logic should be handled on the server. Data can be sent to the server from the client using remote events.

Also, since the queue logic should be handled on the server, given that it is dealing with multiple players the teleportation between experiences should also be handled on the server. In this case, TeleportAsync can be used for transporting all the players in the queue.

You can put a local script in the GUI for the player like so:
Screenshot 2023-08-21 at 8.29.12 AM

For the server script, it would be best to put it in ServerScriptService like so:
Screenshot 2023-08-21 at 8.37.20 AM

Along with a local script in the client gui and a server script in ServerScriptService, you should put a remote event in ReplicatedStorage for enabling communication between the client and server like so:
Screenshot 2023-08-21 at 8.33.41 AM


Client:

-- Variables: --
local queueFrame = script.Parent:WaitForChild("QueueScreenFrame")
local queueRemote = game:GetService("ReplicatedStorage"):WaitForChild("QueueRemote")

local queueLabel = queueFrame:WaitForChild("QueueLabel")
local countdownLabel = queueFrame:WaitForChild("Countdown")

local queueButton = queueFrame:WaitForChild("QueueButton")
local tutorialButton = queueFrame:WaitForChild("TutorialButton")
local shopButton = queueFrame:WaitForChild("ShopButton")

local countdown = 0

-- Functions: --
local function setButtons(visible)
	queueButton.Visible = visible
	tutorialButton.Visible = visible
	shopButton.Visible = visible
	
	countdownLabel.Visible = (not visible)
end

local function updateCountDown(waitTime)
	countdown = waitTime

	for i=1, waitTime do
		if (countdown ~= waitTime) then -- will terminate prior-running threads
			break
		end
		task.wait(1)
		print(i)
	end
end

local function queueInfo(canJoin, amount, waitTime)
	if (canJoin ~= nil) then
		setButtons(not canJoin)
	end
	
	queueLabel.Text = tostring(amount) .. "/20 in queue" --updates the queue count whenever someone joins or leaves
	task.spawn(function() updateCountDown(waitTime) end)
end

-- Events: --
queueRemote.OnClientEvent:Connect(queueInfo)

queueButton.MouseButton1Click:Connect(function()
	-- removed the "if true then" since it served no purpose
	queueRemote:FireServer(true) -- passing true as an argument since we want to join
end)

Server:

-- Variables: --
local Players = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")

local queueRemote = game:GetService("ReplicatedStorage").QueueRemote -- we don't need to use WaitForChild on the server since everything is loaded before script execution

local queue = {}
local threshold = 2
local countdown = 30
local running = false

-- Functions: --
local updateCountDown = coroutine.create(function(run)
	running = run
	
	while (countdown > 0 and running) do
		countdown -= task.wait(1)
	end
	countdown = 30

	if running then
		TeleportService:TeleportAsync(14453697332, queue)
		running = false
		queue = {}
	end
end

-- Events: --
queueRemote.OnServerEvent:Connect(function(player, isJoining)
	local index = table.find(queue, player)
	local canJoin = isJoining and (index == nil) and (#queue < 20)

	if canJoin then
		table.insert(queue, player)
	elseif (index ~= nil) then
		table.remove(queue, index)
	end
	
	if (#queue >= threshold) and (not running) then
		task.spawn(function() updateCountDown(true) end)
	elseif (#queue < threshold) then
		task.spawn(function() updateCountDown(false) end)
	end
	
	for _, plr in Players:GetPlayers() do
		queueRemote:FireClient(plr, if (plr ~= player) then nil else canJoin, #queue, math.floor(countdown))
	end
end)



Note: You will need to check the logic of the scripts to make sure it does what you want. I based it off of the script you provided, but it may not execute the way you want. There are also many better ways to achieve this. This is just an example, so it isn’t necessarily good. It only contains code for a single queue, so if you want to add more you’ll have to do that yourself.

it does not work; still doesnt act serverside and doesnt teleport the players at all

are third party teleports enabled?

yes, they are (character limit)