Hello, i have a problem with i fire event to change hair color then i get error.
This is a error:
ServerScriptService.ExecuteEventHandler:50: attempt to call a nil value
Here is my code:
game.ReplicatedStorage.CharacterCreatorEvent.ChangeHairColorEvent.OnServerEvent:Connect(function(Player, HairColor)
local Character = Player.Character
local AccessoryType = {[Enum.AssetType.HairAccessory.Value] = Enum.AssetType.HairAccessory.Name,}
for _, part in pairs(AccessoryType) do
if (part:IsA("BasePart")) then
part.Color = Color3.fromRGB(HairColor)
end
end
end)
game.ReplicatedStorage.CharacterCreatorEvent.ChangeHairColorEvent.OnServerEvent:Connect(function(Player, HairColor)
local Character = Player.Character
local AccessoryType = {[Enum.AssetType.HairAccessory.Value] = Enum.AssetType.HairAccessory.Name,}
for _, part in pairs(AccessoryType) do
if (part:IsA("BasePart")) then
part.Color = Color3.fromRGB(HairColor)
end
end
end)
game.ReplicatedStorage.CharacterCreatorEvent.ChangeHairColorEvent.OnServerEvent:Connect(function(Player, HairColor)
local Character = Player.Character
for _, Accessory in pairs(Character:GetChildren()) do
if Accessory:IsA("Accessory") then
local Handle = Accessory:FindFirstChild("Handle")
if Handle and Handle:FindFirstChild("HairAttachment") then
Handle.Color = HairColor
end
end
end
end)
Would something like this work? I think the issue is that your not looping through the character your just looping through a dictionary table with no actual parts
“HairColor” isn’t defined in your script. You also don’t need to check for an attachment. if Handle and Handle:FindFirstChild("HairAttachment") then is the same as if Handle:FindFirstChild("HairAttachment") then as well.
local storage = game:GetService("ReplicatedStorage")
storage:WaitForChild("CharacterCreatorEvent"):WaitForChild("ChangeHairColorEvent").OnServerEvent:Connect(function(Player, HairColor)
local Character = Player.Character
for _, child in pairs(Character:GetChildren()) do
if child:IsA("Accessory") then
if child:FindFirstChild("Handle") then
local handle = child:FindFirstChild("Handle")
handle.Color = Color3.new(0, 0, 0) --change to desired color
end
end
end
end)
Uh what? Hair color is defined as a parameter in his function, and you DO need to check for attachment because he specifically just wants to change hair color, accessories have different types of attachments like back attachments for swords
Also the code wasn’t even made for the purposes of directly copying and pasting, it was just an example since there very limited information of his post to begin with
He even mentioned that the code worked…
If you want to discuss more you can message me Thedagz#4176 on discord want to avoid flooding this solved post
if Handle and Handle:FindFirstChild("HairAttachment") then is the same as if Handle:FindFirstChild("HairAttachment") then as well.
If the color is being passed as an argument you do not need to locate the attachment instance. Handle.Color = (some valid Color3 value here) will suffice.