How can i optimize this code?

I want to optimize my code but i dont see how i can put “for” or “collectionservice” here

local DataLoad = game.ReplicatedStorage:WaitForChild("DataLoad")
local plr = game.Players.LocalPlayer
local Folder = plr:WaitForChild("Itens")
local RocksV = script.Parent:WaitForChild("RocksValue")
local CopperV = script.Parent:WaitForChild("CopperValue")
local IronV = script.Parent:WaitForChild("IronValue")
local GoldV = script.Parent:WaitForChild("GoldValue")
local WoodV = script.Parent:WaitForChild("WoodValue")
local WheatV = script.Parent:WaitForChild("WheatValue")

Folder.Rocks.Changed:Connect(function()
	RocksV.Text = Folder.Rocks.Value
end)
Folder.Coppers.Changed:Connect(function()
	CopperV.Text = Folder.Coppers.Value
end)
Folder.Irons.Changed:Connect(function()
	IronV.Text = Folder.Irons.Value
end)
Folder.Golds.Changed:Connect(function()
	GoldV.Text = Folder.Golds.Value
end)
Folder.Woods.Changed:Connect(function()
	WoodV.Text = Folder.Woods.Value
end)
Folder.Wheats.Changed:Connect(function()
	WheatV.Text = Folder.Wheats.Value
end)

DataLoad.OnClientEvent:Connect(function()
	RocksV.Text = Folder.Rocks.Value
	CopperV.Text = Folder.Coppers.Value
	IronV.Text = Folder.Irons.Value
	GoldV.Text = Folder.Golds.Value
	WoodV.Text = Folder.Woods.Value
	WheatV.Text = Folder.Wheats.Value
end)
1 Like
local values = {
"RocksValue",
"CopperValue",
--etc.
}
local resourceTypes = {
"Rocks",
"Copper"
--put in same order as table above
--etc.
}
for i, v in resourceTypes do
resourceTypes[i] = Folder:WaitForChild(v)
end
for i, v in values do
values[i]=script.Parent:WaitForChild(v)
values[i].Changed:Connect(function()
values[i].Text = resourceTypes[i].Value
end)
end
1 Like

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