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

How do I make a CUSTOM Hat Giver (non mesh) that gives you a hat on touch?

2 answers

✓ Accepted answer

Ungroup them then seleect all parts of you hat and union them, then insert this script into your hat :-

debounce = true

function onTouched(hit)
	if (hit.Parent:findFirstChild("Humanoid") ~= nil and debounce == true) then
		debounce = false
		h = Instance.new("Hat")
		p = Instance.new("Part")
		h.Name = "Hat"
		p.Parent = h
		p.Position = hit.Parent:findFirstChild("Head").Position
		p.Name = "Handle" 
		p.formFactor = 0
		p.Size = Vector3.new(1.045, 1.045, 1.045) 
		p.BottomSurface = 0 
		p.TopSurface = 0 
		p.Locked = true 
		script.Parent:clone().Parent = p
		h.Parent = hit.Parent
		h.AttachmentForward = Vector3.new(-0, -0, -1)
		h.AttachmentPos = Vector3.new(0, 0.5, -0.2)
		h.AttachmentRight = Vector3.new(1, 0, 0)
		h.AttachmentUp = Vector3.new(0, 1, 0)

		wait(5)
		debounce = true
	end
end

script.Parent.Touched:connect(onTouched)

I am not sure it will work but you can try it

Edit :-

debounce = true

function onTouched(hit)
		if (hit.Parent:findFirstChild("Humanoid") ~= nil and debounce == true) then
			debounce = false
		h = Instance.new("Hat")
		p = Instance.new("Part")
		h.Name = "Hat"
		p.Parent = h
		p.Position = hit.Parent:findFirstChild("Head").Position
		p.Name = "Handle" 
		p.formFactor = 0
		p.Size = Vector3.new(-0,-0,-2) 
		p.BottomSurface = 0 
		p.TopSurface = 0 
		p.Locked = true 
		script.Parent.Mesh:clone().Parent = p
		h.Parent = hit.Parent
		h.AttachmentPos = Vector3.new(0,-0.075,0.2) -- Change this positions to fix your hat position
		wait(5)		debounce = true
	end
end

script.Parent.Touched:connect(onTouched)

Now try?

Edit2 :-

debounce = true

function onTouched(hit)
	if (hit.Parent:findFirstChild("Humanoid") ~= nil and debounce == true) then
		debounce = false
		h = Instance.new("Hat")
		p = script.Parent:Clone()
		h.Name = "Hat"
		p.Parent = h
		p.Position = hit.Parent:findFirstChild("Head").Position
		p.Name = "Handle" 
		p.formFactor = 0
		p.BottomSurface = 0 
		p.TopSurface = 0 
		p.Locked = true 
		h.Parent = hit.Parent

		wait(5)
		debounce = true
	end
end

script.Parent.Touched:connect(onTouched)

This code should work, try again please.

7 comments

0

It doesn't work. It just one part as a hat on my head and it's definitely not the same as it should look.. https://gyazo.com/3936103acf13a3dcd63a4bbba68388c8

0

That didn't fix it, now it doesn't work at all.

0

don't make your hat anchored and try 3rd code

0

"Hat" objects are depreciated now and don't work as they should anymore, which might be why this isnt working as you expect it to.

0

@Xetrax "Instance.new("Hat")" still works, the problem was with the mesh, first 2 codes were desined for hats with mesh, but i tested last one is working. https://gyazo.com/c04a404f5841c9c6f3f28725def22ea9/preview

0

That is only a single part hat, and upon reexamination of your code, it seems you're using the built-in insta-weld of the hat object, which it works, but only for hats like the one you demonstrated that is perfectly centered. I dont think hes trying to union it into one brick and if he did I dont think its centered...

0

"script.Parent:Clone()" it will clone the part it doesn't matter it is union part or normal part or mesh part

I've coded morphs that are similar to this, and you should start with creating a "dummy" head brick at the position where you want your head to be with the hat on, group and anchor all bricks.

Next, use the PrimaryPart function to set that dummy head as the primarypart, just makes life easier with things.

Then, insert this at the beginning of your code, after you put all your variables ("local"s, etc.):

local Button = script.Parent;
local Hat = Button.Parent:FindFirstChild("HatGroup")	--Checking if the button is inside a group with the Hat parts.
if #Hat :GetChildren()==0 then warn("Hat is empty.") return end	--If the hat has no parts, then technically there is no hat.
if Hat.PrimaryPart==nil then warn("No PrimaryPart set.") return end	--If you didn't set the dummy head as the primarypart of the hat model.

