title says it all. how to add a hat to a character while following the rules of local script, and server script?
how to add a hat to a character while following the rules of local script, and server script?
1 answer
So a LocalScript can only be used locally for a player. In that case, you can use game.Players.LocalPlayer. Any changes made from a LocalScript will only be seen from the client, not anyone else in the server. If you'd like to clone a hat when a player first joins the game, then you would have to use PlayerAdded event.
game.Players.PlayerAdded:Connect(function(plr) --plr is defining the player
local char = plr.Character --We are now defining the character here.
local hat = game.ServerStorage.Hat:Clone() --Clones the hat from ServerStorage.
hat.Parent = char --Parents it to the character.
end)
This script will run everytime a new player joins. If you want the hat to clone everytime the player respawns, you should create a ServerScript and place it in StarterPlayer > StarterCharacterScripts. Since this script is in StarterCharacterScripts, it will be cloned into the player everytime the player respawns or joined. In this case, you're allowed to use both LocalScripts and ServerScripts. LocalScript changed are still made local because they are scripts only being run for the client.
local char = script.Parent --This script is inside the character, so that would be the parent.
--If you'd like to remove every other hats currently in the character, then add these lines first.
for i,v in pairs(char:GetChildren()) do
if v:IsA("Accessory") then
v:Destroy()
end
end
local hat = game.ServerStorage.Hat:Clone() --Clones hat
hat.Parent = char --Parents it to character.
2 comments
np.
actually i was assuming it would work, for some reason nothing happened. i did what you said. stick it in a normal script, and put it in startercharacter scripts. but it did nothing when i spawned.
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Needs more context...
sorry, i made this question for a certain person to answer who already knew the context of it