Need help with scripting a script that makes hat model wearable

Hi guys!

recently, i was learning how to script.
I stopped on one important script for my game. i dont know how to make a script that let npc or player
wear team based model as a hat.

please help.

1 Like

Assuming you already have teams set up, you could place a hat object inside each team, and then clone it into the player whenever their character spawns.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
  player.CharacterAppearanceLoaded:Connect(function(character)
    local hat = player.Team:FindFirstChildOfClass("Hat") or player.Team:FindFirstChildOfClass("Accessory")
    if hat then
      hat:Clone().Parent = character
    end
  end)
end)
2 Likes

I would do what @GnomeCode did, just putting the hat in a folder inside of ServerStorage, also naming the hat the same as the team.

local Players = game:GetService("Players")
local serverStorage = game:GetService("ServerStorage")

local hatFolder = serverStorage:WaitForChild("TeamHats")

Players.PlayerAdded:Connect(function(player)    
    player.CharacterAdded:Connect(function(character)
        hatsFolder:FindFirstChild(player.Team.Name):Clone().Parent = character
    end)
end)

Either way works just fine. It just depends on how you want to organize it.

I personally like to stay very organized. I organize everything into subflolders, which are children of one single folder. I do this in both ReplicatedStorage and ServerStorage. The main folder is named “MainGame”, and all of the subfolders are named after their contents. Such as a folder named “Remotes” under the ReplicatedStorage MainGame folder. That would hold all of the RemoteEvents, RemoteFunctions, BindableEvents and BindableFunctions.
An example for a subfolder in the ServerStorage would be what I did in the code, a folder for all of the team hats. Another would be a folder for all of the maps in your game, or monsters that will spawn