Randomized Accessories

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want every players character to have different accessories when they load into the game.

  2. What is the issue? Include screenshots / videos if possible!
    I am using math.random, and the folder for all the hats are in ServerStorage, the script works fine but all accessories end up being the same hat. I want all the hats to be different.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    There wasn’t many videos that are doing what I’m trying to do but I’ve tried looking and it’s only helped me create the script.

Here’s the script:

local ServerStorage = game:GetService("ServerStorage")
local StarterPlayer = game:GetService("StarterPlayer")
local StarterCharacter = StarterPlayer:WaitForChild("StarterCharacter")

local Hats = ServerStorage.CharacterAssets:WaitForChild("Hats"):GetChildren()
local Hat = Hats[math.random(1, #Hats)]:Clone()
Hat.Parent = StarterCharacter

Here’s an image, I’m testing with three players and they all have the exact same accessory, this has happened when I tried restarting the server, except the hat only changes but are all the same on all players, if that makes sense.

I need help on this as I’ve had this idea in mind for a while but it seems I can’t really get it to work.

1 Like

the problem is that you dont change the hat for every player just one

local ServerStorage = game:GetService("ServerStorage")
local StarterPlayer = game:GetService("StarterPlayer")
local StarterCharacter = StarterPlayer:WaitForChild("StarterCharacter")


game.Players.PlayerAdded:Connect(function(plr)
local Hats = ServerStorage.CharacterAssets:WaitForChild("Hats"):GetChildren()
local Hat = Hats[math.random(1, #Hats)]:Clone() -- gets a random hat every time a new player joins
Hat.Parent = StarterCharacter
end)
Summary

This just picks a random hat every time a player joins.

It sort of worked but all the players still have the same hat, with like two of them having a different hat.

Heres a screenshot:

the thing is that it is random for all players so there is not stopping if it picks the same hat for some players but you could just not clone the hat just take it and set its parent to the character as it then wouldn’t be there for it to be put onto another player but then you would have several issues if you where to do that so I wouldn’t recommend that.

@errnell Do you still need help or not?

Yeah, I don’t think it’s possible to limit the amount of accessories it picks, is there?

could you explain what you mean?

Like, is there a way to make it so the script only chooses one, minus what you said about only parenting it without cloning the hat.

There is a way by Inserting the hats into a table and once it has used all the hats then it adds them all back to the table again or you just have enough hats for every player and destroy the original hat.

ill leave this to GoIden now as he is typing anyways.

Okay I might help I just need to clarify the info.

So you want when a player joins the game they get a different hat and nobody else can get it?

Just clarify the info cause I do have a idea…

I think that is his goal but i am not 100% sure.

Okay I just need to clarify this so I can create the system. I have used something like this just a different base

1 Like

Okay, so basically I want it so when a player joins they get one hat of the folder, it doesn’t matter if it’s the same thing but the script chooses only one hat for all players per server. What I’m trying to attain is that every character has different hats and only 1 hat on the character. It’s kind of hard to explain it

Yeah sure give me some minutes. You might get code a bit later I have a test tomorrow so yeah…

I got that done right here…

Ok, it still works but the script puts on like a lot of hats on, I’ll be using this screenshot as reference

Screenshot 2023-12-18 135048

So this is kind of how I want it to look when three players join in, they only have one hat on and they all aren’t really the same, it doesn’t matter if two players have the same hat but you should know what I’m trying to say.

local ServerStorage = game:GetService("ServerStorage")
local StarterPlayer = game:GetService("StarterPlayer")
local StarterCharacter = StarterPlayer:WaitForChild("StarterCharacter")

game.Players.PlayerAdded:Connect(function(plr)
	local Hats = ServerStorage.CharacterAssets:WaitForChild("Hats"):GetChildren()
	local Hat = Hats[math.random(1, #Hats)]
	Hat.Parent = StarterCharacter
end)

If i am right this should work

local Hats = game:GetService(‘ReplicatedStorage’):FindFirstChild(‘CharacterAssets’):FindFirstChild(‘Hats’):GetChildren()

game.Players.PlayerAdded:Connect(function(Player)
if Player then
local Character = Player.Character or Player.CharacterAdded:Wait()

	local randomHat = math.random(1,#Hats)
	local Chosen = Hats[randomHat]:Clone()

	Chosen.Parent = Character:FindFirstChild('Head')
end

end)

Dunnar how 2 put it on the head liek

If there is an issue just say cause I don’t have hats :saluting_face: