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.
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)
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
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!