Combining two scripts into one

Hi developers, I tried a script from a person who helped me last time (thanks EXPLOSION_KING). This script puts certain clothes, accessories and hair on the player when he clicks on an image button. Now there is only one problem and that is I wanted the player to also be given a certain bundle so that certain characters with certain clothes don’t look weird (the sight of a male avatar wearing female clothes and accessories is a bit weird :joy:). I have the bundle script (which might work) and the script that gives the player clothes and accessories, how can I connect this bundle script to the other script? Is that even possible?
(I’m not the best scripter so I would be happy if someone could explain to me how this works)

Bundle Script:

Newbundle = 382061


if MorphInfo.Bundle then
   Humanoid.RequiresNeck = false

   local function TryGet(context, object, funcName, ...)
   	local success, result = pcall(object[funcName], object, ...)

   	if success then
   		return result
   	else
   		warn("Invalid", context .. ':', ..., "(Error:", result .. ')')
   	end
   end

   local Bundle
   local Info = TryGet("BundleId", AssetService, "GetBundleDetailsAsync", MorphInfo.Bundle)

   if Info then
   	local Id = 0

   	for Index, Item in pairs(Info.Items) do
   		if Item.Type == "UserOutfit" then
   			Id = Item.Id
   			break
   		end
   	end

   	if Id > 0 then
   		Bundle = TryGet("Bundle", Players, "GetHumanoidDescriptionFromOutfitId", Id)
   	end
   }

   Humanoid:ApplyDescription(Bundle)
end

The working script that put clothings on the player:

local script place it in button.

local imageButton = script.Parent -- Reference to the ImageButton
local replicatedStorage = game:GetService("ReplicatedStorage")
local changeEvent = replicatedStorage:WaitForChild("ChangeClothesAndAccessories")

imageButton.MouseButton1Click:Connect(function()
    local player = game.Players.LocalPlayer
    if player then
        changeEvent:FireServer()
    end
end)

Normal sever script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")

local changeEvent = replicatedStorage:WaitForChild("ChangeClothesAndAccessories")

-- New clothing and accessories IDs (Replace these with the actual asset IDs you want to use)
local newShirtId = 12118562014
local newPantsId = 12903182530
local newAccessories = {
    17850329744,
    17569661551,
    -- Add more accessory IDs as needed
}

-- Function to remove old clothes and accessories
local function removeOldClothesAndAccessories(character)
    -- Remove old clothes
    for _, child in pairs(character:GetChildren()) do
        if child:IsA("Shirt") or child:IsA("Pants") then
            child:Destroy()
        end
    end

    -- Remove old accessories
    for _, child in pairs(character:GetChildren()) do
        if child:IsA("Accessory") then
            child:Destroy()
        end
    end

    -- Remove old tools
    for _, tool in pairs(character:GetChildren()) do
        if tool:IsA("Tool") then
            tool:Destroy()
        end
    end
end

-- Function to add new clothes and accessories
local function addNewClothesAndAccessories(character, player)
    -- Add new shirt
    local newShirt = Instance.new("Shirt", character)
    newShirt.ShirtTemplate = "rbxassetid://" .. newShirtId

    -- Add new pants
    local newPants = Instance.new("Pants", character)
    newPants.PantsTemplate = "rbxassetid://" .. newPantsId

    -- Add new accessories
    for _, accessoryId in ipairs(newAccessories) do
        local success, accessoryModel = pcall(function()
            return game:GetService("InsertService"):LoadAsset(accessoryId)
        end)
        if success then
            local accessory = accessoryModel:FindFirstChildOfClass("Accessory")
            if accessory then
                accessory.Parent = character
            end
            accessoryModel:Destroy()
        else
            warn("Failed to load accessory with ID: " .. tostring(accessoryId))
        end
    end

    -- Add new tools from ServerStorage
    local vipToolsFolder = serverStorage:FindFirstChild("VIPTools")
    if vipToolsFolder then
        for _, tool in pairs(vipToolsFolder:GetChildren()) do
            if tool:IsA("Tool") then
                local clonedTool = tool:Clone()
                clonedTool.Parent = player.Backpack
            end
        end
    else
        warn("VIPTools folder not found in ServerStorage")
    end
