Player ready button makes everyone ready instead of single player / Players being teleported to different places

Hello, this post has 2 problems:
When me or my friend plays ready, it sets both of us ready instead of just me or my friend and when we get teleported it makes two separate servers instead of a single one where we are both in. Here’s the script.

local ReadyEvent = game.ReplicatedStorage.Ready
local NotReadyEvent = game.ReplicatedStorage.Unready

local TS = game:GetService("TeleportService")

game.Players.PlayerAdded:Connect(function(player)
	local Char = player.Character or player.CharacterAdded:Wait()
	local ReadyV = Char:WaitForChild("Ready")
	local Head = Char:WaitForChild("Head")
	
	ReadyEvent.OnServerEvent:Connect(function()
		local ReadyGuiClone = game.ReplicatedStorage.ReadyGui:Clone()
		
		ReadyGuiClone.Parent = Head
		if ReadyV.Parent == Char then
			ReadyV.Value = true
		end
	end)
	
	NotReadyEvent.OnServerEvent:Connect(function()
		Head:WaitForChild("ReadyGui"):Destroy()
		ReadyV.Value = false
	end)
	
	for _, Players in pairs(game.Players:GetPlayers()) do
		local Characters = Players.Character
		local possiblePlayer = game.Players:GetPlayers()

		Characters.Ready.Changed:Connect(function()
			if Characters.Ready.Value == true then
				print("Teleporting to place...")
				local server = TS:ReserveServer(10164359672)
				TS:TeleportToPrivateServer(10164359672, server, {Players})
			end
		end)
	end
end)
1 Like

Solution should be below; however, to explain some things.

The main solution, I changed

TS:TeleportToPrivateServer(10164359672, server, {Players}) -- 'Players' was the LocalPlayer instead of a table of players
-- to;
TS:TeleportToPrivateServer(10164359672, server, {ReadyPlayers}) -- 'ReadyPlayers' is a table of Players
local Players = game:GetService("Players")
local TS = game:GetService("TeleportService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ReadyEvent = ReplicatedStorage.Ready
local NotReadyEvent = ReplicatedStorage.Unready

local debounce = false
function ReadyOrNot()
	if not debounce then -- add a debounce to prevent remote spam
		debounce = true
		local ReadyPlayers = {}
		local NumberOfPlayers = table.getn(Players:GetPlayers()) -- table.getn simply gets a number.
		for _, Player in pairs(Players:GetPlayers()) do
			if Player:GetAttribute("Ready") and Player:GetAttribute("Ready") == true then
				table.insert(ReadyPlayers,Player)
			end
		end


		if table.getn(ReadyPlayers) == NumberOfPlayers then -- Check to ensure both table counts match. 
			local server = TS:ReserveServer(10164359672)
			TS:TeleportToPrivateServer(10164359672, server, {ReadyPlayers}) 
			-- Yours previously was NOT working becuase you had 'Players' as the variable set to the for _,Players which is the specific object of a player, not the group of players.

		else
			warn("Not all players are ready!")
		end		
		debounce = false	
	end
end

ReadyEvent.OnServerEvent:Connect(function(Player)
	Player:SetAttribute("Ready",true)

	local Character = Player.Character
	local Head = Character.Head
	local ReadyGuiClone = game.ReplicatedStorage.ReadyGui:Clone()

	ReadyGuiClone.Parent = Head
	
	ReadyOrNot()
end)

NotReadyEvent.OnServerEvent:Connect(function(Player)
	Player:SetAttribute("Ready",false)	

	local Character = Player.Character
	local Head = Character.Head
	
	if Head:FindFirstChild("ReadyGui") then
		Head.ReadyGui:Destroy()	
	end
end)

Players.PlayerAdded:Connect(function(Player)
	Player:SetAttribute("Ready",false) -- Sets it to the player rather than character (more efficient)
end)

Please let me know if you have issues.

Thanks for the answer but i already found an answer, i appreciate your answer tho. This might help the others tho

1 Like