Need some help with a camera tween

So,what I want to make with this script is making a 2D camera putting the camera inside a part that will always be 40 studs above the player,and it will be looking at another part that will always be 3 studs above the player,I will be doing this using a camera tween.
However I don’t know why the camera tween is’nt working,the tween is the only part of the script that does’nt work properly.
I’m a beginner at scripting,and I really need help with it.

wait()
local ts = game:GetService("TweenService")--TweenService variable
local Cam = game.Workspace.CurrentCamera--Cam variable
Cam.CameraType = Enum.CameraType.Scriptable--Cam Type Property

local lookAt = Instance.new("Part")--Selecting "lookAt" Properties,it's the part that will be 3 studs above the player
lookAt.Name = "lookAt"
lookAt.Parent = workspace
lookAt.Anchored = true
lookAt.Size = Vector3.new(0.1, 0.1, 0.1)    
lookAt.CastShadow = false
lookAt.CanCollide = true

local CamPart = Instance.new("Part") --Selecting "CamPart" Properties,it's the part that is going to be 40 studs above the player
CamPart.Name = "CamPart"
CamPart.Parent = workspace
CamPart.Anchored = true
CamPart.Size = Vector3.new(0.5, 0.5, 0.5)
CamPart.CastShadow = false
CamPart.CanCollide = true

local Player = game:GetService("Players").LocalPlayer--Player variable
local char = Player.Character or Player.CharacterAdded:Wait()--character variable
if not (char) then
	return
end
local HumanoidRootPart = char:FindFirstChild("HumanoidRootPart")--humanoidrootpart variable
if not (HumanoidRootPart) then
	return
end
local humanoid = char:WaitForChild("Humanoid")--humanoid variable

while true do                                    --Selecting CamPart and lookAt new positions
	local rootPos = HumanoidRootPart.Position
	wait()
	if true then
		CamPart.Position = rootPos + Vector3.new(0, 40, 0)
		wait()
		lookAt.Position = rootPos + Vector3.new(0, 3, 0)
		wait()
	end	
end
-------------Making a Tween--------------------
wait()
local lookAtPosition = lookAt.Position                    --Making lookAt's position a variable
local CamPartPosition = CamPart.Position                  --Making CamPart's position a variable
local camgoal = {CFrame = CFrame.new(CamPartPosition), lookAtPosition}   --camgoal variable
local info = TweenInfo.new(0, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)  --tweenInfo variable
local tween = ts:Create(Cam, info, camgoal)               --Tween
wait(1)
while true do
task.wait()
tween:Play()
end
2 Likes

I’m not the best scripter, but I tried:

local ts = game:GetService("TweenService")
local Cam = game.Workspace.CurrentCamera
Cam.CameraType = Enum.CameraType.Scriptable

local lookAt = Instance.new("Part")
lookAt.Name = "lookAt"
lookAt.Parent = workspace
lookAt.Anchored = true
lookAt.Size = Vector3.new(0.1, 0.1, 0.1)
lookAt.CastShadow = false
lookAt.CanCollide = false

local CamPart = Instance.new("Part")
CamPart.Name = "CamPart"
CamPart.Parent = workspace
CamPart.Anchored = true
CamPart.Size = Vector3.new(0.5, 0.5, 0.5)
CamPart.CastShadow = false
CamPart.CanCollide = false

local Player = game:GetService("Players").LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait()
if not char then
    return
end
local HumanoidRootPart = char:FindFirstChild("HumanoidRootPart")
if not HumanoidRootPart then
    return
end
local humanoid = char:WaitForChild("Humanoid")

while true do
    local rootPos = HumanoidRootPart.Position
    CamPart.Position = rootPos + Vector3.new(0, 40, 0)
    lookAt.Position = rootPos + Vector3.new(0, 3, 0)
    wait(0.1)
end

local lookAtPosition = lookAt.Position
local CamPartPosition = CamPart.Position
local camgoal = {
    CFrame = CFrame.new(CamPartPosition, lookAtPosition) -- Corrected CFrame creation
}
local info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
local tween = ts:Create(Cam, info, camgoal)

tween:Play()

Thank you so much for your help,unfortunately the problem still remains,I can still move the camera as I want,I wanted to upload a video but the limit for archives is 10MB for me

What’s not working about the tween? Is nothing happening, is it doing the wrong thing, etc?

It just does’nt happen,I can still move my camera freely.

1 Like

This is a problem–tween:Play() doesn’t yield, so it’s basically trying to start the tween 30 times a second, and each time cancels the last tween. Add tween.Completed:Wait() in there.

while true do                                    --Selecting CamPart and lookAt new positions
	local rootPos = HumanoidRootPart.Position
	wait()
	if true then
		CamPart.Position = rootPos + Vector3.new(0, 40, 0)
		wait()
		lookAt.Position = rootPos + Vector3.new(0, 3, 0)
		wait()
	end	
end
-------------Making a Tween--------------------
wait()
local lookAtPosition = lookAt.Position                    --Making lookAt's position a variable
local CamPartPosition = CamPart.Position                  --Making CamPart's position a variable
local camgoal = {CFrame = CFrame.new(CamPartPosition), lookAtPosition}   --camgoal variable
local info = TweenInfo.new(0, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)  --tweenInfo variable
local tween = ts:Create(Cam, info, camgoal)               --Tween
wait(1)
while true do
task.wait()
tween:Play()
end

Another problem is that your while true loops are stopping each other. A while true loop runs forever, so it never gets to the next line. Instead, wrap that in a task.spawn so it starts doing that not in the same sequence:

task.spawn(function()  -- Makes this run "out of sequence" so the stuff below doesn't wait forever for this to finish

	while true do                                    --Selecting CamPart and lookAt new positions
		local rootPos = HumanoidRootPart.Position
		wait()
		if true then
			CamPart.Position = rootPos + Vector3.new(0, 40, 0)
			wait()
			lookAt.Position = rootPos + Vector3.new(0, 3, 0)
			wait()
		end	
	end

end)
-------------Making a Tween--------------------
wait()
local lookAtPosition = lookAt.Position                    --Making lookAt's position a variable
local CamPartPosition = CamPart.Position                  --Making CamPart's position a variable
local camgoal = {CFrame = CFrame.new(CamPartPosition), lookAtPosition}   --camgoal variable
local info = TweenInfo.new(0, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)  --tweenInfo variable
local tween = ts:Create(Cam, info, camgoal)               --Tween
wait(1)
while true do
	tween:Play()
	tween.Completed:Wait() -- Wait for the tween to finish before trying to play it again
	task.wait() -- Just in case the tween has zero length to avoid an overflow
end

Thank you so much the camera works now.
I just have to do a change from here which was moving the second while loop to the beggining of the “making a tween part”,so the two parts positions will be always updating.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.