Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make script to remove hats and give you a new one?

Trying to make a script where when a player joins and is on a certain team, their hats they are currently wearing are removed THEN replaced with a new hat. This is what I have so far, I don't know how to insert a hat from the ROBLOX catalog and weld it onto the player's head

local teams = {"British Army"}

function isTeam(plr)
for _,v in pairs(teams) do
if game.Teams[v].TeamColor == plr.TeamColor then
return true
end
end
return false
end

game.Players.PlayerAdded:connect(function(np)
	np.CharacterAdded:connect(function(nc)
		if isTeam(np) then
			nc.ChildAdded:connect(function(item)
				if item:IsA("Accessory") then
					item:Destroy()
				end
			end)
		end
	end)
end)

Thank you

1 answer

You're checking the Player for Accessories being added when they're actually added to the Character. And, instead of doing a ChildAdded event for the Character, I believe that simply doing a for loop after doing a np.CharacterAppearanceLoaded:wait() would have less risk for errors.

For adding hats from the Catalog, use InsertService, but since InsertService always puts the thing requested inside a model, do something like game:GetService("InsertService"):LoadAsset(putTheIdHere):FindFirstChildOfClass("Accessory").Parent = nc

And I noticed that you were using :connect, which is depreciated. Try using :Connect instead.

local teams = {"British Army"}

function isTeam(plr)
for _,v in pairs(teams) do
if game.Teams[v].TeamColor == plr.TeamColor then
return true
end
end
return false
end

game.Players.PlayerAdded:Connect(function(np)
	np.CharacterAdded:Connect(function(nc)
		if isTeam(np) then
			nc.CharacterAppearanceLoaded:wait()
			for _,v in pairs(nc:GetChildren()) do
				if v:IsA("Accessory") then
					v:Destroy()
				end
			end
		end
	end)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.