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