Tweening Camera Shop

Hey so I’ve got this little shop thing here. I don’t know what to call it so I’ll just link a video:

Now this works exactly how I want it to, however I know I’m not doing this in the most efficient way possible, and I’m also planning on adding in like 30 different slots to this shop. Here is my code to make this work:

– Note this is a LocalScript located in StarterPlayerScripts.

game.Players.LocalPlayer.PlayerGui:WaitForChild("ChoosePowerUpUI").NextButtonRight.MouseButton1Click:Connect(function()
	if currentCamNumber == 1 then
		local Tween = game:GetService("TweenService"):Create(camera, tweeninfo, {CFrame = cameraPart2.CFrame})
		currentCamNumber = 2
		unlockButton.Text = "Unlock: Katana"
		Tween:Play()
		print(currentCamNumber)
	elseif currentCamNumber == 2 then
		local Tween = game:GetService("TweenService"):Create(camera, tweeninfo, {CFrame = cameraPart3.CFrame})
		currentCamNumber = 3
		unlockButton.Text = "Unlock: Double Daggers"
		Tween:Play()
		print(currentCamNumber)
	elseif currentCamNumber == 3 then
		local Tween = game:GetService("TweenService"):Create(camera, tweeninfo, {CFrame = cameraPart4.CFrame})
		currentCamNumber = 4
		unlockButton.Text = "Unlock: Bo Staff"
		Tween:Play()
		print(currentCamNumber)
	elseif currentCamNumber == 4 then
		local Tween = game:GetService("TweenService"):Create(camera, tweeninfo, {CFrame = cameraPart5.CFrame})
		currentCamNumber = 5
		unlockButton.Text = "Unlock: Trident of Darkness"
		Tween:Play()
		print(currentCamNumber)
	elseif currentCamNumber == 5 then
		local Tween = game:GetService("TweenService"):Create(camera, tweeninfo, {CFrame = cameraPart6.CFrame})
		currentCamNumber = 6
		unlockButton.Text = "Unlock: Aaron"
		Tween:Play()
		print(currentCamNumber)
	elseif currentCamNumber == 6 then
		local Tween = game:GetService("TweenService"):Create(camera, tweeninfo, {CFrame = cameraPart1.CFrame})
		currentCamNumber = 1
		unlockButton.Text = "Unlock: Pistol"
		Tween:Play()
		print(currentCamNumber)
	end
end)

currentCamera refers to a variable I added at the top of the code to track which spot in the shop the player was on, so if the player was on the first powerup of the shop, they would be in the first slot, so currentCamera would be set to 1.

Not only do I want that part to be more efficient if possible, but as you can see I’m also creating a new tween every single time, and I was wondering if there was another way to do this, but considering that I have a different position for each tween, that may not be possible.

Like I said, everything works perfectly fine, which is why I’m posting it here, I just to know how I could make this more efficient. I’ll try to answer any of your questions. If you need more code, you can ask me because this is only one part of it. Thanks for looking over this!

well, you could make it shorter and more expendable like this:

local camera = workspace.CurrentCamera

local tween = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1) -- replace this

local player = game.Players.LocalPlayer
local playerUi = player:WaitForChild("PlayerGui")
local myUi = playerUi:WaitForChild("ChoosePowerUpUI")
local rightButton = myUi:WaitForChild("NextButtonRight")
local leftButton = myUi:WaitForChild( LEFT BUTTON NAME HERE :) ) -- <-- replace this part, to the button that makes the camera go left
local unlockButton = myUi:WaitForChild("unlockButton")

local myCamPats = nil -- replace this where ever the parts are and make sure that the parts are named 1, 2, 3, ...
local currentTween = nil
local currentCamNumber = 0

local camData = {
-- put here all of the item names. Left it as a table, so you can customize each camera scene.
-- Something like that
	{
		text = "Random text"
	},
	{
		text = "blah blah"
	},
}

local function playTween()
	if currentTween then
		currentTween:Cancel()
		currentTween = nil
	end
	
	currentTween = tween:Create(camera, tweenInfo, {Position = myCamPats:FindFirstChild(currentCamNumber).Position})
	currentTween:Play()
end

rightButton.MouseButton1Click:Connect(function()
	if currentCamNumber + 1 < #camData then -- not sure about this part tho, makes sure you cant go over the table's size
		currentCamNumber += 1
		local myCurrentData = camData[currentCamNumber]
		unlockButton.Text = myCurrentData.text
		playTween()
	end
end)

leftButton.MouseButton1Click:Connect(function()
	if currentCamNumber - 1 > 0 then -- makes sure you cant go belov 0
		currentCamNumber -= 1
		local myCurrentData = camData[currentCamNumber]
		unlockButton.Text = myCurrentData.text
		playTween()
	end
end)

I hope this helped you. If you don’t understand something, or it doesn’t really work, feel free to tell me!

3 Likes

Thanks so much for replying. Your ideas worked perfectly. Really happy that everything is simplified and shortened now! Thanks again!

2 Likes