Changing Hair Texture

I’m trying to make it so it changes the current hat’s/accessory’s texture when you click on a gui button but for some reason it is not working. It doesn’t show any errors at all.

local red = script.Parent
red.MouseButton1Down:connect(function(Player)
	for _,Hat in pairs (Player.Character:GetDescendants()) do
		if Hat.ClassName == "Accessory" then
			for _, mesh in pairs (Hat:GetDescendants()) do
				if mesh.ClassName == "SpecialMesh" or mesh.ClassName == "Mesh" then
					mesh.TextureId = "rbxassetid://5737199461"
					end
				end
			end
		end
	end)

My script is above, I’m fairly new so if there’s any error please let me know.

Hey. I have modified your code a bit… Try something like this. Hope this helps :slightly_smiling_face:


local red = script.Parent
local Player = game.Players.LocalPlayer

red.MouseButton1Down:connect(function()
for _,Hat in pairs (Player.Character:GetDescendants()) do
if Hat.ClassName == “Accessory” then
for _, mesh in pairs (Hat:GetDescendants()) do
if mesh.ClassName == “SpecialMesh” or mesh.ClassName == “Mesh” then
mesh.TextureId = “rbxassetid://5737199448”
end
end
end
end
end)

You’re using “LocalPlayer” which means a Local Script but I’m trying to do it in a normal Script.

2 Likes

MouseButton1Down doesn’t have a player argument. Also, you shouldn’t use a script to do GUI actions. Instead, use a local script and send a remote event to the server if you want to change the hair color so that everyone can see it.

Is there any way to do it through a script or do I have to use a local script and a remote event?

You can but you have to put your code in a PlayerAdded event to get the player’s name. Also, you have to get the button through the PlayerGui.

I already made it so it gets the button to PlayerGui also do you mean like this?

local red = script.Parent
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
	red.MouseButton1Down:connect(function()
		for _,Hat in pairs (player.Character:GetDescendants()) do
			if Hat.ClassName == "Accessory" or Hat.ClassName == "Hat" then
				for _, mesh in pairs (Hat:GetDescendants()) do
					if mesh.ClassName == "SpecialMesh" or mesh.ClassName == "Mesh" then
						mesh.TextureId = "rbxassetid://5737199448"
					end
				end
			end
		end
	end)	
end)

If so, that doesn’t work.

No I meant:

player.PlayerGui.

And put the script in the ServerScriptService.

1 Like

If you are saying about Connecting a MouseButton1Click event from server side, please don’t do that. It will create replication issues, as I believe that event will only check in Server Side but the Client’s UI works in the client side itself.

What I would do for this is, make a MouseButton1Click Connection in a localscript and place it in StarterGui or wherever you want (should be in a location where localscripts work).

Then OnClick You should fire an event and then change the Hair texture in the server side OnServerEvent handler.

Example code:

--Client Side Script
local red = script.Parent
local event = <pathToEvent>

red.MouseButton1Down:Connect(function()
	event:FireServer()
end)

--Server Side Script
local event = <pathToEvent>

event.OnServerEvent:Connect(function(player)
 for _,Hat in pairs (player.Character:GetDescendants()) do
		if Hat.ClassName == "Accessory" or Hat.ClassName == "Hat" then
			for _, mesh in pairs (Hat:GetDescendants()) do
				if mesh.ClassName == "SpecialMesh" or mesh.ClassName == "Mesh" then
					mesh.TextureId = "rbxassetid://5737199448"
				end
			end
		end
 end
end)
1 Like

That’s why I said connect to the button through the player’s PlayerGui so it knows who the button belongs to. When clicking the button on the client side, it replicates to the server too. I tested it. Both way works but Remote Event is better I think.

2 Likes

Thank you, that fixed the problem.

1 Like