Hat equip / unequip using UserInputService

So basically I’m trying to make a Hat equip or unequip when “g” is pressed on the keyboard.
The Hat won’t stay on the player, and falls through the baseplate. I’m pretty certain I’ve made the hat properly because when I add it to StarterCharacter, it sticks.
I’ve spent weeks looking up how to do this. I’ve read multiple forums on many different websites. I can’t find a working solution anywhere, even though I can clearly see this is a problem plagued by many.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local hat = ReplicatedStorage:WaitForChild("mask")

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.G then
		local player = game.Players.LocalPlayer
		local character = player.Character
		print("G was pressed")

		if not character then
			return
		end

		local humanoid = character:FindFirstChild("Humanoid")

		if not humanoid then
			return
		end

		if hat.Parent == character then
			hat.Parent = ReplicatedStorage -- Unequip
		else
			hat.Parent = character -- Equip
		end
	end
end)

Any help would be greatly appreciated. I’ve been trying to get this to work for over a month.

2 Likes

is the hat an accessory? it will not equip onto a humanoid if its class is not an accessory.

Yes it is. I have tried using addAccessory but to no avail :frowning:

may I see the accessory in the explorer?

Of course. is everything OK?

hatexplorer

when you find the hat, please remember to alyways clone it or else no other player can equip it, maybe that is the issue.

It isn’t, I’ve tried that too. I’ll show another script I’ve tried before but with the same issue.

local player = game.Players.LocalPlayer

local mask = game.ReplicatedStorage.Mask

local uis = game:GetService(“UserInputService”)

uis.InputBegan:Connect(function(input)

if input.KeyCode == Enum.KeyCode.G then



	local clone = mask:Clone()

	clone.Parent = player.Character

	

	print("Complete")

end

end)

does the console say anything
hey there would you ever wonder 30 text limit

By console you mean output? If so, no, just the print statements. Is there a 30 text limit?

yeah the output, yes there is a 30 text limit its to avoid short word spamming.

Probably a good thing so in that case :laughing:

You are doing this client sided but you should do it server sided, by using a RemoteEvent in Replicated storage:

I added a RemoteEvent in ReplicatedStorage called “RemoteEvent”

A Local Script in StarterCharacterScripts that will fire the RemoteEvent, in order Server Add or Destroy the Accessory

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.G then
		local player = game.Players.LocalPlayer
		local character = player.Character
		print("G was pressed")

		if not character then
			return
		end

		local humanoid = character:WaitForChild("Humanoid")

		if not humanoid then
			return
		end
		
		ReplicatedStorage:WaitForChild("RemoteEvent"):FireServer()
	end
end)

A Server Script in ServerScriptService listening the RemoteEvent when player press G key to add or remove the Accessory called “mask”:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local mask = game.ServerStorage:WaitForChild("mask")

ReplicatedStorage:WaitForChild("RemoteEvent").OnServerEvent:Connect(function(player)
	if player.Character:FindFirstChild("mask") then
		player.Character:FindFirstChild("mask"):Destroy()
	else
		local newHat = mask:Clone()
		player.Character.Humanoid:AddAccessory(newHat)
	end
end)
1 Like

I bet it works, Im using your script edited with the approach I did mention, in this video Im pressing G key to add and remove the hat.
I know the hat is beautiful… Its just a quick accessory made with a part :v

1 Like

Thank you so much! This drove me insane because I thought it would be pretty simple, even as a novice to understand. Now I continue working on other aspects of my game. Thanks again! Have a great day!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.