I want it to give a certain hair when you press the button, and remove your old hats.
When I click it again it makes me bald.
I’ve tried re-ordering the scripts.
script.Parent.MouseButton1Click:Connect(function(Player)
local Player = game.Players.LocalPlayer
Player.Character.Shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=1510071483"
Player.Character.Pants.PantsTemplate = "http://www.roblox.com/asset/?id=439737201"
for i,char in pairs(Player.Character:GetChildren()) do
if char:IsA("Accessory") then
char:Destroy()
local Hat = game.ServerStorage:WaitForChild("NerdHair"):Clone()
Hat.Parent = Player.Character
end
end
end)
```.
Ok, I just realized that setting these properties do not replicate to other clients. If you want other players to see the changes made to the character appearance, you would have to use a RemoteEvent.
script.Parent.MouseButton1Click:Connect(function()
local Player = game:GetService("Players").LocalPlayer
local character = Player.Character
local shirt = character:FindFirstChild("Shirt") or Instance.new("Shirt", character)
shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=1510071483" --not replicated
local pants = character:FindFirstChild("Pants") or Instance.new("Pants", character)
pants.PantsTemplate = "http://www.roblox.com/asset/?id=439737201" --not replicated
character.Humanoid:RemoveAccessories()
local Hat = game.ReplicatedStorage:WaitForChild("NerdHair"):Clone()
Hat.Parent = character
Player.Character.Humanoid:AddAccessory(Hat)
end)
As you can see, you don’t need that loop to remove accessories; there is already a built-in function for that. Note that :AddAccessory() and :RemoveAccessories() are replicated to the server and you don’t have to worry about it (unlike setting shirt/pants properties).
This may provide some clues. Its been ages since I’ve dealt with hats, but this Roblox article on attachments, may be helpful in attaching hats (the example shows how to put some glasses on a player’s face)… it also notes that its should be done in a server script…
Local script (I added a debounce so users don’t spam hats; destroy the button or disconnect the connection if necessary)
local debounce = false
local event = game.ReplicatedStorage.RemoteEvent
script.Parent.MouseButton1Click:Connect(function()
if debounce then
return
end
debounce = true
local Player = game:GetService("Players").LocalPlayer
local character = Player.Character
event:FireServer()
character.Humanoid:RemoveAccessories()
wait(1)
debounce = false
end)
Server script (you can put this inside ServerScriptStorage)
local RS = game:GetService("ReplicatedStorage")
RS.RemoteEvent.OnServerEvent:Connect(function(Player)
local character = Player.Character
local Hat = RS:WaitForChild("NerdHair"):Clone()
Hat.Parent = character
character.Humanoid:AddAccessory(Hat)
local shirt = character:FindFirstChild("Shirt") or Instance.new("Shirt", character)
shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=1510071483"
local pants = character:FindFirstChild("Pants") or Instance.new("Pants", character)
pants.PantsTemplate = "http://www.roblox.com/asset/?id=439737201"
end)
Please do not ask other devs to do your scripts. In the link we provided, it clearly explains how to make an accessory work properly [also, my bad, it says :AddAccessory() should be called server-side]:
An Accessory is attached to the character by searching for an Attachment in the Humanoid's parent that shares the same name as an Attachment in the accessory’s HandlePart . If one is found, the Handle part will be connected to the parent of the Attachment using a Weld .
This means your accessory’s handle must contain an Attachment object as a child. You can name it “HairAttachment”, as it is one of the attachment names this function will look for inside the character.
Also, please, check this tutorial so you can solve this kind of problems by yourself in the future. We will be happy to help you if you don’t understand something, but first read it.
Thank you so much, I’m sorry for making you do scripts, I didn’t mean to, but I appreciate it so much, I’ll see the tutorial for help, thank you so much!