Player to part position

Is there a way to treat a part as a origin position then check where the player is in comparison to the part.

Like the part being 0,0 for position and the player would be a different number depending on the part instead of the world.

Sorry if its explained poorly, I don’t really know how to formulate it that well.

Yeah, what you’re looking for is translating a position from world space to object space and vice versa.

Guide:

Docs:

Yes, it is possible to treat a part as an origin position and then check where the player is in comparison to the part. To do this, you can use the “CFrame” property of a part and the “Character” and “HumanoidRootPart” properties of a player. Then, you can calculate the player’s relative position to the part using the subtraction operation of “CFrames”.

Here is an example code:

local part = script.Parent
local player = game.Players.LocalPlayer

local relativePosition = player.Character.HumanoidRootPart.CFrame - part.CFrame
print(relativePosition)

This code takes the position of the part (part) as the origin and then calculates the relative position of the player’s HumanoidRootPart (player.Character.HumanoidRootPart) in comparison to the part

Besides the fact that I’m 99% sure your response is from chatgpt, CFrames can’t be subtracted from one another

2 Likes

no lol, I’m just a novice programmer

Sorry to bother you, but I’ve just used ToObjectSpace and can’t make heads or tails out of the results that are printed, can you help me?
image

That’s a CFrame which contains both positional and rotational data (rotation is made of 3 directional vectors which make up a matrix). Think what you’d need from that is the position (so instead of print(cf), do print(cf.Position), or just read the first 3 numbers)

You don’t really need to know about matrices and stuff to work with CFrames, you can get the rotation from a CFrame by using CFrame:ToOrientation to get each axis of rotation in radians (which can be converted to degrees with math.deg, then back to radians through math.rad)

Also if you only need the position from the translated CFrame, it might be more performant to use CFrame:PointToObjectSpace() since it doesn’t have to take into account the “goal” result’s orientation but I might be wrong.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.