How to get a parts Current position?

I am working on a adventure game with a scripted camera, the camera has a parts CFrame the part tweens if you press on a click-detector so I want it to tween back when you press on it again but however I don’t know how to get the camera-parts current position before tweeing in.

You could use a variable to store the current/previous CFrame.

How would I get a parts current CFrame?

local partCFrame = part.CFrame

l> ocal tweenservice = game:GetService(“TweenService”)

local ClickDetector = workspace.Telynv.ClickDetector
local cam = workspace.FAQ
local sound = workspace.MouseButton
local guit = workspace.Telynv.BillboardGui.ImageLabel
local TweenStyle = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,1)
local goal = {}
goal.Position = Vector3.new(-132.294, 95.352, 305.48)

local tween = tweenservice:Create(cam, TweenStyle, goal)

ClickDetector.MouseClick:Connect(function()
if
guit.Visible == false then
local currentval = cam.CFrame
local goal = {}
goal.Position = currentval – need to get the current CFrame here , doesnt work
sound:Play()
tween:Play()
wait(0.5)
guit.Visible = true

else
guit.Visible = false
sound:Play()
local tween2 = tweenservice:Create(cam, TweenStyle, goal)
end

end)

It looks like you’re trying to get the camera’s CFrame, try changing the camera line that didn’t work to this:

local currentval = workspace.CurrentCamera.CFrame

Doesnt work , the camera is a part.

I believe it should be as simple as changing it to goal.CFrame = currentval

The problem is that

local currentval = workspace.CurrentCamera.CFrame

gets more than 1 CFrame ı placed a print after currentval and it printed out 2 CFrames.

Sorry, I should’ve made myself clear, since you’re using a part as the camera don’t use the CurrentCamera.CFrame and instead use your original currentval = (part).CFrame, and change the goal to goal.CFrame = currentval (rather than goal.Position = currentval)

Still doesnt work it gives this output when ı try to print the currentval

45.5331497, 552.085876, -2.37557983, -0.986991405, 0.140279517, -0.0785482079, 1.77174807e-05, 0.488658845, 0.872474968, 0.160773635, 0.86112386, -0.482304573

Still the problem is ı cant get the current CFrame it gets more than 1

That’s the full CFrame; the position followed by the 9 entries of the rotation matrix. The position is the first 3 numbers: x=45.5331497, y=552.085876, z=-2.37557983

Clarify with me something really quick.

If guit.Visible == false, do you want it to tween to the object or tween back to the original position of the camera?

If the gui is visible ı want it to tween back to the original place it came from
if it isnt visible ı want it to tween to the object

Alright, I’m not sure if this works, for I can’t test it by myself, but you should try this:

local tweenservice = game:GetService("TweenService")
local TweenStyle = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,1)

local ClickDetector = workspace.Telynv.ClickDetector
local Camera = workspace.CurrentCamera
local OriginalCamPos -- Empty variable which will be used later to define the camera
local guit = workspace.Telynv.BillboardGui.ImageLabel

ClickDetector.MouseClick:Connect(function()
	local Debounce = false -- This will stop the player from spamming the camera
	
	local ObjectPos = Vector3.new(-132.294, 95.352, 305.48) -- If this is not the position you want the camera to go to, feel free to change it
	
	local MouseBtnSound = workspace.MouseButton
	
	if guit.Visible == false and Debounce == false then
		OriginalCamPos = Camera.CFrame -- Gets the current starting CFrame of the camera
		guit.Visible = true
		Debounce = true
		local TweenToObject = tweenservice:Create(Camera, TweenStyle, ObjectPos)
		MouseBtnSound:Play()
		TweenToObject:Play()
		wait()
		-- Put any extra code you want to occur when the player goes to the object
		Debounce = false
	elseif guit.Visible == true and Debounce == false then
		guit.Visible = false
		Debounce = true
		local TweenToCamera = tweenservice:Create(Camera, TweenStyle, OriginalCamPos)
		MouseBtnSound:Play()
		TweenToCamera:Play()
		wait()
		-- Put any extra code you want to occur when the player goes to the camera
		Debounce = false
	end
end)

I created an empty variable called “OriginalCamPos,” which can detect the original camera position before starting the tween, giving the object to the original starting CFrame a vector3 to go to.

If I did it right, this should work.

PS: If that doesn’t work I have another idea

1 Like

Unable to cast dictionary thank you anways for the effort!

That’s what I thought might happen. That’s an error in the Tween itself, not necessarily the script.

What you should do is create two parts, one for the CFrame of the original camera (if you don’t have it already) and one for the CFrame of the camera being hovered over the object.

Then, you should change the variable “StartingCamera” in the script to the path to the part you want to be the starting position of the camera.

Next, change the “HoveringOverObject” variable to the path to the part you want the camera to be at when the player clicks the object.

local tweenservice = game:GetService("TweenService")

local ClickDetector = workspace.Telynv.ClickDetector
local Camera = workspace.CurrentCamera

local StartingCamera = Path.To.Your.Starting.Camera.Pos -- Make sure NOT to add ".CFrame" at the end, that will come later
local HoveringOverObject = Path.To.Your.Camera.Part.When.Player.Clicks -- Make sure NOT to add ".CFrame" at the end, that will come later

local guit = workspace.Telynv.BillboardGui.ImageLabel

ClickDetector.MouseClick:Connect(function()
	local Debouce = false -- This will stop the player from spamming the camera
	
	local MouseBtnSound = workspace.MouseButton
	
	if guit.Visible == false and Debouce == false then
		Debouce = true
		local TweenToObject = tweenservice:Create(Camera, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false,1), {CFrame = HoveringOverObject.CFrame})
		MouseBtnSound:Play()
		TweenToObject:Play()
		wait(1) -- You wait one second because that's how long the tween plays. If you want it longer, then I suggest you change the tween and the wait length
		-- Put any extra code you want to occur when the player goes to the object
		guit.Visible = true
		Debouce = false
	elseif guit.Visible == true and Debouce == false then
		Debouce = true
		guit.Visible = false
		local TweenBackToCam = tweenservice:Create(Camera, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingStyle.In, 0, false,1), {CFrame = StartingCamera.CFrame})
		MouseBtnSound:Play()
		TweenBackToCam:Play()
		wait(1)
		Debouce = false
	end
end)

I just tested this myself and it works fine for me. I hope this can help!

1 Like

Thank you this doesnt solve my problem cause you can move the camera with the wasd but ı think ı can make a script that doesnt move the Y axis and moves with the camera on the other directions.

1 Like