Character Swapping System, how can I minimize this?


#1

I’m not sure what to say the title says it all. Code shouldn’t be to hard to understand. I’m getting all the characters (NPC’s) in a model. Setting the character to NPC. Then updating on key press. I feel like it’s too bulky. If there is any confusion please let me know and I will edit the post.

local NPCCharacters = {}
local CharacterValue = 1

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
	for i,v in pairs(workspace.NPCCharacters:GetChildren()) do
		table.insert(NPCCharacters, v.Name)
	end
	wait(.01)
	player.Character = workspace.NPCCharacters:WaitForChild(NPCCharacters[CharacterValue])
	workspace.CurrentCamera.CameraSubject = workspace.NPCCharacters:WaitForChild(NPCCharacters[CharacterValue])
	
	local function onKeyPress(inputObject, gameProcessedEvent)
		if inputObject.KeyCode == Enum.KeyCode.Q then
		    if CharacterValue == #NPCCharacters then
			CharacterValue = 1
		    else
			CharacterValue = CharacterValue + 1
		    end

		    local cloned = player.Character:Clone()
		    cloned.Parent = workspace.NPCCharacters
		    player.Character = workspace.NPCCharacters:WaitForChild(NPCCharacters[CharacterValue])
		    workspace.CurrentCamera.CameraSubject = workspace.NPCCharacters:WaitForChild(NPCCharacters[CharacterValue])

		elseif inputObject.KeyCode == Enum.KeyCode.E then
		    if CharacterValue == 1 then
			CharacterValue = #NPCCharacters
		    else
			CharacterValue = CharacterValue - 1
		    end

		    local cloned = player.Character:Clone()
		    cloned.Parent = workspace.NPCCharacters
		    player.Character = workspace.NPCCharacters:WaitForChild(NPCCharacters[CharacterValue])
		    workspace.CurrentCamera.CameraSubject = workspace.NPCCharacters:WaitForChild(NPCCharacters[CharacterValue])

		end
	end
	game:GetService("UserInputService").InputBegan:connect(onKeyPress)
	
    end)
end)

#2

Why does it to do this for all players if it’s only relevant to the single client it’s running on? To fix this, just remove the PlayerAdded listener and set player to game.Players.LocalPlayer.

As for your code, it seems fine. However, you should also make a debounce (and maybe a cooldown too) to stop the player from spamming keys to cause errors and lag.

Also, you should be checking if the player’s character is dead before swapping characters (otherwise they could just instantly revive themselves). Depending on your game you may also want to be putting the new character back at the position of the old character, and deleting the old character too.

Finally, you also have some duplicate code (the part that actually swaps the characters based on CharacterValue). You can turn this into a function you call.

local function ChangeCharacter()
    local cloned = player.Character:Clone()
    local newCharacter = workspace.NPCCharacters:WaitForChild(NPCCharacters[CharacterValue])
    cloned.Parent = workspace.NPCCharacters
    player.Character = newCharacter
	workspace.CurrentCamera.CameraSubject = newCharacter
end

-- whenever you've update CharacterValue, call ChangeCharacter afterwards
ChangeCharacter()