You shouldn't to use the CharacterAdded event in a LocalScript, given that they automatically reload along with the character...(when in the right services (PlayerGui..)).
Client
local player = game.Players.LocalPlayer
local character = player.Character
if not (character or character.Parent) then
character = player.CharacterAdded:wait()
end
wait(1)
for _, child in pairs(character:GetChildren()) do
if child:IsA("Hat") or child:IsA("Accoutrement") then
child:Destroy()
end
end
for _, child in pairs(playerGui:GetChildren()) do
if child:IsA("Hat") then
v.Parent = character
end
end
Server
I'd recommend -- storing the hats in "ServerStorage",
and cloning them to the character whenever they spawned instead of keeping them
locally.
Pretty much works on the same logic, but makes use of the CharacterAdded and PlayerAdded
events.
local function Refreshed(player)
local character = player.Character
local service = game.ServerStorage
for _, child in pairs(character:GetChildren()) do
if child:IsA("Hat") or child:IsA("Accoutrement") then
child:Destroy()
end
end
for _, child in pairs(service:GetChildren()) do
if child:IsA("Hat") or child:IsA("Accoutrement") then
v.Parent = character
end
end
end
game.Players.PlayerAdded:connect(function(player)
player.Changed:connect(function(property)
if (property == "Character") then
wait(1)
Refreshed(player)
end
end)
end)
2 comments
Make sure it is running from a LocalScript because LocalPlayer can only be accessed from the client.
Thanks that was the problem!