Change CameraOffset on keybind

Here is my script which will be mentioned below. I am using a module so ignore the module properties.

local module = {}

local cas = game:GetService("ContextActionService")
local tween = game:GetService("TweenService")

local char = game.Players.LocalPlayer.Character
local hum = char:WaitForChild("Humanoid")

local debounce = false

local function offset()
	if not debounce then
		debounce = true
		tween:Create(char:WaitForChild("Humanoid"), TweenInfo.new(0.5), {CameraOffset = Vector3.new(1.75, 0, 0)}):Play()
	else
		debounce = false
		tween:Create(char:WaitForChild("Humanoid"), TweenInfo.new(0.5), {CameraOffset = Vector3.new(0, 0, 0)}):Play()
	end
end

cas:BindAction("offset", offset, true, Enum.KeyCode.X)

return module

I want to change my camera’s offset when I press X. But when I press X, if I don’t hold it, it goes back to being where it was before I pressed X. How do I make it so that I does not go back once I release X unless I press it again? Thanks in advance and here is what it currently does.

robloxapp-20220712-1719477.wmv (591.3 KB)

It could be that you’ll have to check the UserInputState.
Let me know if there’s another issue.

local players = game:GetService("Players")
local contextActionService = game:GetService("ContextActionService")
local tweenService = game:GetService("TweenService")
local player = players.LocalPlayer
local module = {}
local debounce = false
local tweenInformation = TweenInfo.new(.5)

contextActionService:BindAction('offset', function(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		local character = player.Character
		if typeof(character) == 'Instance' then
			local humanoid = character:FindFirstChildWhichIsA("Humanoid") :: Humanoid
			if typeof(humanoid) == 'Instance' then
				if debounce ~= true then
					debounce = true
					tweenService:Create(humanoid, tweenInformation, {
						CameraOffset = Vector3.new(1.75, 0, 0)
					}):Play()
				else
					debounce = false
					tweenService:Create(humanoid, tweenInformation, {
						CameraOffset = Vector3.zero
					}):Play()
				end
			end
		end
	end
end, true, Enum.KeyCode.X)

return module
1 Like

It works PERFECTLY, thank you so much!

1 Like