Clone players from the server into npcs that spawn

I want to make my script get all the players from in the server and clone them into npcs and sit them in random seats in the bus for a cutscene

Current ServerScript

local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

local bus = Workspace:FindFirstChild("BusB")
local seatsFolder = bus and bus:FindFirstChild("Seats")

if bus and seatsFolder then
    local seats = seatsFolder:GetChildren()
    local seatIndex = 1

    local function seatPlayerAsNPC(player)
        local character = player.Character or player.CharacterAdded:Wait()
        if character then
            local characterClone = character:Clone()
            characterClone.Name = player.Name .. "_NPC"

            -- Remove player-specific components
            for _, child in characterClone:GetChildren() do
                if child:IsA("Script") or child:IsA("LocalScript") then
                    child:Destroy()
                end
            end

            -- Position the NPC at a seat
            if seatIndex <= #seats then
                local seat = seats[seatIndex]
                characterClone:SetPrimaryPartCFrame(seat.CFrame)
                seatIndex = seatIndex + 1
            else
                print("No more seats available for", player.Name)
                return
            end

            characterClone.Parent = Workspace
            print("Cloned player:", player.Name)
        else
            print("Character not found for player:", player.Name)
        end
    end

    -- Connect to PlayerAdded event
    Players.PlayerAdded:Connect(function(player)
        player.CharacterAdded:Connect(function()
            seatPlayerAsNPC(player)
        end)
    end)

    -- Handle existing players
    for _, player in Players:GetPlayers() do
        if player.Character then
            seatPlayerAsNPC(player)
        else
            player.CharacterAdded:Wait()
            seatPlayerAsNPC(player)
        end
    end
else
    print("BusB or Seats folder not found in Workspace.")
end
1 Like

so does it work or not? if not, what is not working or how does it look like now?

it doesnt work at all and there is currently no output on the script, it doesnt clone the player into workspace in the position of the seat.

local function seatPlayerAsNPC(player)
     -- add a print here
    print("seatPlayerAsNPC", player, player.Character)

let’s check if it even goes into this function

1 Like

yup just checked it the print still pops up when I join the game I just dont know why the player isnt cloning at all into workspace.

there is an error here though

  21:16:43.597  ServerScriptService.PlayersToBusClone:16: attempt to index nil with 'Name'  -  Server - PlayersToBusClone:16
  21:16:43.597  Stack Begin  -  Studio
  21:16:43.598  Script 'ServerScriptService.PlayersToBusClone', Line 16 - function seatPlayerAsNPC  -  Studio - PlayersToBusClone:16
  21:16:43.598  Script 'ServerScriptService.PlayersToBusClone', Line 45  -  Studio - PlayersToBusClone:45

so because the player is there, the character is also there. then the clone() failed?

1 Like

i fixed the script but there is an issue im trying to fix now. Right now the body of the character spawns but their clothes and accessories dont spawn with them

local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

local bus = Workspace:FindFirstChild("BusB")
local seatsFolder = bus and bus:FindFirstChild("Seats")

