Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

ClickDetector changing clothing help?

I am trying to make a part that when clicked changes the players shirt. I'm not getting any errors, but it's not working. Why isn't this script working?

workspace.Part.ClickDetector.MouseClick:connect(function(playerWhoClicked)
		game.Workspace.ChildAdded:connect(function(plr)
			local name = plr.Name
			local playerinplayers = game.Players:FindFirstChild(plr.Name)
			local shirt = Instance.new('Shirt', plr)
			shirt.ShirtTemplate = 'rbxassetid://423017688'
	end)
end)

2 answers

✓ Accepted answer

##Problem

You already get the player on line 1. MouseClick from ClickDetectors returns the player who clicked the part ClickDetector is located in. Your ChildAdded event is completely unnecessary.

Also, as GuestLIVE points out, there may be some interference between two shirts being in the player.


##Solution

Forget about ChildAdded, forget about getting the player's name, forget about getting the user through players service. You will be able to use the Player instance the MouseClick event provides to get the player who clicked on the part.

Have the script detect if there is an object named "Shirt" before making a new one. If there is already a shirt, we can just change the AssetId.


##Explanation

Whenever working with MouseClick you can always get the player who clicked the part with the first parameter of the function. When the function runs, you should check if the character exists, and if the character exists see if the Shirt exists to prevent interference between two shirts. Once you find you have a shirt, then the script will set a new ShirtTemplate.

If the script determines the player does not have a shirt or the character doesn't exist, the script will check once again if the player exists or not. If the script is to determine the character exists yet the shirt doesn't, it will create a new shirt object and place it in the player's character.


##Final Script

workspace.Part.ClickDetector.MouseClick:connect(function(playerWhoClicked)
    if playerWhoClicked.Character ~= nil and playerWhoClicked.Character:FindFirstChild('Shirt') then --Checking if the character exists, also seeing if there is an object named Shirt.
        playerWhoClicked.Character.Shirt.ShirtTemplate = 'rbxassetid://423017688' --Set the ShirtTemplate
    else --If the player doesn't exist or the shirt doesn't exist, resort to this.
        if playerWhoClicked.Character ~= nil then --Since one of the two conditions could be false in the first if then statement, we want to make sure the character actually exists.
            local shirt = Instance.new('Shirt', playerWhoClicked.Character) --Create new shirt object and place it in the player's character.
            shirt.ShirtTemplate = 'rbxassetid://423017688' --Set the ShirtTemplate.
        end
    end
end)

######Hopefully, this answered your question. If so, do not forget to hit the accept answer button. If you have any questions, feel free to leave them in the comments below and I will get back to you.

2 comments

0

It says "unknown global 'character'"

0

Fixed

Try deleting the old shirt that they might be wearing before there you put a new shirt on.

This discussion is preserved as it was. New answers, comments, and votes are closed.