How to get the second player in the server

I’m making a 2-player puzzle game where the players are assigned different roles, and i need to get the other player in the server that isn’t quietPlayer.

local players = game:GetService("Players")

local requiredAmount = 2
local playersInGame = players:GetPlayers()

local gameStarted = false

players.PlayerRemoving:Connect(function(plr)
	local remaining = players:FindFirstChildOfClass("Player")
	if remaining then
		remaining:Kick("Second player (@"..plr.Name.. ") has left the game, thus making continuation impossible. That was rude of them.")
	end

	playersInGame = players:GetPlayers()
	if #playersInGame < requiredAmount then
		gameStarted = false
	end
end)

players.PlayerAdded:Connect(function()
	playersInGame = players:GetPlayers()
end)

local timeout = 1

while wait(timeout) do
	if not gameStarted and #playersInGame == requiredAmount then
		gameStarted = true
		local quietPlayer = playersInGame[math.random(1, #playersInGame)]
		local quietCharacter =  quietPlayer.Character or quietPlayer.CharacterAdded:Wait()
		print(quietPlayer.Name.." has been chosen to inhabit the Quiet realm.")
		for i, des in pairs(quietCharacter:GetDescendants()) do
			if des.Name ~= "NewHead" and des.Name ~= "HumanoidRootPart" and des:IsA("BasePart") and not des.Parent:IsA("Tool") then
				des.Transparency = 0.8
			end
		end
		local quietHead = quietCharacter:FindFirstChild("Head")
		if quietHead and quietHead:FindFirstChild("face") then
			quietHead.face.Transparency = 0.8
		end
		local physiPlayer -- this should be the other player
		local physiCharacter -- this should be the other player's character
	end
end
2 Likes

just make checks when a player joins
(example that should give an idea)

if quietPlayer then
physiPlayer = Player
else
quietPlayer = Player
end

or when you assign a player to quietPlayer, assign the other player to physiPlayer

local quietPlayer = playersInGame[math.random(1, #playersInGame)]
local phsyiPlayer = otherplayer
2 Likes

I tried your first example like this,

local players = game:GetService("Players")

local requiredAmount = 2
local playersInGame = players:GetPlayers()

local gameStarted = false

local quietPlayer
local physiPlayer

players.PlayerRemoving:Connect(function(plr)
	local remaining = players:FindFirstChildOfClass("Player")
	if remaining then
		remaining:Kick("Second player (@"..plr.Name.. ") has left the game, thus making continuation impossible. That was rude of them.")
	end

	playersInGame = players:GetPlayers()
	if #playersInGame < requiredAmount then
		gameStarted = false
	end
end)

players.PlayerAdded:Connect(function(plr)
	playersInGame = players:GetPlayers()
	
	if plr == quietPlayer then
		physiPlayer = plr
	else
		quietPlayer = plr
	end
end)

local timeout = 1

while wait(timeout) do
	if not gameStarted and #playersInGame == requiredAmount then
		gameStarted = true
		local quietCharacter =  quietPlayer.Character or quietPlayer.CharacterAdded:Wait()
		print(quietPlayer.Name.." has been chosen to inhabit the Quiet realm.")
		for i, des in pairs(quietCharacter:GetDescendants()) do
			if des.Name ~= "NewHead" and des.Name ~= "HumanoidRootPart" and des:IsA("BasePart") and not des.Parent:IsA("Tool") then
				des.Transparency = 0.8
			end
		end
		local quietHead = quietCharacter:FindFirstChild("Head")
		if quietHead and quietHead:FindFirstChild("face") then
			quietHead.face.Transparency = 0.8
		end
		local physiPlayer = physiPlayer.Character or physiPlayer.CharacterAdded:Wait()
		print(physiPlayer.Name.." has been chosen to inhabit the Physical realm.")
	end
end

but it says “Attempt to index nil with Character”. How can i wait until physiPlayer and quietPlayer are not nil?

1 Like

sorry, don’t use that example it was just to give a idea, try looping through the players and assigning the roles in your while do.

local players = game:GetService("Players")

local requiredAmount = 2
local playersInGame = players:GetPlayers()

local gameStarted = false

local quietPlayer
local physiPlayer

players.PlayerRemoving:Connect(function(plr)
	local remaining = players:FindFirstChildOfClass("Player")
	if remaining then
		remaining:Kick("Second player (@"..plr.Name.. ") has left the game, thus making continuation impossible. That was rude of them.")
	end

	playersInGame = players:GetPlayers()
	if #playersInGame < requiredAmount then
		gameStarted = false
	end
end)

players.PlayerAdded:Connect(function()
	playersInGame = players:GetPlayers()
end)

local timeout = 1

while wait(timeout) do
	if not gameStarted and #playersInGame == requiredAmount then
		gameStarted = true
	    quietPlayer = playersInGame[math.random(1, #playersInGame)]
		local quietCharacter =  quietPlayer.Character or quietPlayer.CharacterAdded:Wait()
		print(quietPlayer.Name.." has been chosen to inhabit the Quiet realm.")
		for i, des in pairs(quietCharacter:GetDescendants()) do
			if des.Name ~= "NewHead" and des.Name ~= "HumanoidRootPart" and des:IsA("BasePart") and not des.Parent:IsA("Tool") then
				des.Transparency = 0.8
			end
		end
		local quietHead = quietCharacter:FindFirstChild("Head")
		if quietHead and quietHead:FindFirstChild("face") then
			quietHead.face.Transparency = 0.8
		end
		if not physiPlayer then
			for i,v in pairs(game.Players:GetChildren()) do
				if v ~= quietPlayer then
					physiPlayer = v
                    print(physiPlayer.Name.." has been chosen to inhabit the Physical realm.")
				end
			end
		end
	end
end

you could also just pick the other player by implementing this code from another post that picks the other player by removing the first player off a table

2 Likes

You could use Teams.
Teams | Documentation - Roblox Creator Hub

3 Likes

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