So basically, I have been making a non mesh hat, I have grouped it, but I do not know how to make it so that if you touch the hat, it will appear on your char. Help would be appreciated. https://gyazo.com/57139b1282ef3da3e7a21555851caa1e
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.
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