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

How to make hat remover for a certain team?

I want to make a script that gives you a hat if you join a certain team e.g. the Security Department. I have put the script in game.ServerScriptService. When you switch to the Security Team, it clones the hat into your character however it does not get rid of the characters current accessories e.g. their hair, glasses, backpacks etc...

Here is my code:

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		if plr.TeamColor == game.Teams["Security Dept."].TeamColor then
			for i,v in pairs(char:GetChildren()) do --this is the hat remover part
				if v.ClassName == "Accessory" then
					v:Destroy()
				end
			end
			-- this is the hat giver below
			game.ServerStorage.SecurityHat:Clone().Parent = char
		end
	end)
end)

Should I put the script in a different place or make it a local script or is there something wrong with the hat giver?

2 answers

✓ Accepted answer

You have to use the AddAccessory function of the humanoid. Also, I made it so the script uses :GetService() and :IsA(). :IsA() checks if an instance's class-name is equal to the string argument. Try this:

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        if plr.TeamColor == game:GetService("Teams")["Security Dept."].TeamColor then
            for i,v in pairs(char.Humanoid:GetAccessories()) do 
		v:Destroy()
        end
char.Humanoid:AddAccessory(game:GetService("ServerStorage").SecurityHat:Clone())
    end)
end)

I ALSO made the in pairs loop loop through the accessories by using the Humanoid:GetAccessories() function.

1 comment

0

Hmm, it gives me the hat but it doesn't remove my hair and glasses.

I believe I solved it.

I had to do this script:

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        if plr.TeamColor == game:GetService("Teams")["Security Dept."].TeamColor then
			char.Humanoid:RemoveAccessories()
			game.ServerStorage.SecurityHat:Clone().Parent = char
		end
    end)
end)

However, I would still like to thank youtubemasterWOW as they did help to make this script work!

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