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:
What you will need:
The proximity prompt model
The Hat in SERVERSTORAGE:
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.