if bus and seatsFolder then
    local seats = seatsFolder:GetChildren()
    local seatIndex = 1

    local function seatPlayerAsNPC(player)
        print("Attempting to seat player:", player.Name)
        
        -- Wait for the character to be fully loaded
        local character = player.Character or player.CharacterAdded:Wait()

        -- Ensure the character is valid
        if not character then
            print("Character is nil for player:", player.Name)
            return
        end

        -- Check if the Humanoid exists
        local humanoid = character:FindFirstChild("Humanoid")
        if not humanoid then
            print("Humanoid not found for player:", player.Name)
            return
        end

        -- Check if the HumanoidRootPart exists
        local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
        if not humanoidRootPart then
            print("HumanoidRootPart not found for player:", player.Name)
            return
        end

        -- Clone the character manually (to avoid potential issues with entire model cloning)
        local characterClone = Instance.new("Model")
        characterClone.Name = player.Name .. "_NPC"

        -- Function to clone a character part
        local function cloneCharacterPart(partName)
            local part = character:FindFirstChild(partName)
            if part then
                local clonedPart = part:Clone()
                clonedPart.Parent = characterClone
                print(partName .. " cloned successfully.")
            else
                print(partName .. " not found for player:", player.Name)
            end
        end

        -- Handle both R6 and R15 models
        if humanoid.RigType == Enum.HumanoidRigType.R6 then
            -- R6 model uses "Torso"
            cloneCharacterPart("Torso")
            cloneCharacterPart("LeftLeg")
            cloneCharacterPart("RightLeg")
            cloneCharacterPart("LeftArm")
            cloneCharacterPart("RightArm")
        elseif humanoid.RigType == Enum.HumanoidRigType.R15 then
            -- R15 model uses "UpperTorso" and "LowerTorso"
            cloneCharacterPart("UpperTorso")
            cloneCharacterPart("LowerTorso")
            cloneCharacterPart("LeftUpperArm")
            cloneCharacterPart("LeftLowerArm")
            cloneCharacterPart("RightUpperArm")
            cloneCharacterPart("RightLowerArm")
            cloneCharacterPart("LeftUpperLeg")
            cloneCharacterPart("LeftLowerLeg")
            cloneCharacterPart("RightUpperLeg")
            cloneCharacterPart("RightLowerLeg")
        end

        -- Clone essential parts: Head, HumanoidRootPart, and Humanoid
        cloneCharacterPart("Head")
        cloneCharacterPart("HumanoidRootPart")
        cloneCharacterPart("Humanoid")

        -- Clone clothing (Shirt, Pants)
        local shirt = character:FindFirstChild("Shirt")
        if shirt then
            local shirtClone = shirt:Clone()
            shirtClone.Parent = characterClone
            print("Cloned Shirt.")
        else
            print("Shirt not found for player:", player.Name)
        end

        local pants = character:FindFirstChild("Pants")
        if pants then
            local pantsClone = pants:Clone()
            pantsClone.Parent = characterClone
            print("Cloned Pants.")
        else
            print("Pants not found for player:", player.Name)
        end

        -- Clone accessories
        for _, accessory in pairs(character:GetChildren()) do
            if accessory:IsA("Accessory") then
                local clonedAccessory = accessory:Clone()
                clonedAccessory.Parent = characterClone
                print("Cloned accessory:", accessory.Name)
            end
        end

        -- Remove non-cloneable objects like PlayerScripts or Player components
        for _, child in character:GetChildren() do
            if child:IsA("Script") or child:IsA("LocalScript") or child:IsA("PlayerScripts") then
                child:Destroy()
            end
        end

        -- Ensure we have a valid PrimaryPart
        if not characterClone.PrimaryPart then
            -- Try to set the PrimaryPart to HumanoidRootPart if it's not set
            local primaryPart = characterClone:FindFirstChild("HumanoidRootPart")
            if primaryPart then
                characterClone.PrimaryPart = primaryPart
                print("PrimaryPart set to HumanoidRootPart.")
            else
                print("Failed to set PrimaryPart for cloned character.")
                return
            end
        end

        -- Position the NPC at a seat
        if seatIndex <= #seats then
            local seat = seats[seatIndex]
            characterClone:SetPrimaryPartCFrame(seat.CFrame)
            seatIndex = seatIndex + 1
        else
            print("No more seats available for", player.Name)
            return
        end

        -- Parent the cloned character to the workspace
        characterClone.Parent = Workspace
        print("Successfully cloned and seated player:", player.Name)
    end

    -- Connect to PlayerAdded event
    Players.PlayerAdded:Connect(function(player)
        player.CharacterAdded:Connect(function()
            seatPlayerAsNPC(player)
        end)
    end)

    -- Handle existing players
    for _, player in Players:GetPlayers() do
        if player.Character then
            seatPlayerAsNPC(player)
        else
            player.CharacterAdded:Wait()
            seatPlayerAsNPC(player)
        end
    end
else
    print("BusB or Seats folder not found in Workspace.")
end

1 Like

Maybe try adding a wait(1) before running the seatPlayerAsNPC()

i added the wait and now there is an error here

  21:44:42.284  Shirt is not a valid member of Humanoid "DemonicPandazYT_NPC.Humanoid"  -  Server - PlayersToBusClone:89

Current Script

local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

local bus = Workspace:FindFirstChild("BusB")
local seatsFolder = bus and bus:FindFirstChild("Seats")

