Why can't a variable be used in a tweens position?

Hiya! I’m currently working on making a smooth camera with multiple different tweens that allow it to flow nicely with the player’s movement! However, because of the camera being pushed forward, it clips through walls. I’ve already tried other ways of fixing this issue like just offsetting the Vector3 value and just generally messing with the code. But now I’m trying to make the Z value a custom variable which can easily be switched out so it can be pushed back and forward. However, while the value of the variable changes accordingly (I can see it by using a print script) it doesn’t actually move the Z axis anymore in any of the tweens. How can I fix this?

Here is the entire code for this script I’m using)

--Variables--
local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local TweenService = game:GetService("TweenService")

-- Tweens
local AgainstWall = 0
local AwayWall = -0.9
local CamZ = AwayWall

local NormCam = Vector3.new(0, 0, CamZ) -- -0.9
local CrouchCam = Vector3.new(0, -1.5, CamZ) -- -1.25
local MovingNormCam = Vector3.new(0, 0, CamZ) -- -1.05
local RunningNormCam = Vector3.new(0, 0, CamZ) -- -1.2
local MovingCrouchCam = Vector3.new(0, -1.5, CamZ) -- 1.4
local CameraPosition = Humanoid.CameraOffset

local IntoRunTweeninfo = TweenInfo.new(0.25, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local OutRunTweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local CrouchTweenInfo = TweenInfo.new(0.45, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local MovementChangeTweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
local IdleTweenInfo = TweenInfo.new(0.25, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)

local Camera = workspace.CurrentCamera
local CrouchCamPosition = Camera.CFrame * CrouchCam

local DefaultFoV = 85
local IntroRunProperties = {FieldOfView = DefaultFoV + 4.5}
local OutOfRunProperties = {FieldOfView = DefaultFoV - 4.5}

local CrouchIntoProperties = {CameraOffset = CrouchCam}
local CrouchOutOfProperties = {CameraOffset = NormCam}

local IntoCrouchWalkProperties = {CameraOffset = MovingCrouchCam}
local IntoWalkProperties = {CameraOffset = MovingNormCam}
local IntoRunMovingProperties = {CameraOffset = RunningNormCam}
local IdleProperties = {CameraOffset = NormCam}

local RunTween = TweenService:Create(game.Workspace.CurrentCamera, IntoRunTweeninfo, IntroRunProperties)
local OutOfRunTween = TweenService:Create(game.Workspace.CurrentCamera, OutRunTweenInfo, OutOfRunProperties)

local IntoCrouchWalkTween = TweenService:Create(Humanoid, MovementChangeTweenInfo, IntoCrouchWalkProperties)
local IntoWalkTweenTween = TweenService:Create(Humanoid, MovementChangeTweenInfo, IntoWalkProperties)
local IntoRunMovingTween = TweenService:Create(Humanoid, MovementChangeTweenInfo, IntoRunMovingProperties)
local IdleTween = TweenService:Create(Humanoid, IdleTweenInfo, IdleProperties)

local IntoCrouchTween = TweenService:Create(Humanoid, CrouchTweenInfo, CrouchIntoProperties)
local OutCrouchTween = TweenService:Create(Humanoid, CrouchTweenInfo, CrouchOutOfProperties)

-- UI --
local Gui = Player.PlayerGui:WaitForChild("MobileUI"):WaitForChild("Screen")
local GuiCrouch = Gui:WaitForChild("CrouchButton")
local GuiRun = Gui:WaitForChild("RunButton")

local UIS = game:GetService('UserInputService')
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local PlayerLight = ReplicatedStorage:WaitForChild("AmbienceLight")
local PlayerlightClone = PlayerLight:Clone()
PlayerlightClone.Parent = Character:WaitForChild("Torso")

local Flashlight = Character:WaitForChild("Flashlight")
local LightEmitter = Flashlight:WaitForChild("FlashlightLightEmitter")
local Rings = Flashlight:WaitForChild("FlashlightRings")

local RunKey = Enum.KeyCode.LeftShift or Enum.KeyCode.ButtonL2
local CrouchKey = Enum.KeyCode.C or Enum.KeyCode.ButtonB

local Running = false
local Crouching = false
local HumanoidMoving = false

local StaminaAmount = 150
StaminaAmount = math.clamp(StaminaAmount, 0, 150)

-- Functions --

local function RunKeyPressed()
	if StaminaAmount > 0 then
		Running = true
		RunTween:Play()
		wait(0.45)
		AwayWall = -0.9
		AgainstWall = 0
		CameraPosition = NormCam
		while StaminaAmount > 0 and Running ==  true do -- Stamina decrease
			StaminaAmount = StaminaAmount - 1
			wait(0.1)
			if StaminaAmount <= 0 then -- Ran out of Stamina
				OutOfRunTween:Play()
				AwayWall = -0.9
				AgainstWall = 0
				CameraPosition = NormCam
				Running = false
			end
		end
	end
end

local function CrouchKeyPressed()
	Crouching = true
	Running = false
	IntoCrouchTween:Play()
	wait(0.45)
	AwayWall = -1.25
	AgainstWall = -0.35
	CameraPosition = CrouchCam
end

local function RunKeyReleased()
	Running = false
	OutOfRunTween:Play()
	AwayWall = -0.9
	AgainstWall = 0
	CameraPosition = NormCam
	while StaminaAmount < 150 and Running == false do
		StaminaAmount = StaminaAmount + 0.5
		wait(0.1)
		if StaminaAmount <= 0 then
			Running = false
			AwayWall = -0.9
			AgainstWall = 0
			CameraPosition = NormCam
		end
	end
end

local function CrouchKeyReleased()
	Crouching = false
	OutCrouchTween:Play()
	wait(0.45)
	AwayWall = -0.9
	AgainstWall = 0
	CameraPosition = NormCam
end

-- Dececting input --

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == RunKey then
		RunKeyPressed()
	elseif input.KeyCode == CrouchKey then
			CrouchKeyPressed()
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == RunKey then
		RunKeyReleased()
	elseif input.KeyCode == CrouchKey then
		CrouchKeyReleased()
	end
end)

GuiRun.MouseButton1Down:Connect(RunKeyPressed)
GuiCrouch.MouseButton1Down:Connect(CrouchKeyPressed)
GuiRun.MouseButton1Up:Connect(RunKeyReleased)
GuiCrouch.MouseButton1Up:Connect(CrouchKeyReleased)

--Change Transparency--
for childIndex, child in pairs(Character:GetChildren()) do
	if child:IsA("BasePart") and child.Name ~= "Head" then
		child:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
			child.LocalTransparencyModifier = child.Transparency
			LightEmitter.LocalTransparencyModifier = LightEmitter.Transparency
			Rings.LocalTransparencyModifier = Rings.Transparency
		end)
	end
