Camera script not focusing

I’am trying to make a shop that is simmilar to Bee Swarm’s… Where the camera focuses on the thing you are selling

I ran into a problem I have to exact scirpts they only differ in a few values but for some reason the Jar part of script doesn’t focus on the intended part

I tried to print the values thinking it was the problem but the values are fine, and I really have no idea whats the problem!

Video of the problem:

Code:

local TweenService = game:GetService("TweenService")
local localPlayer = game:GetService("Players").LocalPlayer
local MoneyHandler = game.ReplicatedStorage.MoneyHandler
local changeTable = game.ReplicatedStorage.changeTable
local forward = script.Parent.Forward
local back = script.Parent.Backward
local buy = script.Parent.Buy
local cam = game.Workspace.CurrentCamera


--Tables
local camsTables1 = workspace.Store.Room1.Cams:GetChildren()
table.sort(camsTables1, function(a,b)
	return a.Name < b.Name
end)
local focusTables1 = workspace.Store.Room1.Focus:GetChildren()
table.sort(focusTables1, function(a,b)
	return a.Name < b.Name
end)

--Jars
local camsJars1 = workspace.Store.Room3.Cams:GetChildren()
table.sort(camsJars1, function(a,b)
	return a.Name < b.Name
end)

local focusJars1 = workspace.Store.Room3.Focus:GetChildren()
table.sort(focusJars1, function(a,b)
	return a.Name < b.Name
end)

print(focusJars1)

local Name = script.Parent.itemName
local Info = script.Parent.Info
local Index = 1





local function moveCamera(cameraObj, focusObj)
	print(camsJars1[Index], " ", focusJars1[Index], "Received value: ", cameraObj, " ", focusObj)
	
	local camAnim = TweenService:Create(cam,TweenInfo.new(.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), --Moves the camera right here
		{CFrame = cameraObj.CFrame, Focus = focusObj.CFrame})
		camAnim:Play()
end

forward.MouseButton1Click:Connect(function()
	Index += 1
	if Index == 6 then
		Index = 1
	end
	if script.Parent.Parent.Parent.openShop.LocalScript.TableMode.Enabled then
		print("Tablet Mod")
		Info.Text = focusTables1[Index].Info.Value
		Name.Text = focusTables1[Index].itemName.Value
		cam.CameraType = Enum.CameraType.Scriptable
		moveCamera(camsTables1[Index], focusTables1[Index])
	end
	
	if script.Parent.Parent.Parent.openShop.LocalScript.JarMode.Enabled then
		print("Jar Mod")
		Info.Text = focusJars1[Index].Info.Value
		Name.Text = focusJars1[Index].itemName.Value
		cam.CameraType = Enum.CameraType.Scriptable

		moveCamera(camsJars1[Index], focusJars1[Index])
	end
end)

back.MouseButton1Click:Connect(function()
	if Index == 1 then
		Index = 6
	end
	Index -= 1
	if script.Parent.Parent.Parent.openShop.LocalScript.TableMode.Enabled then
		Info.Text = focusTables1[Index].Info.Value
		Name.Text = focusTables1[Index].itemName.Value
		cam.CameraType = Enum.CameraType.Scriptable
		moveCamera(camsTables1[Index], focusTables1[Index])
	end
	if script.Parent.Parent.Parent.openShop.LocalScript.JarMode.Enabled then
		Info.Text = focusJars1[Index].Info.Value
		Name.Text = focusJars1[Index].itemName.Value
		cam.CameraType = Enum.CameraType.Scriptable
		print(focusJars1[Index])
		moveCamera(camsJars1[Index], focusJars1[Index])
	end
end)

Hiercarhy:

Thanks in advance!

1 Like

Idk what the Focus is doing here as its mentioned in the docs

Focus has no bearing on the positioning or orientation of the Camera (see Camera.CFrame for this).

But here’s the fix:

local camAnim = TweenService:Create(cam,TweenInfo.new(.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), --Moves the camera right here
		{CFrame = CFrame.lookAt(cameraObj.Position, focusObj.Position)})
camAnim:Play()
1 Like

Hey, man thank you soo freaking much I’ve been looking at the code for hours trying to find the problem, but didn’t think of going to the docs :sweat_smile:

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