✓ 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
playerWhoClicked.Character.Shirt.ShirtTemplate = 'rbxassetid://423017688'
else
if playerWhoClicked.Character ~= nil then
local shirt = Instance.new('Shirt', playerWhoClicked.Character)
shirt.ShirtTemplate = 'rbxassetid://423017688'
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
It says "unknown global 'character'"
Fixed