CFrame help - variable grows exponentially & resets

Hey, so I’ve been tinkering with using variables with my part, so it moves 1 stud each (ex. 1 second) whenever I hold down a certain key, but the part starts increasing every value, even though I only change 1.

I’m mainly looking for (2) answers here, how to fix my part resetting to its original value/spot, and how to fix the parts value(s) increasing exponentially, I only want 1 value to increase by 1.

Script:

wait(2)

local InputService = game:GetService("UserInputService")
local spot = game.Workspace.poerge
local KeyDown = false
local Yvalue = 0

InputService.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		KeyDown = false
	end
end)

InputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		KeyDown = true

		repeat
			wait(0.5)
			print("I thinks it work")
			Yvalue += 1
			game.Workspace.poerge.CFrame += Vector3.new(Yvalue, spot.Position.Y, spot.Position.Z)
			-- function here 
		until KeyDown == false
	end
end)

Video:

Note: The part will reset even if/when I am still holding E, it has no contribution to it. The part will only stop where it is if I let go of E.

If you want the part to reset back to it’s original position, you have to store the original position somewhere.

This can be in your code, or in a Vector3Value object.

local originalPosition = Vector3.new(0, 0, 0) -- Variable in your code
local originalPosition2 = workspace.OriginalPosition -- Vector3Value somewhere in your game.

I would recommend using a Vector3Value if you’re going to use the position in another script. Otherwise stick to a variable.

When the repeat breaks after KeyDown is false, then I would set the parts’ position from there.

-- Below the repeat loop,

workspace.poerge.Position = originalPosition

Also, there is a difference between CFrame and Vector3,

If you’re going to manipulate the parts’ Orientation and Position, then use CFrame. It alters position and orientation.

If you’re going to manipulate ONLY the parts’ Position, then just use Vector3:

workspace.poerge.Position += Vector3.new(0, Yvalue, 0) -- Since you're adding two Vector3 values, ROBLOX will add 0 to the other two axes, which does nothing.

^ I’m assuming you want the part to go up since the variable says Yvalue, so put it in the Y parameter.

This may be quite a bit of information to absorb, but I hope this helps.

1 Like

The OP doesn’t want the part to reset its position. They mention it here:

Edit: Your other parts are valid though

2 Likes

Whoops… my bad!
He should still have a secondary variable, but to just record the parts’ position in space.

local currPosition


-- In the repeat loop AFTER the parts' position has been changed,
currPosition = workspace.poerge.Position

-- When the player releases E, set the parts' position to the currPosition.
InputService.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		KeyDown = false
        workspace.poerge.Position = currPosition
	end
end)

I hope I answered their second question though.

Hey! I think the script works more smoothly now, but it keeps resetting to 0, 0, 0 (I may have placed the position variable in the wrong spot) and the part continues to move exponentially. It’s something to do with the value, as the position was -6, then it went to -5, -3, 0, 3, 5, 8, 14, 21, 29, etc.
The Yvalue is still increasing by 1, but the position isn’t.

wait(2)

Code if it matters

local originalPosition = Vector3.new(0, 0, 0) -- Variable in your code
local originalPosition2 = workspace.OriginalPosition -- Vector3Value somewhere in your game.
local InputService = game:GetService("UserInputService")
local spot = game.Workspace.poerge
local KeyDown = false
local Yvalue = 0
local currPosition

InputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		KeyDown = true

		repeat
			wait(0.5)
			print("I thinks it work")
			Yvalue += 1
			workspace.poerge.Position += Vector3.new(Yvalue, 0, 0) -- Since you're adding two Vector3 values, ROBLOX will add 0 to the other two axes, which does nothing.
			-- function here 
			currPosition = workspace.poerge.Position
			print(Yvalue)
		until KeyDown == false
		workspace.poerge.Position = originalPosition
	end
end)


InputService.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		KeyDown = false
		workspace.poerge.Position = currPosition
	end
end)

It’s because your originalPosition variable is set to Vector3.new(0, 0, 0).

Set the Vector3 to somewhere you want the part to be.

Also, you don’t need originalPosition2 if you’re not going to use a Vector3Value.

Use task.wait() for a better wait time. Don’t use wait().