I'm trying to remove a players hat then give them a new hat. Removal script:
function newPlayer(player)
wait(1)
p = player.Character:GetChildren()
for i=1, #p do
if p[i].className == "Hat" then
p[i]:Remove()
end
end
end
game.Players.ChildAdded:connect(newPlayer)
Giving script:
game.Players.PlayerAdded:connect(function(player)
repeat wait()until player.Character
local hatId=1045408
local hat = game:service("InsertService"):LoadAsset(hatId):GetChildren()[1]
hat.Parent=player.Character
end)
All I want to know is how to combine them into one script and make it more efficient; and work.

4 comments
This error came up: 20:42:51.595 - Service is not a valid member of DataModel
Edited
The removal side of the script doesn't work.
efficient is the wrong word here--destroy isn't any more efficient, it just is less likely to bug because it isn't deprecated like destroy