end

--Detecting Movement--
local function DetectMovement()
	if Humanoid.MoveDirection.Magnitude > 0 then
		HumanoidMoving = true
		if Running == true then -- Player is running
			AwayWall = -1.2
			AgainstWall = -0.3
			IntoRunMovingTween:Play()
		elseif Crouching == true then -- Player is crouching
			AwayWall = -1.4
			AgainstWall = -0.5
			IntoCrouchWalkTween:Play()
		elseif Running == false and Crouching == false then -- Player is walking
			AwayWall = -1.05
			AgainstWall = -0.15
			IntoWalkTweenTween:Play()
		end
	elseif Humanoid.MoveDirection.Magnitude == 0 then -- Player is not moving
		if Crouching == true then
			AwayWall = -1.25
			AgainstWall = -0.35
			IntoCrouchTween:Play()
		else
			AwayWall = -0.9
			AgainstWall = 0
			IdleTween:Play()
		end
	end
end

RunService.Heartbeat:Connect(DetectMovement)

--If the player steps in a vehicle--
Camera:GetPropertyChangedSignal("CameraSubject"):Connect(function()
	if Camera.CameraSubject:IsA("VehicleSeat") then
		Camera.CameraSubject = Humanoid
	end
end)




RunService.RenderStepped:Connect(function() --Hit Wall
	local ray = Ray.new(Character.Head.Position, ((Character.Head.CFrame + Character.Head.CFrame.LookVector * 2) - Character.Head.Position).Position.Unit)
	local ignoreList = Character:GetChildren()

	local hit, pos = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)

	if hit then
		CamZ = AgainstWall
	else
		CamZ = AwayWall
	end
end)


while true do
	print(CamZ)
	wait()
end

Thank you for any help!!

tweens have to be called on objects because the “goal” table is basically simulating the object, any values you add to the tween table are mirrored in the end result of the object. You can’t tween variables because the object itself would have to change. 1 ~= 2 but part == part regardless of its properties if that makes sense.

in order to solve this use a simulated variable. Create a NumberValue and parent it wherever (I prefer under the script that’s modifying it) then replace all references to your variable with Path.To.NumberValue.Value. Because the NumberValue itself is an object you can tween its Value property just as you would any other object property.

Ohh I see! TYSM!! I’ll try than when I can and report back what happens C: thank you though!!! :smiley:

1 Like

Update:

Okay knowing me I’m probably just being stupid, but I put two number values under the local script and changed their value instead as well as referenced them in the script and nothing changed. I then even made one unique number value for every possible value and manually put them in the script and still nothing changed

local NormCamZAway = script:WaitForChild("AwayWall"):WaitForChild("NormCam").Value
local CrouchCamZAway = script:WaitForChild("AwayWall"):WaitForChild("CrouchCam").Value
local MovingNormCamZAway = script:WaitForChild("AwayWall"):WaitForChild("MovingNormCam").Value
local RunningNormCamZAway = script:WaitForChild("AwayWall"):WaitForChild("RunningNormCam").Value
local MovingCrouchCamZAway = script:WaitForChild("AwayWall"):WaitForChild("MovingCrouchCam").Value

local NormCamZAgainst = script:WaitForChild("AgainstWall"):WaitForChild("NormCam").Value
local CrouchCamZAgainst = script:WaitForChild("AgainstWall"):WaitForChild("CrouchCam").Value
local MovingNormCamZAgainst = script:WaitForChild("AgainstWall"):WaitForChild("MovingNormCam").Value
local RunningNormCamZAgainst = script:WaitForChild("AgainstWall"):WaitForChild("RunningNormCam").Value
local MovingCrouchCamZAgainst = script:WaitForChild("AgainstWall"):WaitForChild("MovingCrouchCam").Value

local AwayWall = NormCamZAway
local AgainstWall = NormCamZAgainst

local CamZ = AwayWall

local NormCam = Vector3.new(0, 0, CamZ) -- -0.9
local CrouchCam = Vector3.new(0, -1.5, CamZ) -- -1.25
local MovingNormCam = Vector3.new(0, 0, CamZ) -- -1.05
local RunningNormCam = Vector3.new(0, 0, CamZ) -- -1.2
local MovingCrouchCam = Vector3.new(0, -1.5, CamZ) -- 1.4
local CameraPosition = Humanoid.CameraOffset

Once again, knowing me I’m probably just being stupid bu I’m just not sure why nothing is happening. So sorry to bother :sob: