I'm trying to make a brick part of the character appearance, but it rotates differently everytime I trigger the script. How do I get it to not rotate differently everytime I trigger the touch event?
script.Parent.Touched:Connect(function(hit) local newPart = game.ServerStorage.Hazmat.HazmatMask:Clone() if hit.Parent:FindFirstChild("Humanoid") then if hit.Parent:FindFirstChild("HazmatMask") then else newPart.Parent = hit.Parent newPart.CFrame = CFrame.new(hit.Parent:FindFirstChild("Head").Position) local weld = Instance.new("ManualWeld") weld.Part0 = newPart weld.Part1 = hit.Parent:FindFirstChild("Head") weld.C0 = newPart.CFrame:inverse() * hit.Parent:FindFirstChild("Head").CFrame weld.Parent = hit.Parent:FindFirstChild("Head") end end end)
Replace:
newPart.CFrame = CFrame.new(hit.Parent:FindFirstChild("Head").Position)
with:
newPart.CFrame = hit.Parent:FindFirstChild("Head").CFrame
If your part in ScriptStorage is not positioned properly, you may still the get it wrong, but it will be wrong always the same. Consider this code, replacing x,y,z and r with proper values or repositon CFramePrimaryPart in ServerStorage.
newPart.CFrame = (hit.Parent:FindFirstChild("Head").CFrame + Vector3.new(x,y,z)) * CFrame.Angles(0,math.rad(r),0)
Also consider adding debounce, to avoid having a multiple masks on one character.