So I am making a character/avatar customizer, and I made some of it. I just started and I am getting an error on a script that places the hair. This is the unfinished version of the script(only by a line or 2 which isn't that important). I am very confused on the error. May someone help?
Note:The way it works is that a a remote event fires when an intvalue is changed(by a gui text button). When it changes, it fires a remote event with the only argrument being the value of the intvalue. That value is used to figure out which hair piece will go on the character. Here is my script.(Note: It is a bit long due to the arrays.)
local event = game:GetService('ReplicatedStorage'):WaitForChild('ChangeHair') local Clothing = { 'None', 'Brown Charmer Hair', 'Black Ponytail', 'Normal Boy Hair', 'Blonde Action Ponytail', 'Stylish Blue Hair', 'Straight Brunette Hair', 'Brooding Black Hair', 'Cinnamon Hair', 'Ravenhaired Charmer', 'Cinnamon Hair', 'Messy Hair', 'Black and Red' } local HairIds = { 0, 376548738, 376527350, 13477818, 398673196, 846803597, 3814496544, 13655562, 77799954, 13745548, 26658141, 14815761 } event.OnServerEvent:Connect(function(fakeplr, value) print(value) local insertService = game:GetService('InsertService') local selectedHair = Clothing[value] local selectedId = HairIds[value] if selectedId~= 0 then local hair = insertService:LoadAsset(selectedId) hair.Parent = game.Workspace.Rig end end)
I will appreciate it if you help as I am very confused.
Your arrays are not the same length, and value
is likely nil. You have to pass a valid argument to value
from the client in order to make it work. If you're passing something that cannot be replicated over the server/client boundary, that could also be why it's nil.
As for the arrays, I suggest that you make them the same length. #Clothing
is 13 and #HairIds
is 12. Go through the first array and insert another ID into HairIds
according to the values in Clothing
.
When you insert an accessory using the InsertService
, it will always be parented to a model. If you parent the model itself to the rig, the accessory will not be attached to the rig. Therefore, line 43 is incorrect.
Line 43 should actually be:
hair:FindFirstChildWhichIsA("Accessory").Parent = workspace.Rig hair:Destroy()
Please note that the code is not supposed to be fully functional. If the hair is not an accessory (because I haven't developed in a while), then replace Accessory
with the hair's ClassName
.
Try this:
local event = game:GetService('ReplicatedStorage'):WaitForChild('ChangeHair') local Clothing = { 'None', 'Brown Charmer Hair', 'Black Ponytail', 'Normal Boy Hair', 'Blonde Action Ponytail', 'Stylish Blue Hair', 'Straight Brunette Hair', 'Brooding Black Hair', 'Cinnamon Hair', 'Ravenhaired Charmer', 'Cinnamon Hair', 'Messy Hair', 'Black and Red' } -- do the same thing here, do [id] = 'None' or the specific clothing local HairIds = { [0] = false, [376548738] = true, [376527350] = true, [13477818] = true, [398673196] = true, [846803597] = true, [3814496544] = true, [13655562] = true, [77799954] = true, [13745548] = true, [26658141] = true, [14815761] = true } event.OnServerEvent:Connect(function(fakeplr, value) print(value) local insertService = game:GetService('InsertService') local selectedHair = Clothing[value] local selectedId = HairIds[value] if selectedId then local hair = insertService:LoadAsset(value) hair.Parent = game.Workspace.Rig end end)