So I've got a script that's meant to randomly add an accessory to a player when they join from a folder in replicated storage but the script won't work, no errors come out but nothing happens at all.
local Teams = game:GetService("Teams") local Hunters = Teams:FindFirstChild("Hunters") local Folder = game.ReplicatedStorage:WaitForChild("HikingPacks") local Players = game:GetService("Players") Players.PlayerAdded:Connect(function(player) if player.Team == game.Teams["Hunters"] then print("OnTeamHunters") local Playername = player.Name local PlayerCharacter = game.Workspace:WaitForChild(Playername) local Humanoid = PlayerCharacter:WaitForChild("Humanoid") print(Playername) for i, Packs in pairs(Folder:GetChildren()) do local Chosen = math.random(1, 3) if Chosen then Humanoid:AddAccessory(Chosen) end end end end)
Could someone tell my why? Thanks!
For loop is not necessary for picking random value in a table.
To fix this:
local Accesorries = Folder:GetChildren() --Returns the accessories in the folder local RandomAccesorry = Accesorries[math.random(1, #Accesorries)]-- Gets a random accessory in the table, What basically does #Accesorries do is get the length of the table. Example: #TableHere --After getting a random accesorry, add it to the character