if Hat :IsA("Model") then	--Basically, :IsA is a shortened version of .ClassName, except it also works for anything thats a subclass of the class.
	for Indx2, Prt in pairs(Hat:GetChildren()) do --For each part inside the hat, do:
		if Prt:IsA("BasePart") and Prt~=Hat.PrimaryPart then	--[ BasePart is the overall "Part" class, it contains things like meshpart, unions, wedges, etc.
			if Prt.ClassName~="Seat" and Prt.ClassName~="VehicleSeat" then	--Welding seats can cause annoying glitches so just dont.
				local Wld = Instance.new("Weld", Hat.PrimaryPart)	--Creates a new weld inside the DummyHead.
				Wld.Part0 = Wld.Parent	--Sets Part0 of the weld as the dummyhead
				Wld.Part1 = Prt --Weld the part of the hat to the dummyhead
				local CJ = CFrame.new(Wld.Part0.CFrame.p)
				Wld.C0 = Wld.Part0.CFrame:inverse()*CJ
				Wld.C1 = Prt.CFrame:inverse()*CJ
				Wld.Name = Wld.Part0.Name..":"..Wld.Part1.Name	--Naming the weld if you ever wanted to reference it or manipulate it later
			end
			Prt.Anchored = false --We dont want these parts anchored anymore, just the Dummyhead.
			Prt.CanCollide = false --Also dont want them to collide, this restricts the camera of the player.
			Prt.Locked = true --Makes stealing/copying the hat (and other parts) harder to do.
		end
	end
end

The above code is just a recursive weld, it will weld all the bricks in the hat, as they are, to the dummy head.

And follow it with something like this, a little sophisticated, but it has a lot of safeguards in place and works perfectly for every morph I have made:

local Players = game:GetService("Players");	--Get all Players in the game.
local Changing = false; --Same as "debounce", just an on/off value.

Button.Touched:Connect(function(Obj)
	local Parent = (Obj.Parent:IsA("Model") and Obj.Parent) or 
		(Obj.Parent.Parent:IsA("Model") and Obj.Parent.Parent) or nil
	if Parent~=nil and not Changing then
		Changing = true
		local Player = Players:GetPlayerFromCharacter(Parent)
		if Player~=nil then
			local Body = Player.Character;
			if Body:FindFirstChild("Humanoid") and Body:FindFirstChild("Head") and not Body:FindFirstChild(Hat.Name) then
				local Model = Hat:Clone()	--Copy the hat to put it into the player
				
				local Wld = Instance.new("Weld", Body.Head) --Make a weld and parent it to the player's head.
				Wld.Part0 = Wld.Parent
				Wld.Part1 = Model.PrimaryPart --Part1 is the cloned Dummyhead of the cloned hat model.
				Wld.Name = Wld.Part0.Name..":"..Wld.Part1.Name
				Wld.C0 = CFrame.new() --This is just a bunch of zeroes and normal rotation, as long as you placed the dummyhead exactly as you want, this makes the hat stay as it is.
			
				Model.PrimaryPart.Transparency = 1 --Makes the dummyhead transparent.
				Model.PrimaryPart.Anchored = false --Unanchors it so the hat model is actually on the player.
				Model.PrimaryPart.CanCollide = false --Dummyhead should also be non-cancollide now.
				Model.PrimaryPart.Locked = true
			
				Model.Parent = Body --And lastly, parent the new cloned model to the body so its not floating in "nil-space" (When you clone or make new parts, they dont exist in the workspace unless you set their parent property to something in the workspace.
			end
		end
		wait(1)
		Changing = false --Reset the boolean so someone can use this again.
	end
end

Alrighty-then, should work pretty well, if it doesn't thats probably because I coded it mostly from scratch and memory Lol. Comment and I'll look over it a few times and test it myself. If you want anything in it explained a bit more, specify the line numbers and I'll add comments to them.

Cheers, -Xetrax

Heres an example model: https://www.roblox.com/library/1370450282/Hat-Morph-Example

7 comments

0

Ah, forgot to mention, Group the hat so that you have a invisible non-cancollide box surrounding it, called "Button" or w/e, and have that outside the buttons group, so like, group the hats parts with the button.

0

May I please be told a bit more like "beginner" like? Sorry. I'm a really big beginner at scripting and it's probably dumb for me not to know this stuff.

0

Does the script go in the dummy head?

0

No, place the script in the transparent box/part around the hat parts(including the dummy head), but dont group the transparent box with the hat parts and head. Group the hat parts and dummy head into one model, setting the primarypart of that model as the dummy head, then group that model with the transparent button and put the script inside the button.

0

Well, I added a bunch of explanatory comments there, tell me if you need any more help, I can make you an example model if you need.

0

An example model would be very helpful. Thanks.

0

Added it into my answer, its free.

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