Script Not Working

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)
```.
1 Like
local Hat = game.ServerStorage:WaitForChild("NerdHair"):Clone()

Local scripts do not have access to ServerStorage service. Try placing it inside the ReplicatedStorage.

1 Like

Okay, let me try that @mishajones

@Amiaa16

it works, but then it falls off, got a fix?

Is the hat attached to the characters head? If not it just behaves like a simple part that is not anchored.

You need to add an attachment to the hat and name it ‘HeadAttachment’

1 Like

Okay @Nova_MrRoyal I’ll try thay!

1 Like

You should move your code that adds a new hat, outside the FOR loop. I think also you may need to weld the hat on.

@JasonTheOwner How would I weld it to the player in code?

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).

1 Like

This looks like a LocalScript in a part, in workspace, where it will never run. Move the LocalScript somewhere where it will run and see what happens.

Nevermind, I was wrong.

MouseButton1Click is an event that belongs to GuiButtons, so he did good.

Thank you, but still doesn’t show up on other players screen.

Whoops. My bad, I was confusing that event with a ClickDetector.

1 Like

So can anyone help with my new 2 problems?

One, it won’t weld the hat to the player because, I don’t know how, and 2
it won’t show on other player screens?

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…

1 Like

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 Handle Part . 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.

2 Likes

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!

It is okay, no one is born knowing Lua. But knowing how do remote events and functions work is very basic to develop in Roblox.

Side note: now you can place the hat inside the ServerStorage again, since server scripts can access that service!