Trouble with try on/take off pants system

I’m making a try on/take off system.

here’s my mannequin code (only using pants)

local clickDetector = script.Parent.ClickDetector
local originalPantsFolder = game.Workspace:FindFirstChild("OriginalPants")
local triedOn = game.ReplicatedStorage.triedOn

local cooldown = 3
local lastClickTime = {}
local hasInteracted = {}

local function onClick(player, pants, clickDetector)
    print("Mannequin clicked by:", player.Name)

    local currentTime = tick()
    if lastClickTime[player] and currentTime - lastClickTime[player] < cooldown then
        print("Cooldown in effect. Please wait before clicking again.")
        return
    end

    if hasInteracted[player] then
        print("Mannequin has already been interacted with by this player.")
        triedOn:FireClient(player)
        return
    end

    local character = player.Character or player.CharacterAdded:Wait()
    print("Character:", character)

    -- Find the OriginalPants inside the character and move them back if found
    local originalPants = character:FindFirstChild("OriginalPants")
    if originalPants then
        print("Found OriginalPants inside character, moving them back...")
        originalPants.Parent = originalPantsFolder
        print("Moved OriginalPants back to the folder")
    end

    -- Remove existing pants
    for _, item in ipairs(character:GetChildren()) do
        if item:IsA("Pants") then
            if not item:GetAttribute("Temporary") then
                item:Destroy()
                print("Removed existing pants")
            end
        end
    end

    -- Equip new pants
    local pantsClone = pants:Clone()
    pantsClone.Parent = character
    print("Equipped new pants")

    -- Enable pants GUI (assuming it's a child of the mannequin)
    local pantsGUI = clickDetector.Parent:FindFirstChild("PantsGui")
    if pantsGUI then
        pantsGUI.Enabled = true
        pantsGUI.Parent = player.PlayerGui
        print("Enabled pants GUI")
    else
        print("Pants GUI not found.")
    end

    lastClickTime[player] = currentTime
    hasInteracted[player] = true
end

local function onMannequinClicked(player)
    onClick(player, clickDetector.Parent.Pants, clickDetector)
end

clickDetector.MouseClick:Connect(onMannequinClicked)


now, the code sort of works. when i try to click on another mannequin, it says it can’t detect the originalPants, even though it’s in the character.

the gui’s no button script (which moves the originalPants back into the character):

-- Assuming this is attached to a button within a GUI
local originalPantsFolder = game.Workspace:WaitForChild("OriginalPants")

script.Parent.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local character = player.Character or player.CharacterAdded:Wait()

	if character then
		local humanoid = character:FindFirstChildOfClass("Humanoid")

		if humanoid then
			local originalPants = originalPantsFolder:FindFirstChild("OriginalPants")
			if originalPants then
				-- Remove any temporary pants first
				for _, item in pairs(character:GetChildren()) do
					if item:IsA("Pants") and item:GetAttribute("Temporary") then
						item:Destroy()
					end
				end

				-- Remove the equipped pants
				local equippedPants = character:FindFirstChild("Pants")
				if equippedPants then
					equippedPants:Destroy()
				end

				-- Clone the original pants
				originalPants.Parent = character

				-- Remove the original pants from the originalPantsFolder

				-- Disable the GUI button after equipping the original pants
				script.Parent.Parent.Parent.Enabled = false
			else
				print("Original pants not found for player:", player.Name)
			end
		end
	end
end)

i’ve looked everywhere and found no solutions.

From what it looks like, you’re parenting the original pants back to the character on the client. If that is the case, then it will not be replicated to the server and explains why the server script says that the pants do not exist.

I noticed that, but I’m having a hard time figuring out how I’d go about connecting them with RemoteEvents.

Nevermind, I managed to do it. Thanks for the help.

1 Like

One thing you could do is change the name of the pants to include the name of the player as well. Assuming you change the name somewhere else (as, by default, they are not named “OriginalPants”) and you have access to the player instance (since, well… you’re changing the name of the pants inside of the player’s character), you can do that like so:

-- Assuming player and pants are already defined
pants.Name = player.Name .."_Pants"

I say this because another issue with your current code would be the fact every player’s original pants instance would have the same name (“OriginalPants”). If two players clicked on a mannequin, there would be two different pants instances inside of the originalPantsFolder. Player2 could then click the no button and the original pants found would likely be Player1's. Anyways, when you click on the mannequin, you could just check if the player’s original pants are inside of the folder and if they are, parent them back to the character on the server. At the same time, you can have a RemoteEvent fired to the server if the player clicks on the no button. This RemoteEvent will be in charge of setting the original pants back to the character – in the event that the player does not click on another mannequin.

Edit: Glad you got it working! I’m still going to reply with this as I feel like you should consider what I said about the names for the pants instance being the same for every player.

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