Two player switching system

Alright here it is. Simple. Just kidding. I wish. Two players are inside Workspace. One of the players you have control over, the other is stationary or frozen. You can switch from your current player or rig to another rig. You press Q to move from player 1 to player 2. You press E to move from player 2 to player 1. The players both must be in Workspace at all times. I have worked on this all day couldn’t get anything to work. Any ideas or code would be much appreciated



if Keycode == Enum.KeyCode.Q then 


elseif Keycode = Enum.KeyCode.E then 

end

Just change the camera subject between the humanoids and the players character

1 Like

Just… change the character itself. And change the ‘move set’ if you’re going for a game like ‘super blox 64!’.

1 Like

That is actually a great idea. I would just need to figure out how to disable the movement for the character. Thank you!

I don’t think it possible to ‘control’ 2 character. It’s better to completely alter the character itself instead of altering the controlled character. (It’s not impossible but it would require a lot of complex scripting as to create another character with control respected to your original character)

Right. I get what you are saying but i was able to change character but my original one kept disappearing. I am posting this to save myself some time otherwise i might spend a few weeks trying to develop this system

HUH!? WHAT!? ok? Try enboxing the character. (placing a box and not letting the character fall off)

Alright so I came up with some code, assuming that what you want is a single player game where you can swap characters pressing keys.

Client (StarterPlayerScripts):

local UIS = game:GetService("UserInputService")
local Event = game.ReplicatedStorage.SwitchEvent
local Character1Name = "Character1"
local Character2Name = "Character2"

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	local Keycode = input.KeyCode
	if Keycode == Enum.KeyCode.Q then
		Event:FireServer(Character1Name)
	elseif Keycode == Enum.KeyCode.E then 
		Event:FireServer(Character2Name)
	end
end)

Event.OnClientEvent:Connect(function() -- listen to event for camera change
	workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid -- set camera
end)

Server (ServerScriptService):

local Event = game.ReplicatedStorage.SwitchEvent
local DefaultCharacter = workspace:WaitForChild("Character1")

game.Players.CharacterAutoLoads = false -- disable player's avatar loading in

local function PlayerAdded(Player)
	Player.Character = DefaultCharacter -- load the player in with the default character
end

local function EventFired(Player, CharacterName)
	local Character = workspace:FindFirstChild(CharacterName)
	
	if not Character then return end -- check if character exists
	if Player.Character.Name == CharacterName then return end -- make sure player isnt switching to their current character
	
	local OldCharacter = Player.Character:Clone() -- clone old character
	OldCharacter.Parent = Player.Character.Parent -- set clone parent
	Player.Character = Character -- set player character
	
	Event:FireClient(Player) -- Return event so that the player can set camera
end

Event.OnServerEvent:Connect(EventFired)
game.Players.PlayerAdded:Connect(PlayerAdded)

Note that if you want the animations to work you’ll probably have to put a stored animate script and swap between a client and server animate script when you switch characters. Hopefully this is what you were looking for.

Edit: forgot to mention that technically the character is being deleted but its being cloned right before that happens so im not sure how big of a problem that would be.

1 Like

Thank you! I was going in a different direction with my script. Just tested it out, worked exactly how I imagined. I really do appreciate it. Thank you, again.

1 Like