When the Button is cklicked, it don't do what i have scripted

I don’t understand what i can do else so i ask you, i want to create a VIP fit for my Game, If you cklick an imagebutton named: GirlVIP the old clothings, Accessoires and Tools from the Player gets deleated and the new Accessoires, clothings and Tools get Paste in to the Player. But the script wohnt Work, If i cklick ON the imagebutton in the screenGUI, the Player gets No clothings and Accessoires, but the Players clothings, Accessoires and Tools get deleated. Here IS the Script:

local imageButton = script.Parent -- Reference to the ImageButton
local serverStorage = game:GetService("ServerStorage") -- Reference to ServerStorage
local players = game:GetService("Players") -- Reference to Players

-- 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 accessoryModel = game:GetService("InsertService"):LoadAsset(accessoryId)
		local accessory = accessoryModel:FindFirstChildOfClass("Accessory")
		if accessory then
			accessory.Parent = character
		end
		accessoryModel:Destroy()
	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 button click
local function onButtonClick(player)
	local character = player.Character or player.CharacterAdded:Wait()
	removeOldClothesAndAccessories(character)
	addNewClothesAndAccessories(character, player)
end

-- Connect the button click to the function for each player
imageButton.MouseButton1Click:Connect(function()
	local player = players.LocalPlayer -- This assumes the script is a LocalScript
	if player then
		onButtonClick(player)
	end
end) 

The errormessage:

The reaction when the imagebutton ist cklicked:

Well the error explains it self doesn’t it?

You will have to use a remote event in this case.

Do you know how to work with remote events or you need assistance?

wait let me rethink this script a bit.

I don’t know anything about remote Events

Uhm is this all a local script or?

Another thing is that you can’t access the Server Storage from the client.

1 Like

From the error you got you are doing this script on the server side. You will need to do it on the client side by using a local script. To use remote events this is how:

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)

3 Likes

This should work! I was a bit slow for this. Explosion_King got me

@killerrdrohne If you need to learn some more probably read the documentation.

1 Like

Thankyou For all your Help, i will try it tomorrow.have a great eavening :smiley:

Alright! Let me know if my solution works.

1 Like

Did my solution work?

This text will be blurred

1 Like

I am sorry but i can’t try it today ( i am Not at Home ) but i will try it tomorrow :smiley:

hello, it’s been a while since I haven’t replied, sorry about that, I’ve just been very busy lately. I wanted to ask if you need a remote event for this script? (as I mentioned before, I don’t know anything about remote events and have avoided them as much as possible)

local imageButton = script.Parent -- Reference to the ImageButton
local serverStorage = game:GetService("ServerStorage") -- Reference to ServerStorage
local players = game:GetService("Players") -- Reference to Players

-- 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
	local InsertService = game:GetService("InsertService")
	for _, accessoryId in ipairs(newAccessories) do
		local success, accessoryModel = pcall(function()
			return InsertService:LoadAsset(accessoryId)
		end)

		if success and accessoryModel then
			local accessory = accessoryModel:FindFirstChildOfClass("Accessory")
			if accessory then
				accessory.Parent = character
			end
			accessoryModel:Destroy()
		else
			warn("Failed to load accessory with ID: " .. 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 button click
local function onButtonClick(player)
	local character = player.Character or player.CharacterAdded:Wait()
	removeOldClothesAndAccessories(character)
	addNewClothesAndAccessories(character, player)
end

-- Connect the button click to the function for each player
imageButton.MouseButton1Click:Connect(function()
	local player = players.LocalPlayer -- This assumes the script is a LocalScript
	if player then
		onButtonClick(player)
	end
end)
1 Like

Yes you do. All you need to do is add a remote event to replicated storage and name it ChangeClothesAndAccessories

1 Like

Thankyou everyone! IT worked :smile:

I have one last question: How could I connect this bundle script to the VIP script? is it even possible?
( I’m not the best scripter, so I don’t know if the script even works, I copied it from the step that a friend provided me )

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

I replied to your other post

This text will be blurred

1 Like

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