end

-- Function to handle the RemoteEvent
local function onChangeClothesAndAccessories(player)
    local character = player.Character or player.CharacterAdded:Wait()
    removeOldClothesAndAccessories(character)
    addNewClothesAndAccessories(character, player)
end

changeEvent.OnServerEvent:Connect(onChangeClothesAndAccessories)
1 Like

First, get the service used for assets, assetService:

local assetService = game:GetService("AssetService")

Hardcode the bundleId along with the other variables at the top:

-- New clothing and accessories IDs (Replace these with the actual asset IDs you want to use)
local newShirtId = 12118562014
local newPantsId = 12903182530
local newAccessories = {
    17850329744,
    17569661551,
    -- Add more accessory IDs as needed
}

-- Bundle ID (Replace with your actual bundle ID)
local bundleId = 382061

Create a new function called applyBundle(character, bundleId) with your logic:

local function applyBundle(character, bundleId)
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    if humanoid then
        -- Set RequiresNeck to false
        humanoid.RequiresNeck = false

        -- Apply the bundle
        local function tryGet(context, object, funcName, ...)
            local success, result = pcall(object[funcName], object, ...)
            if success then
                return result
            else
                warn("Invalid", context .. ':', ..., "(Error:", result .. ')')
            end
        end

        local info = tryGet("BundleId", assetService, "GetBundleDetailsAsync", bundleId)
        if info then
            local id = 0
            for _, item in pairs(info.Items) do
                if item.Type == "UserOutfit" then
                    id = item.Id
                    break
                end
            end

            if id > 0 then
                local bundle = tryGet("Bundle", players, "GetHumanoidDescriptionFromOutfitId", id)
                if bundle then
                    humanoid:ApplyDescription(bundle)
                end
            end
        end
    end
    return true
end

Then finally, call this function in your onChangeClothesAndAccessories function alongside your existing calls:

local function onChangeClothesAndAccessories(player)
    local character = player.Character or player.CharacterAdded:Wait()
    removeOldClothesAndAccessories(character)
    applyBundle(character, bundleId)  -- Apply the bundle
    addNewClothesAndAccessories(character, player)
end
1 Like

OK, I summarize what I understand from it (I am not very good in English):

local assetService = game:GetService("AssetService")
-- New clothing and accessories IDs (Replace these with the actual asset IDs you want to use)
local newShirtId = 12118562014
local newPantsId = 12903182530
local newAccessories = {
    17850329744,
    17569661551,
    -- Add more accessory IDs as needed
}

-- Bundle ID (Replace with your actual bundle ID)
local bundleId = 382061

local function applyBundle(character, bundleId)
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    if humanoid then
        -- Set RequiresNeck to false
        humanoid.RequiresNeck = false

        -- Apply the bundle
        local function tryGet(context, object, funcName, ...)
            local success, result = pcall(object[funcName], object, ...)
            if success then
                return result
            else
                warn("Invalid", context .. ':', ..., "(Error:", result .. ')')
            end
        end

        local info = tryGet("BundleId", assetService, "GetBundleDetailsAsync", bundleId)
        if info then
            local id = 0
            for _, item in pairs(info.Items) do
                if item.Type == "UserOutfit" then
                    id = item.Id
                    break
                end
            end

            if id > 0 then
                local bundle = tryGet("Bundle", players, "GetHumanoidDescriptionFromOutfitId", id)
                if bundle then
                    humanoid:ApplyDescription(bundle)
                end
            end
        end
    end

    return true
end
local function onChangeClothesAndAccessories(player)
    local character = player.Character or player.CharacterAdded:Wait()
    removeOldClothesAndAccessories(character)
    applyBundle(character, bundleId)  -- Apply the bundle
    addNewClothesAndAccessories(character, player)
end

are the spaces I made between the scripts okay? or will it not work?

