My goal:
I want to make a cam system where the camera’s position gets tweened to a different spot when a part is touched while being aligned with a different part.
What I have currently:
I have two parts. A part that follows the player’s position using a tween, and a part that aligns with the following player part in a single axis that is apart by a few studs.
I have the camera at the aligning part’s position using a different script but that script is not the issue.
Notes:
- I have created two vector values; CurrentVector is the aligned part that is separated by whatever value CurrentVector has, and NextVector is the vector I want to tween the camera to.
- FrontCam is the part that follows the player’s position.
- I have a value changing script that changes the NextValue’s value when a part is touched so that the script knows where to tween next. I know that the value changing script is also not the issue.
script:
local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")
local Players = game:GetService("Players")
repeat
wait()
until game:IsLoaded()
--// Variables
local player = Players.LocalPlayer
local character = player.Character
local camera = workspace.CurrentCamera
local CurrentCamPart
local PartPos
local CurrentVector = character:WaitForChild("CamDisplacementCurrent")
local NextVector = character:WaitForChild("CamDisplacementNext")
--// Functions
local function currentCam(char)
CurrentCamPart = Instance.new("Part")
CurrentCamPart.Parent = char
CurrentCamPart.Transparency = 0.5
CurrentCamPart.Anchored = true
CurrentCamPart.CanCollide = false
CurrentCamPart.Name = "CurrentCamPart"
CurrentCamPart.Size = Vector3.new(1, 1, 1)
CurrentCamPart.Position = PartPos
end
local function camFollowPart()
if character:FindFirstChild("HumanoidRootPart") then
PartPos = character:WaitForChild("HumanoidRootPart").Position + Vector3.new(5, 6, 30)
if not CurrentCamPart or not character:FindFirstChild(CurrentCamPart.Name) then
currentCam(character)
end
if CurrentVector.Value ~= NextVector.Value and NextVector.Value ~= Vector3.new(0, 0, 0) then
local tweenVector
tweenVector = tweenService:Create(CurrentCamPart, TweenInfo.new(2, Enum.EasingStyle.Back), {Position = character:WaitForChild("HumanoidRootPart").Position + NextVector.Value})
tweenVector:Play() --Tweening to a new spot
if character:WaitForChild("HumanoidRootPart").Position.X < (NextVector.Value.X - 0.01) or character:WaitForChild("HumanoidRootPart").Position.X > (NextVector.Value.X - 0.01) then --Checks if the aligned part is aligned to the following part.
tweenVector:Cancel()
CurrentVector.Value = NextVector.Value
NextVector.Value = Vector3.new(0, 0, 0)
end
end
end
end
local function onUpdate() --Aligning Current Cam Position
if CurrentVector then
if character:FindFirstChild("FrontCam") then
character:WaitForChild("CurrentCamPart").Position = character:WaitForChild("FrontCam").Position + CurrentVector.Value
end
end
end
--// RunService
runService.RenderStepped:Connect(camFollowPart)
runService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, onUpdate)
Problem:
When the tween runs, the onUpdate() function and the tween runs at the same time. The onUpdate() is keeping the part at the CurrentVector while the tween is trying to move to the NextVector so the tweening never works and ends up moving to the NextVector as if a tween never happened.
Are there any solutions on how I can make the tween work normally?
Video:
robloxapp-20240525-2357366.wmv (762.8 KB)