Help with my Script

Hello everyone, I’m new to scripting, I want to ask you if there’s any way to simplify my code? To make it simpler. What the script does is shown in the video.
module Script

local module = {}

local workSpace = game:GetService("Workspace")
local Player = game:GetService("Players")
local ts = game:WaitForChild("TweenService")

local player = Player.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local Gui = player.PlayerGui:WaitForChild("MainGui")
local TradeFrame = Gui.Trade.TradeFrame
local camera = workSpace.CurrentCamera

local EstimateText = TradeFrame.TextEstimate
local OfferText = TradeFrame.TextOffer
local NameText = TradeFrame.TextName

local ItemFolder = workSpace:WaitForChild("ItemFolder")
local ItemName = {"chest", "flower", "lamp", "paper", "pan", "knife", "cup", "plate", "pot", "key", "book", "pillow", "fence", "woodenBox", "woodBox", "box"}
local ItemPrice = {1000, 970, 950, 1100, 550, 560, 530, 570, 500, 520, 250, 230, 220, 260, 270, 300}
local ItemRot = {-90, 180, -90, 180}

local ItemRandom
local ItemClone

local ItemPos = {}
local PlrPos = {}
local CameraPos = {}

for i = 1, 4 do
	ItemPos[i] = workSpace:WaitForChild("ItemPos"..i)
	PlrPos[i] = workSpace:WaitForChild("PlrPos"..i)
end

function module.ItemGen(CurrentPlr)
	
	character:PivotTo(PlrPos[CurrentPlr].CFrame)
	humanoid.WalkSpeed = 0
	humanoid.JumpHeight = 0
	
	camera.CameraType = Enum.CameraType.Scriptable
	CameraPos = ItemPos[CurrentPlr].CFrame * CFrame.new(0, 0, 3) * CFrame.new(0, 1.5, 0) * CFrame.Angles(math.rad(-15), 0, 0)
	ts:Create(camera, TweenInfo.new(0.5), {CFrame = CameraPos}):Play()
	
	if ItemClone ~= nil then
		ItemClone:Destroy()
	end
	
	ItemRandom = math.random(1, #ItemName)
	for i, child in pairs(ItemFolder:GetChildren()) do
		if child.Name == ItemName[ItemRandom] then
			
			ItemClone = child:Clone()
			ItemClone.Parent = workSpace
			ItemClone:MoveTo(ItemPos[CurrentPlr].Position)
			ItemClone:PivotTo(ItemClone:GetPivot() * CFrame.Angles(0, math.rad(ItemRot[CurrentPlr]), 0))
			
		end
	end
end

return module

Local Script

local Replic = game:GetService("ReplicatedStorage")

local Gui = script.Parent.Parent
local But = script.Parent.TextButton
local CurrentPlr

local remotePlr = Replic.remote.remotePlr
local moduleItem = require(Replic.module.ModuleItem)
local IteGen = moduleItem.ItemGen

remotePlr.OnClientEvent:Connect(function(i)
	CurrentPlr = i
end)

But.MouseButton1Click:Connect(function()
	IteGen(CurrentPlr)
end)
1 Like

Your code is already pretty efficient and simple! Although ‘simple’ is a very loose term, so I’m not sure exactly what your goal is

You can probably merge ItemName and ItemPrice into one table

local ItemData = {
[1] = {Name = "Chest", Price = 1000}
}

and then you can just fetch the stuff from it using

local Item = ItemData[1]
local Name = Item.Name
local Price = Item.Price

And I see a lot of unused variables, make sure that once your code is fully complete unused variables don’t remain

Good work!

Edit: while purely stylistic, consider comments for readability and separation

--//SERVICES
local RunSer = game:GetService("RunService")
local TS = game:GetService("TweenSService")

--//CHARACTER
local Character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

--etc etc...

It makes it less likely you’ll get lost in your variables and makes your current variables easier to find

1 Like

Thank you so much for your reply!

1 Like

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