Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a helmet weld itself to a player when bought?

Hey so I am trying make a fantasy armour shop. I was able to get clothing, weapons etc working but I am having issues with how to get the helmet working?

I essentially want it to be so that when the player purchases the helmet it gets put on their head and any other accessories on their head get deleted.

Below is my current code.

local Cash = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Money.Credits
local mcost = script.Parent.Cost.Value
local mname = script.Parent.Itemname.Value

function onClick()
	if Cash.Value >= mcost then
		Cash.Value = Cash.Value - mcost
			local hat = game.ReplicatedStorage.Helmet:Clone()
	end
end

script.Parent.MouseButton1Click:connect(onClick)

1 answer

✓ Accepted answer

So a few things, first of all, I see nowhere where you are defining that the parent of the hat you defined, so you would have to put the character as the parent.

local hat = game.ReplicatedStorage.Helmet:Clone()
local Character = player.Character
hat.Parent = Character

You would need to define the player yourself, which I believe you should be able to do easily given your script.

I made something like this before too, and a problem I had was that the hat would just fall off the player, to fix this, I had to add in a "Weld" from the hat to the Characters head, so I will provide that code if needed.

local w = Instance.new("Weld", Character)
w.Part0 = hat
w.Part1 = Character.Head

If you still are having problems with the rotation/position of the hat, you would have to edit the accessory properties listed here:

AttachmentForward AttachmentPos AttachmentRight AttachmentUp

For the hat remover, I would use Index and Values like this:

local d = Character:GetChildren()
for i = 1, #d do
	if d[i]:IsA("Accessory") then
		d[i].Handle.Transparency = 0

I would recommend using Transparency in case you want to remove the custom hat later, but otherwise just change the script to destroy the hats instead.

Please PM me or Comment on this if you have any questions!

Using all my suggestions, the code would look like this:

local Cash = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Money.Credits
local mcost = script.Parent.Cost.Value
local mname = script.Parent.Itemname.Value

function onClick()
    if Cash.Value >= mcost then
        Cash.Value = Cash.Value - mcost
            local hat = game.ReplicatedStorage.Helmet:Clone()
	    local hat = game.ReplicatedStorage.Helmet:Clone()
	    local Character = player.Character
	    hat.Parent = Character
	    local w = Instance.new("Weld", Character)
	    w.Part0 = hat
            w.Part1 = Character.Head
	    local d = Character:GetChildren()
	    for i = 1, #d do
		    if d[i]:IsA("Accessory") then
			    d[i].Handle.Transparency = 0
    end
end

script.Parent.MouseButton1Click:connect(onClick)

This discussion is preserved as it was. New answers, comments, and votes are closed.