How would I condense this script?

How would I condense this script to get all of the tools from a specific folder? I just feel like this script is causing unnecessary lag.

local ToolNames = {"AYSA","Adios","As If It's Your Last", "BAAM","Bad Boy","Birthday","Bon Bon Chocolat","Boom","Boombayah","Butterfly","DDU-DU DDU-DU","Dance the Night Away","Don't Know What to Do","Dun Dun","Dalla Dalla","Fancy","Feel Special","Fiesta","Flower Shower","Forever Young","GASHINA","Gee","Gotta Go","HIP","Happiness","Hi High","Hobgoblin","ICY","IDOL","Ice Cream Cake","Judas","LALALAY","ME","MARIA","Mic Drop","More & More","Not Shy","Oh My God","POPIPO","Peek a Boo","Pick Me (NEKKOYA)","Playing with Fire","Psycho","Red Flavor","Red Velvet Medley","Rookie","Russian Roulette","SOLO","Say So (Japanese)","Say So (Korean)","So Hot","Stay Tonight","Wannabe","What is Love","Whistle","Yes or Yes","Zimzalabim","Pop Star","Lovesick Girls","WYGOWM","Deja Vu","Ice Cream","RBB","TT","AIWFCIY","Roly Poly","DNA","Home","Hot Summer","Chica/Snapping","HYLT","KTL","I Can't Stop Me","What You Waiting For","PANORAMA","DUMDI DUMDI","Fever","Apple","Like Ooh Ahh","Time for the Moon Night","Bboom Bboom","Crown","Wee Woo","Secret Story Of  The Swan","Whatta Man","Mamma Mia","Nonstop","The Baddest","Go Go","Energetic (Wanna One)","Energetic (IZ*ONE*)","Fingertip"}
local Storage = game:GetService("ServerStorage")


local Part = script.Parent
local ClickDetector = Part:WaitForChild("ClickDetector")

ClickDetector.MouseClick:connect(function(Player)
	if Player and Player.Character then
		local Backpack = Player:WaitForChild("Backpack")
		for i = 1, #ToolNames do
			local Tool = Storage:FindFirstChild(ToolNames[i])
			if Tool then
				Tool:clone().Parent = Backpack
			end
		end
	end
end)
1 Like

You could make a folder in ServerStorage with all the items and iterate through all the items in the folder, clone then parent it to the player’s Backpack.

I feel like this can be simplified down to this

local Part = script.Parent
local ClickDetector = Part:WaitForChild("ClickDetector")
local ServerStorage = game:GetService("ServerStorage") -- I don't know how you intend to store all the tools, so I left this unchanged. I suggest putting all the tools in a folder somewhere instead of just leaving it in SS.

ClickDetector.MouseClick:Connect(function(Player) -- :connect -> :Connect
    if Player and Player.Character then
        for _, obj in pairs(ServerStorage:GetChildren()) do
            if obj:IsA("Tool") and (not Player.Backpack:FindFirstChild(obj.Name)) then -- instead of iterating through an entire loop, check if it's a tool. Then see if the tool is already in the backpack.
                obj:Clone().Parent = Player.Backpack -- no need for :WaitForChild since the character exists anyway
            end
        end
    end
end)

I also suggest what @AWhale_OfATime suggested: put all your tools in a folder so that it can be accessed such that game.ServerStorage.Tools:GetChildren() can return all the tools you want for this task.

Furthermore if you believe that a script is causing unnecessary lag, you can check the console and see which scripts are eating up resources instead of blindly believing one script is doing so.

1 Like