Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to add a hat to a character while following the rules of local script, and server script?

Asked by
wookey12 174
6 years ago

title says it all. how to add a hat to a character while following the rules of local script, and server script?

0
Needs more context... Sekant 20 — 6y
0
sorry, i made this question for a certain person to answer who already knew the context of it wookey12 174 — 6y

1 answer

Log in to vote
0
Answered by
Mayk728 855 Moderation Voter
6 years ago

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.
0
np. Mayk728 855 — 6y
0
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. wookey12 174 — 6y
Ad

Answer this question