Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Player accessory randomiser script not working?

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

✓ Accepted answer

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

1 comment

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?

This discussion is preserved as it was. New answers, comments, and votes are closed.