Is there a way to find which player is Player 1 and which is Player 2?

Hey so I’ve been trying to make a game like Mortal Kombat or Street Fighter where it addresses which player is Player 1 and which is Player 2 and puts them on either sides of the screen.

So far I’ve been able to make a script that will clone the player when there is only one in the server, but it breaks when two players join. I’m guessing it is because the script doesn’t know which player to clone, but I just need help assigning a player to Player 1 and Player 2

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char.Archivable = true;
		local clonedChar = char:Clone()
		clonedChar.Name = "Player1"
		clonedChar.DisplayName = plr.Name
		clonedChar.Parent = game:GetService("Workspace");
		clonedChar.HumanoidRootPart.CanCollide = false
		clonedChar.HumanoidRootPart.CFrame = workspace.PlayerOneStand.CFrame
	end)
end)
3 Likes

If your only allowing 2 players in a server you can just create a variable to check wether player one has been taken. Quickly made up this code let me know if there are any bugs. If you want to allow more people you could let the player decide which player they want to be or use a dictionary of some sort. Goodluck!

local IsPlayerOneTaken = false
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char.Archivable = true;
		local clonedChar = char:Clone()
		clonedChar.DisplayName = plr.Name
		clonedChar.Parent = game:GetService("Workspace");
		clonedChar.HumanoidRootPart.CanCollide = false
		if not IsPlayerOneTaken then 
			clonedChar.Name = "Player1"
			clonedChar.HumanoidRootPart.CFrame = workspace.PlayerOneStand.CFrame
		else 
			clonedChar.Name = "Player2"
			clonedChar.HumanoidRootPart.CFrame = workspace.PlayerTwoStand.CFrame --guessing your second stand is called this
		end
	end)
end)
2 Likes

So I just tried this, the script has a bug where it now has an error here;

local clonedChar = char:Clone()
		clonedChar.DisplayName = plr.Name
		clonedChar.Parent = game:GetService("Workspace");

where it doesn’t know which player to clone.

For clarification, this script is in ServerScriptService, lemme know if it should be placed somewhere else.

What is exactly the error that the output gives? I’m not sure what you mean by

where it doesn’t know which player to clone.

The variable “char” should be assigned when the character gets added to the game so there should be only 1 character to clone, right? The script should indeed be in ServerScriptService, don’t worry about that!

I’m pretty sure that your script is correct, the error may occur because the players join at the same time?

Just make each player have a different Team. When they join put them in a nuetral team, then make a script assigning them to their individual team so it doesn’t crash or wut so ever.

1 Like

I’ve tried using this script, but nothing clones

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char.Archivable = true;
		if plr.TeamColor == game.Teams.PlayerOne.TeamColor then
			local clonedChar = char:Clone()
			clonedChar.DisplayName =  "Player1"
			clonedChar.Parent = game:GetService("Workspace");
			clonedChar.HumanoidRootPart.CanCollide = false
			clonedChar.Name = "Player1"
			clonedChar.HumanoidRootPart.CFrame = workspace.PlayerOneStand.CFrame
		end
		
		if plr.TeamColor == game.Teams.PlayerTwo.TeamColor then
			local clonedChar = char:Clone()
			clonedChar.DisplayName = "Player2"
			clonedChar.Parent = game:GetService("Workspace");
			clonedChar.HumanoidRootPart.CanCollide = false
			clonedChar.Name = "Player2"
			clonedChar.HumanoidRootPart.CFrame = workspace.PlayerTwoStand.CFrame
		end
		
	end)
end)

dont do

plr.TeamColor

do

plr.Team

Now nothing cloned, I don’t understand why it’s not working.

1 Like

Sorry for late reply. You’re going to need to apply their teams when they join. then have a separate, or if you could combine it within the same script, clone them afterwards.

1 Like
local available = {"Player1", "Player2"}

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
        task.wait()
		char.Archivable = true
		if table.find(available, "Player1") then
            available["Player1"] = nil
			local clonedChar = char:Clone()
			clonedChar.Humanoid.DisplayName =  "Player1"
			clonedChar.Name = "Player1"
			clonedChar.HumanoidRootPart.CFrame = workspace.PlayerOneStand.CFrame
			clonedChar.Parent = workspace
		elseif table.find(available, "Player2") then
            available["Player2"] = nil
			local clonedChar = char:Clone()
			clonedChar.Humanoid.DisplayName = "Player2"
			clonedChar.Name = "Player2"
			clonedChar.HumanoidRootPart.CFrame = workspace.PlayerTwoStand.CFrame
			clonedChar.Parent = workspace
        else
           char.Archivable = false
           --code for if both players are taken
		end
	end)
end)
2 Likes

Thank you for all your help, your script does in fact work by cloning them, but it would name them the same thing.

So I just changed a few things in your script so now everything works! Thanks again!

local player1 = false

local player2 = false

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		task.wait()
		char.Archivable = true
		if player1 == false then
			player1 = true
			local clonedChar = char:Clone()
			clonedChar.Humanoid.DisplayName =  "Player 1"
			clonedChar.Name = "Player 1"
			clonedChar.HumanoidRootPart.CFrame = workspace.PlayerOneStand.CFrame
			clonedChar.Parent = workspace
		elseif player2 == false and player1 == true then
			player2 = true
			local clonedChar = char:Clone()
			clonedChar.Humanoid.DisplayName = "Player 2"
			clonedChar.Name = "Player 2"
			clonedChar.HumanoidRootPart.CFrame = workspace.PlayerTwoStand.CFrame
			clonedChar.Parent = workspace
		else
			char.Archivable = false
			--code for if both players are taken
		end
	end)
end)

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