LUA doesn’t consider spaces or indents as part of its syntax; they are used purely for readability and organization. LUA interprets code based solely on its syntax rules and structures, regardless of whitespace. So yes, the spaces you have made should be okay.

1 Like

Ok, do i have to rename the Remoteevent?

We didn’t modify the remote event itself; rather, we integrated your existing server script with the bundle script you provided. Unless I misunderstood something or there’s an additional requirement, why would we need to rename it?

1 Like

server script try this
may need editing to work

local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")
local assetService = game:GetService("AssetService") -- Make sure to get AssetService
local changeEvent = replicatedStorage:WaitForChild("ChangeClothesAndAccessories")

-- New clothing and accessories IDs (Replace these with the actual asset IDs you want to use)
local newShirtId = 12118562014
local newPantsId = 12903182530
local newAccessories = {
    17850329744,
    17569661551,
    -- Add more accessory IDs as needed
}
local newBundleId = 382061

-- Function to remove old clothes and accessories
local function removeOldClothesAndAccessories(character)
    -- Remove old clothes
    for _, child in pairs(character:GetChildren()) do
        if child:IsA("Shirt") or child:IsA("Pants") then
            child:Destroy()
        end
    end

    -- Remove old accessories
    for _, child in pairs(character:GetChildren()) do
        if child:IsA("Accessory") then
            child:Destroy()
        end
    end

    -- Remove old tools
    for _, tool in pairs(character:GetChildren()) do
        if tool:IsA("Tool") then
            tool:Destroy()
        end
    end
end

-- Function to add new clothes and accessories
local function addNewClothesAndAccessories(character, player)
    -- Add new shirt
    local newShirt = Instance.new("Shirt", character)
    newShirt.ShirtTemplate = "rbxassetid://" .. newShirtId

    -- Add new pants
    local newPants = Instance.new("Pants", character)
    newPants.PantsTemplate = "rbxassetid://" .. newPantsId

    -- Add new accessories
    for _, accessoryId in ipairs(newAccessories) do
        local success, accessoryModel = pcall(function()
            return game:GetService("InsertService"):LoadAsset(accessoryId)
        end)
        if success then
            local accessory = accessoryModel:FindFirstChildOfClass("Accessory")
            if accessory then
                accessory.Parent = character
            end
            accessoryModel:Destroy()
        else
            warn("Failed to load accessory with ID: " .. tostring(accessoryId))
        end
    end

    -- Add new tools from ServerStorage
    local vipToolsFolder = serverStorage:FindFirstChild("VIPTools")
    if vipToolsFolder then
        for _, tool in pairs(vipToolsFolder:GetChildren()) do
            if tool:IsA("Tool") then
                local clonedTool = tool:Clone()
                clonedTool.Parent = player.Backpack
            end
        end
    else
        warn("VIPTools folder not found in ServerStorage")
    end
end

-- Function to apply new bundle
local function applyNewBundle(humanoid, bundleId)
    if bundleId then
        humanoid.RequiresNeck = false

        local function tryGet(context, object, funcName, ...)
            local success, result = pcall(object[funcName], object, ...)

            if success then
                return result
            else
                warn("Invalid", context .. ':', ..., "(Error:", result .. ')')
            end
        end

        local bundle
        local info = tryGet("BundleId", assetService, "GetBundleDetailsAsync", bundleId)

        if info then
            local id = 0

            for _, item in pairs(info.Items) do
                if item.Type == "UserOutfit" then
                    id = item.Id
                    break
                end
            end

            if id > 0 then
                bundle = tryGet("Bundle", players, "GetHumanoidDescriptionFromOutfitId", id)
            end
        end

        if bundle then
            humanoid:ApplyDescription(bundle)
        end
    end
end

-- Function to handle the RemoteEvent
local function onChangeClothesAndAccessories(player)
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    if humanoid then
        removeOldClothesAndAccessories(character)
        addNewClothesAndAccessories(character, player)
        applyNewBundle(humanoid, newBundleId)
    end
end

changeEvent.OnServerEvent:Connect(onChangeClothesAndAccessories)

1 Like

let me know if my script works!! Once you have tried it.