Looking for Feedback/Code Review for my 3D Shop

Hey!
I’ve made a shop for my game I’m working on.

I don’t want it to be too basic, so instead of just making a ScreenGui I put together a simple shop model.
Looks like this at the moment of writing:
RobloxStudioBeta_9eGjv2GRQU

I’m a begginer at programming, so my code was spaghetti’d together and it’s not very good… :sweat_smile:
Although super messy, it works somehow.

The way it works is, the player enters the shop by interacting with a ProximityPrompt.
To leave the shop, the player just needs to press the “Back” button on the Shop Gui.
When the player enters the shop the following code gets executed:

local RunService = game:GetService("RunService")
local TS = game:GetService("TweenService")
local CamPos = workspace.Shop.CamPos.CFrame -- Shop Cam Pos
local Camera = workspace.CurrentCamera -- Player Camera
local Player = game.Players.LocalPlayer
local PlayerLoco = Player.Character.HumanoidRootPart.CFrame -- Local Player Position
local PlayerPos = workspace.Shop.PlayerPos.CFrame -- Player Position while in Shop
local ShopGui = script.Parent.Parent.Parent -- ShopGUI
local SwipeAnimGui = script.Parent.Parent.Parent.Parent.TransitionGui.Left
-- Configure and Anchor Camera into Shop
Camera.CameraType = Enum.CameraType.Scriptable
TS:Create(Camera, TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {CFrame = Camera.CFrame * CFrame.new(-50, 0, 0)}):Play()
SwipeAnimGui:TweenPosition(UDim2.new(2, 0, 0, 0),
	Enum.EasingDirection.Out,
	Enum.EasingStyle.Sine,
	1.2,
	false)
wait(.2)
Camera.CFrame = CamPos * CFrame.new(50, 0, 0)
TS:Create(Camera, TweenInfo.new(.5), {CFrame = CamPos}):Play()
wait(.5)
Camera.CFrame = CamPos
ShopGui.Enabled = true
wait(.5)
SwipeAnimGui.Position = UDim2.new(-1, 0, 0, 0)
Player.Character.HumanoidRootPart.Anchored = true
Player:SetAttribute("InShop", true)

Essentially what this script does is:

  • It sets the camera as scriptable and animates it into a part (the camera position while in shop)
  • Animates a GUI to give it a cool transition
  • Stucks the player in place while in the shop

Now, when the player leaves the shop a similar script is executed:

local RunService = game:GetService("RunService")
local TS = game:GetService("TweenService")
local Camera = workspace.CurrentCamera
local CamPos = workspace.Shop.CamPos.CFrame
local Player = game.Players.LocalPlayer
local ShopGui = script.Parent.Parent.Parent
TS:Create(Camera, TweenInfo.new(.5), {CFrame = CamPos * CFrame.new(20, 0, 0)}):Play()
local SwipeAnimGui = script.Parent.Parent.Parent.Parent.TransitionGui.Right
	SwipeAnimGui:TweenPosition(UDim2.new(-2, 0, 0, 0),
		Enum.EasingDirection.Out,
		Enum.EasingStyle.Sine,
		1.2,
		false)
Player.Character.HumanoidRootPart.Anchored = false
wait(.2)
Camera.CFrame = Player.Character.Head.CFrame * CFrame.new(-20, -1, -1)
TS:Create(Camera, TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {CFrame = Player.Character.Head.CFrame * CFrame.new(0, -1, -1)}):Play()	
wait(.5)
Camera.CameraType = Enum.CameraType.Custom
ShopGui.Enabled = false
wait(.5)
SwipeAnimGui.Position = UDim2.new(1, 0, 0, 0)
Player:SetAttribute("InShop", false)

That’s all the code! It’s pure spagghetti (I hope it’s bearable enough).
Does anyone have suggestions of how I can improve it?
Also if this post’s quality is bad, I apologize, it’s my first time posting in the forum, I tried my best :3

2 Likes

Pretty cool! I like it. Maybe I should do something like this in my game

1 Like

Question. How do you get the items that are in the shop to be not flat on the screen. Is it a surface ui or what?

Alright so, here’s the review

  1. use task.wait() instead of wait()
  2. camelCase and no name abbreviation (personal opinion)
  3. Both scripts are so close, merge them.
  4. Add empty lines between unrelated items, ex.
local players = game:GetService...
local replicatedStorage = game:GetService...

local unrelatedThing = ...
local relatedKinda = ...

local tween = ...
-- etc
  1. You used the character multiple times so create a variable for it.
  2. Your tween infos are the same, create variables for them
  1. You got TweenService so use it.
  1. Don’t chain script.parent.parent....., create variables
  1. Get the Players service then do players.LocalPlayer, an exploiter can change the name of services and ruin this for you.

I think that’s everything.
- msix

4 Likes

I made ImageButtons for the items, so they’re 2D in my case. For the buy menu itself to be 3D, yes, I used a SurfaceGui.
That’s a cool idea though, if you do come up with something for the items to be 3D as well please let me know!

1 Like

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