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?
01 | script.Parent.Touched:Connect( function (hit) |
02 | local newPart = game.ServerStorage.Hazmat.HazmatMask:Clone() |
03 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
04 | if hit.Parent:FindFirstChild( "HazmatMask" ) then |
05 | else |
06 | newPart.Parent = hit.Parent |
07 | newPart.CFrame = CFrame.new(hit.Parent:FindFirstChild( "Head" ).Position) |
08 | local weld = Instance.new( "ManualWeld" ) |
09 | weld.Part 0 = newPart |
10 | weld.Part 1 = hit.Parent:FindFirstChild( "Head" ) |
11 | weld.C 0 = newPart.CFrame:inverse() * hit.Parent:FindFirstChild( "Head" ).CFrame |
12 | weld.Parent = hit.Parent:FindFirstChild( "Head" ) |
13 | end |
14 | end |
15 | end ) |
Replace:
1 | newPart.CFrame = CFrame.new(hit.Parent:FindFirstChild( "Head" ).Position) |
with:
1 | 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.
1 | newPart.CFrame = (hit.Parent:FindFirstChild( "Head" ).CFrame + Vector 3. new(x,y,z)) * CFrame.Angles( 0 ,math.rad(r), 0 ) |
Also consider adding debounce, to avoid having a multiple masks on one character.