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

Making a morph script, but the part rotates differently everytime I trigger the script?

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)


1 answer

✓ Accepted answer

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.

10 comments

0

It doesn't rotate anymore, but it positions itself differently now. Depanding on where I touch the part.

0

I have checked the code with random mask from library, it works. Weird. Maybe your HazmatMask model is bad.

0

It doesn't do this till I add to the Vector3 value.

0

I may be overcomplicating things. Is your HazmatMask made of a single part with mesh?

0

No, It's a union, could that be the reason it's not working?

0

Alright, I tested it with a normal part and it works. So I guess it is because of it being a union.

0

Is there a way it could work with a union?

0

Sorry, i don't know. I am not really using the unions. Try creating invisible, non collidable block in the middle and weld the union to it. Then make a union as a child of this part, and CFrame only this part.

0

Smart thinking, I'll try it.

0

Thanks a lot with helping, I did what you did but with a head. So I wouldn't have to change the vector value.

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