if bus and seatsFolder then
	local seats = seatsFolder:GetChildren()
	local seatIndex = 1

	local function seatPlayerAsNPC(player)
		wait(1)
		print("Attempting to seat player:", player.Name)

		-- Wait for the character to be fully loaded
		local character = player.Character or player.CharacterAdded:Wait()

		-- Ensure the character is valid
		if not character then
			print("Character is nil for player:", player.Name)
			return
		end

		-- Check if the Humanoid exists
		local humanoid = character:FindFirstChild("Humanoid")
		if not humanoid then
			print("Humanoid not found for player:", player.Name)
			return
		end

		-- Check if the HumanoidRootPart exists
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
		if not humanoidRootPart then
			print("HumanoidRootPart not found for player:", player.Name)
			return
		end

		-- Clone the character manually (to avoid potential issues with entire model cloning)
		local characterClone = Instance.new("Model")
		characterClone.Name = player.Name .. "_NPC"

		-- Function to clone a character part
		local function cloneCharacterPart(partName)
			local part = character:FindFirstChild(partName)
			if part then
				local clonedPart = part:Clone()
				clonedPart.Parent = characterClone
				print(partName .. " cloned successfully.")
			else
				print(partName .. " not found for player:", player.Name)
			end
		end

		-- Handle both R6 and R15 models
		if humanoid.RigType == Enum.HumanoidRigType.R6 then
			-- R6 model uses "Torso"
			cloneCharacterPart("Torso")
			cloneCharacterPart("LeftLeg")
			cloneCharacterPart("RightLeg")
			cloneCharacterPart("LeftArm")
			cloneCharacterPart("RightArm")
		elseif humanoid.RigType == Enum.HumanoidRigType.R15 then
			-- R15 model uses "UpperTorso" and "LowerTorso"
			cloneCharacterPart("UpperTorso")
			cloneCharacterPart("LowerTorso")
			cloneCharacterPart("LeftUpperArm")
			cloneCharacterPart("LeftLowerArm")
			cloneCharacterPart("RightUpperArm")
			cloneCharacterPart("RightLowerArm")
			cloneCharacterPart("LeftUpperLeg")
			cloneCharacterPart("LeftLowerLeg")
			cloneCharacterPart("RightUpperLeg")
			cloneCharacterPart("RightLowerLeg")
		end

		-- Clone essential parts: Head, HumanoidRootPart, and Humanoid
		cloneCharacterPart("Head")
		cloneCharacterPart("HumanoidRootPart")
		cloneCharacterPart("Humanoid")

		-- Clone clothing (Shirt, Pants)
		local shirt = character:FindFirstChild("Shirt")
		if shirt then
			local shirtClone = shirt:Clone()
			shirtClone.Parent = characterClone
			-- Apply the cloned shirt to the cloned humanoid
			local humanoidClone = characterClone:FindFirstChild("Humanoid")
			if humanoidClone then
				humanoidClone.Shirt = shirtClone
			end
			print("Cloned Shirt.")
		else
			print("Shirt not found for player:", player.Name)
		end

		local pants = character:FindFirstChild("Pants")
		if pants then
			local pantsClone = pants:Clone()
			pantsClone.Parent = characterClone
			-- Apply the cloned pants to the cloned humanoid
			local humanoidClone = characterClone:FindFirstChild("Humanoid")
			if humanoidClone then
				humanoidClone.Pants = pantsClone
			end
			print("Cloned Pants.")
		else
			print("Pants not found for player:", player.Name)
		end

		-- Clone accessories
		for _, accessory in pairs(character:GetChildren()) do
			if accessory:IsA("Accessory") then
				local clonedAccessory = accessory:Clone()
				clonedAccessory.Parent = characterClone
				print("Cloned accessory:", accessory.Name)
			end
		end

		-- Remove non-cloneable objects like PlayerScripts or Player components
		for _, child in character:GetChildren() do
			if child:IsA("Script") or child:IsA("LocalScript") or child:IsA("PlayerScripts") then
				child:Destroy()
			end
		end

		-- Ensure we have a valid PrimaryPart
		if not characterClone.PrimaryPart then
			-- Try to set the PrimaryPart to HumanoidRootPart if it's not set
			local primaryPart = characterClone:FindFirstChild("HumanoidRootPart")
			if primaryPart then
				characterClone.PrimaryPart = primaryPart
				print("PrimaryPart set to HumanoidRootPart.")
			else
				print("Failed to set PrimaryPart for cloned character.")
				return
			end
		end

		-- Position the NPC at a seat
		if seatIndex <= #seats then
			local seat = seats[seatIndex]
			characterClone:SetPrimaryPartCFrame(seat.CFrame)
			seatIndex = seatIndex + 1
		else
			print("No more seats available for", player.Name)
			return
		end

		-- Parent the cloned character to the workspace
		characterClone.Parent = Workspace
		print("Successfully cloned and seated player:", player.Name)
	end

	-- Connect to PlayerAdded event
	Players.PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function()
			seatPlayerAsNPC(player)
		end)
	end)

	-- Handle existing players
	for _, player in Players:GetPlayers() do
		if player.Character then
			seatPlayerAsNPC(player)
		else
			player.CharacterAdded:Wait()
			seatPlayerAsNPC(player)
		end
	end
else
	print("BusB or Seats folder not found in Workspace.")
end

Oh wait you made it make a model instead of cloning the character can’t be cloned? But you can set the character.Archivable = true and then you can clone it, which will be much better.

yeah, I was trying to just spawn them in through a model and I just got it to bring their accessories over so I am going to run a few more tests but it seems to be working good now.

image
image

an alternative:
prepare a blank npc in the ServerStorage
clone it into workspace

Statue.Humanoid:ApplyDescription(Players:GetHumanoidDescriptionFromUserId(userId))

i cant do that because in the cutscene i would need to get the exact amount of players that joined and put that amount on the bus to clone so it wouldnt make sense to do it like that.

you may be mistaken. you will clone just about every player/character added like you already does. but instead of cloning directly their character, you clone the blank npc, and then dress it up with the humanoid description of the player