Attempting to get Position relative from a Hat's "AccessoryWeld"

hello my issue is a reoccuring thing where I want to get the new position of a hats handles after it has been repositioned relative to the “AccessoryWeld” which sits depending on what the accessory is. I’ve come to an abrupt stop in my script to where I reposition it and it starts to move sporadically and not to my desired direction.

tldr: hat moves to random direction after reposition, want to move relative to AccessoryWeld

AdornmentHandles.MouseButton1Up:Connect(function()
	
	local A = HatHandle.AccessoryWeld.C0:ToObjectSpace(HatHandle.CFrame)
	warn("Equation A = " .. A.X, A.Y, A.Y)
	
	local X = A.X
	local Y = A.Y
	local Z = A.Z
	
	local B = HatHandle.AccessoryWeld.C0:ToObjectSpace(A)
	warn("Equation B = " .. B.X, B.Y, B.Z)
	
	local NewX = B.X - origin.X
	local NewY = B.Y - origin.Y
	local NewZ = B.Z - origin.Z
	
	local NewPosition = CFrame.new(NewX, NewY, NewZ)
	
	print("The added position is now " .. NewPosition.Position.X, NewPosition.Position.Y, NewPosition.Position.z)
	
	warn("Position from equation A = " .. X,Y,Z)
	warn("Position from equation B = " .. NewX, NewY, NewZ)
	
	
	
	ReplicatedStorage.Customization.PositionCheck:FireServer(NewPosition, Hat)
	
	HatHandle.AccessoryWeld.Enabled = true
	HatHandle.Anchored = false	
	
	X = 0
	Y = 0
	Z = 0
	
	NewX = 0
	NewY = 0
	NewZ = 0
	
	NewPosition = nil
	B = nil
	A = nil

end)

You can use RobloxAPI:GetRelativeAttachmentCFrame(Attachment) for your problem.

1 Like

I’ve never heard of such API, is there a Documentation for it?

Yes. I currently cannot send links right now, sorry.

1 Like

Wouldn’t it be easier just find the weld’s target C0 from the hat’s CFrame relative to the head?

Where weld is your AccessoryWeld, and handle is your accessory’s handle,

weld.C0 = ((weld.Part1.CFrame * weld.C1):Inverse() * handle.CFrame):Inverse()

also on a side note the person who replied to you above posts nothing but nonsensical ai generated responses, none of the apis they suggest actually exist, so it’s best to ignore them

1 Like

Where would I use this in my script?

I’d calculate the C0 on the client, then use your remote to send it to the server, and on the server set the weld’s C0 using the C0 sent from the client, ie

local targetC0 = ((weld.Part1.CFrame * weld.C1):Inverse() * handle.CFrame):Inverse()

ReplicatedStorage.Customization.PositionCheck:FireServer(targetC0, Hat)

on server:

remote.OnServerEvent:Connect(function(player, c0, hat)
    local weld = hat:FindFirstChild('AccessoryWeld')
    
    if weld then
        weld.C0 = c0
    end
end
1 Like

Thank you it works, however it seems the reset the axis I last editted it on, and what I mean that is, if I edit the X axis, its fine, however when I edit the Z axis, the X axis is resetted to 0, and the Z axis works fine.

would I have to get each individual position (X, Y, Z) then send it through the remote for it to work?


also here’s a video of the system, theres a few mishaps like what I stated above, but also some of the hats go opposite directions of where I desire it to go, and such more.

It looks like an issue with how you’re handling the draggers, can you share how you’re doing that?

1 Like
AdornmentHandles.MouseDrag:Connect(function(a, b)
	
	HatHandle.AccessoryWeld.Enabled = false
	HatHandle.Anchored = true

	warn("Now editting, " .. HatHandle.Parent.Name)
	warn("Supposed to be editting, " .. WhatIsBeingEditted.Name)

	if a.Value == 5 then
		HatHandle.CFrame = origin + HatHandle.CFrame.LookVector * b
		
	elseif a.Value == 2 then
		HatHandle.CFrame = origin + HatHandle.CFrame.LookVector * -b
		
	elseif a.Value == 1 then
		HatHandle.CFrame = origin + HatHandle.CFrame.UpVector * b
		
	elseif a.Value == 4 then
		HatHandle.CFrame = origin + HatHandle.CFrame.UpVector * -b
		
	elseif a.Value == 0 then
		HatHandle.CFrame = origin + HatHandle.CFrame.RightVector * b
		
	elseif a.Value == 3 then
		HatHandle.CFrame = origin + HatHandle.CFrame.RightVector * -b
		
	end
end)

When are you updating origin? It should be updated when handles.MouseButton1Down first fires

1 Like

What do you mean? If you mean something along the lines of like:

origin = HatHandle.CFrame

then I’ll assume I never have.

Yeah, you’d want to do that when the MouseButton1Down event fires so you know which CFrame to translate from

1 Like

Should I update it before or after I fire the remote event?

You’d do it on mouse button down (AdornmentHandles.MouseButton1Down:Connect(...), not up, so it should be an entirely separate connection from the one where you’re firing the remote

1 Like

Ah I See i’ll do that right now and return what happened.

1 Like

Thank you very much it works now, this is a problem I was strugglign with the past 12 hours lmao

1 Like

However though, how cna I stop that jitterness that happens when I move the hat.

1 Like