I am I am getting an error saying “Add Accessory should be passed a valid accessory object”. The script still work and the player still gets the accessory, I just want to know what is causing the error as if I don’t fix it, this can cause a problem during development of the game down the line.
local SS = game:GetService("ServerStorage")
local Hats = SS.RedArmyStuff.Hats:GetChildren()
local function playerAdded(player)
player.CharacterAdded:Connect(function(char)
local RandomHat = nil
if player.Team.Name == "Red Army" then
wait(0.5)
RandomHat = Hats[math.random(#Hats)]
NewHat = RandomHat:Clone()
print ("Hat Created")
end
local hum = char:WaitForChild("Humanoid")
hum:AddAccessory(NewHat)
print("Hat Added")
end)
end
game.Players.PlayerAdded:Connect(playerAdded)
The script is located in serverstorage and basically make it so if a character spawns in as a certain team, they will get a random hat out of a folder. Like I said it is working, I just want to fix the error that is coming up.
The error is self explanatory, you have to pass in an accessory instance or anything related to accessories. Could you show what the Hats folder contains?
It could also be that if t he team of the player is not Red Army it would error as NewHat is only made if they’re on the Red Army team, thus it would try to use nil on AddAccessory
Then it’s the 2nd thing I mentioned, in your code, NewHat is only made in the statement that checks if you’re in the Red Army team, if you are not, it’s going to always error that error since NewHat would be nil, you may have to fix the code around, I think you meant to put
local hum = char:WaitForChild("Humanoid")
hum:AddAccessory(NewHat)
print("Hat Added")
In the if statement
local function playerAdded(player)
player.CharacterAdded:Connect(function(char)
local RandomHat = nil
if player.Team.Name == "Red Army" then
wait(0.5)
RandomHat = Hats[math.random(#Hats)]
NewHat = RandomHat:Clone()
print ("Hat Created")
local hum = char:WaitForChild("Humanoid")
hum:AddAccessory(NewHat)
print("Hat Added")
end
end)
end
If that doesn’t work either, I think something else may be going on when trying to get a hat, could you print out RandomHat and NewHat to see what they contain?
@ScottishKiltBearer Try doing what I had mentioned? Your other issue may be that NewHat is probably local to the if statement scope only, so the code to add the accessory may need to be placed in the if statement as well
You don’t really need to the repeat until line, you already wait for the Humanoid to exist, besides that, I think it should work now that the code to add the accessory is with the scope where NewHat is changed. It can’t be an issue with the class itself since we know they’re accessories