Clothing Changer Tool Problem

:smiley: A little coding practice for myself, I’m making a realistic clothes hanger!!

:star:How it works:
A hanger tool is given to the player, the player may dress themselves with the provided clothing shown on the hanger.

Player may change their clothing with the hanger, and put their shirt onto the hanger in exchange. If the hanger doesn’t have anything, the player is able to put their shirt onto the hanger, and vice versa.

:sob:Problem:
image
The script some how does not recognize the change of the hanger’s clothes or the player’s. It skips the if statements and goes straight to clothing swap.

Here is my function code:

local function Clicked(player)
    local character = player.Character
	if character then
		
		
		print(character.Name .. " found with script!")
		
        local playerShirt = character.Shirt
        if playerShirt then
            print("Player has a shirt!")
            local clothing = model.Shirt
			if clothing then
				
                print("Model also has a shirt!")

                local modelShirtTemplate = clothing.ShirtTemplate
				local playerShirtTemplate = playerShirt.ShirtTemplate
				if modelShirtTemplate and playerShirtTemplate then
					
					local modelShirtTemplate = clothing.ShirtTemplate
					clothing.ShirtTemplate = playerShirt.ShirtTemplate
					playerShirt.ShirtTemplate = modelShirtTemplate
                    print("Shirts exchanged!")
                else
                    print("ShirtTemplate not found on one of the shirts!😱😱")
				end
				
            else
                print("Model does not have a shirt, hanging player's shirt on!")

                playerShirt.Parent = model
            end
        else
            print("Player does not have a shirt, taking from the hanger if available!")

            local wear = model.Shirt
			if wear then
				wear.Parent = character
                print("Player wore the shirt!")
            end
        end
    end
end
local playerShirtTemplate = playerShirt.ShirtTemplate

I believe this is where the problem lies. Essentially, you’re telling the script that the template is an object that may or may not exist, and if it doesn’t exist that will throw an error. Consider changing this and similar lines of code to use :FindFirstChild() instead.

1 Like

ShirtTemplate is a property for clothes!! Here is the example:
image

I also have tried using:FindFirstChild(), it also did not work.

The problem only occurs when the player’s shirt is removed.

Hmmm. I see. Could you check which line the error happens on, or maybe include a full picture of the output?

Highlighted is the line that the error lead me to:
image

Outputs from the script:
image

I’m kinda going out on a limb here, but could you try changing local playerShirt = character,Shirt to local playerShirt = character.Shirt.ShirtTemplate? I’m not sure about it but i think it’s worth a shot.

1 Like

I found my solution…
image

Shirt tool cannot be named the same thing as the actual shirt…

I see! Well, good job finding the solution, this was tricky problem. Cheers!

1 Like

I really appreciate your patience and guidance, thank you so much!!! :pray::pray::pray::sob:

1 Like

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