Hat giver touching a part

Hello, I am making a hat giver when I touch a part, the hat appears on the player’s head and I get the following error:

image

image

script:

local hat = game.ServerStorage.Helmet
local part = script.Parent

function giver (character)
	local humanoid = character:WaitForChild("Humanoid")
	
	local Accessory = Instance.new("Accessory")
	Accessory.Name = "AccesoryHat"
	
	local handle = hat:Clone()
	handle.Name = "Handle"
	handle.Size = Vector3.new(1,1,1)
	handle.Parent = Accessory
	
	local hatAttachment = Instance.new("Attachment")
	hatAttachment.Name = "hatAttachment"
	hatAttachment.Position = Vector3.new(0,2,0)
	hatAttachment.Parent = handle
	
	humanoid:AddAccessory(Accessory)
end

script.Parent.Touched:Connect(function()
	giver()
end)

On the last line, do this instead:

script.Parent.Touched:Connect(giver)

Also, add these two lines right before the one with the WaitForChild?

character = character.Parent
if not game:GetService(“Players”):GetPlayerFromCharacter(character) then return end

It removed the error, but I need the player to put the hat on, I don’t understand why they don’t put it on

The Touched Event has 1 parameter, which is the part that it touched (You can’t refer to the Player’s Character in this instance) I don’t know why you’re spliting it up into 2 different functions

If you wanna get the Player’s Character, you can do Hit.Parent like so:

local Hat = game.ServerStorage.Helmet
local Part = script.Parent

Part.Touched:Connect(function(Hit) --Hit is referring to the Part that it touched (Right Arm, Left Leg, Random Part, etc.)
    local Humanoid = Hit.Parent:FindFirstChild("Humanoid") --Hit.Parent is now referring to the Character, and we're checking for a Humanoid is valid
    if Humanoid then
        local Accessory = Instance.new("Accessory")
        Accessory.Name = "AccesoryHat"

        local Handle = Hat:Clone()
        Handle.Name = "Handle"
        Handle.Size = Vector3.new(1,1,1)
        Handle.Parent = Accessory

        local hatAttachment = Instance.new("Attachment")
        hatAttachment.Name = "hatAttachment"
        hatAttachment.Position = Vector3.new(0,2,0)
        hatAttachment.Parent = Handle

        Humanoid:AddAccessory(Accessory)
    end
end)

It works but it does not appear in the player’s head and too many copies of the helmet are created, what is wrong? :frowning:

Is there a Mesh attached to the Hat variable? (Also the Handle I’m guessing) Also you’d need a debounce if you want the script to only fire once after a period of seconds

You could check if the character already has the accessory and break out of the function before a new copy of the accessory is created. This will stop the script from giving multiple copies of the accessory to the same character.

local Hat = game.ServerStorage.Helmet
local Part = script.Parent

Part.Touched:Connect(function(Hit) --Hit is referring to the Part that it touched (Right Arm, Left Leg, Random Part, etc.)
    local Humanoid = Hit.Parent:FindFirstChild("Humanoid") --Hit.Parent is now referring to the Character, and we're checking for a Humanoid is valid
    if Humanoid then
        if Humanoid.Parent:FindFirstChild("AccesoryHat") then return end --breaks out of the function if the character already contains an "AccesoryHat"

        local Accessory = Instance.new("Accessory")
        Accessory.Name = "AccesoryHat"

        local Handle = Hat:Clone()
        Handle.Name = "Handle"
        Handle.Size = Vector3.new(1,1,1)
        Handle.Parent = Accessory

        local hatAttachment = Instance.new("Attachment")
        hatAttachment.Name = "hatAttachment"
        hatAttachment.Position = Vector3.new(0,2,0)
        hatAttachment.Parent = Handle

        Humanoid:AddAccessory(Accessory)
    end
end)
1 Like

try this one

local hat = game.ServerStorage.Helmet
script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		if not hit.Parent:FindFirstChild("Helmet") then
			hat:Clone().Parent = hit.Parent
		end
	end
end)
2 Likes
1 Like