Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Player accessory randomiser script not working?

Asked by 3 years ago

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!

1 answer

Log in to vote
0
Answered by 3 years ago

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
0
Ok so I've done that, but realised that it wasn't printing the players name (mine in this case) so that's probably also a problem, why would it not be getting the username? CheeseBallGuy11 12 — 3y
Ad

Answer this question