title says it all. how to add a hat to a character while following the rules of local script, and server script?
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 LocalScript
s and ServerScript
s. 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.