Proximity Prompt Hat (Model) Giver

Introduction

Hello! For those who don’t know me I’m NovusRoom, a milsim (military simulation) developer on Roblox. In this tutorial I will be teaching you how to make your own proximity prompt hat giver.

This is what your script will do:

38c8a2035d70b79599fcb1ccb89eae47

What you will need:

The proximity prompt model

a0f39bf37b7c86c4b3b7d86072a57d21

The Hat in SERVERSTORAGE:

ss

The hat does not have to have these exact parts, however it must have the part “Middle” in as that’s the part that connects your hat to your head.

The Code Part 1:

local ServerStorage = game:GetService("ServerStorage")

local Hat = ServerStorage.Hat

local ProximityPrompt = script.Parent.Parent.ProximityPrompt

This defines our initial variables. ServerStorage (where our hat is), and the proximity prompt (what we hold down to give us the hat)

The Code Part 2:

function RemoveHats(Character)
	local d = Character:GetChildren() 
	for i=1, #d do 
		if (d[i].className == "Accessory") then 
			d[i]:remove() 
		end 
	end
end 

function WeldParts(part0,part1)
	local newWeld = Instance.new("Weld")
	newWeld.Part0 = part0
	newWeld.Part1 = part1
	newWeld.C0 = CFrame.new()
	newWeld.C1 = part1.CFrame:toObjectSpace(part0.CFrame)
	newWeld.Parent = part0
end

These are known as functions, they are little sections of script that are written out at the beginning that we can ‘call’ upon at times in our script. The first one removes any accessories (hats) we are currently wearing. If you wish to keep your accessories on then simply remove that function and where is says RemoveHats(Character) around line 27. The second function welds the hat to your head, it creates a weld using Instance.new("Weld"), which welds it to the part 1 which will be our head.

The Code Part 3

ProximityPrompt.Triggered:Connect(function(Player)
		Hat.PrimaryPart = Hat:WaitForChild("Middle")
		
		local Character = Player.Character
		RemoveHats(Character)
		local Head = Character:WaitForChild("Head")
			local NewHat = Hat:Clone()
			NewHat:SetPrimaryPartCFrame(Head.CFrame)
			NewHat.PrimaryPart:Destroy()
			for _, part in pairs (NewHat:GetChildren()) do
				if part:IsA("BasePart") then
					WeldParts(Head, part)
					part.CanCollide = false
					part.Anchored = false
				end
			end
		NewHat.Parent = Character
end)

This final part simply puts the functions into use - finding your hat from serverstorage, cloning it to your head, and making sure it’s not anchored so you don’t just teleport to the hat.

I hope you found this tutorial useful and if you did then I’ve done what I set out to achieve. Have a great rest of your day! :grin:

12 Likes

Thank you so much . It really helped me

1 Like

You should use accessories and Humanoid:AddAccessory() instead of doing weird weld trickery

2 Likes

Thank you very much for sharing this tutorial, i have learned a lot with this! keep doing tutorials! :pray:

Yeah but some people dont know how to convert hat models into accessories and plus you can learn more from something like this such as welding, functions etc.