How do I make a script that gets all the tools in workspace with no lag (Solved)

My current script:

wait(3)
-- Made by dumby_pro56

while true do
	wait(1.5)
	local Save = {}
	for i=1, #workspace:GetDescendants() do
		wait(0)
		
		local Child = workspace:GetDescendants()[i]
		if Child ~= nil then
			if Child:IsA('Tool') then
				if Child:FindFirstChild('Handle') then
					local Handle = Child:FindFirstChild('Handle')::Part
					local Distance = (script.Parent.HumanoidRootPart.Position - Handle.Position).Magnitude
					
					local Insert = {
						[1] = Handle,
						[2] = Distance
					}
					table.insert(Save, Insert)
				end
			end
		end
	end
	print(Save)
end

I just need to make it fast with no lag…

3 Likes

Can’t you just group the tools in a folder and just use Folder:GetChildren()?

2 Likes

what about the people holding the items

Try:

local tools = {}

for i, v in ipairs(workspace:GetDescendants()) do
    if v:IsA("Tool") then
        table.insert(tools, v)
    end
end
1 Like

It’s because it runs every 2 Sec and some of my items don’t have any handles it’s a robber script

So you want to ignore the items that don’t have handles?

mine works fine besides the fact it takes like 3 min if I remove the wait(0) it lags I want to fix both of those

Ok. Try this:

local tools = {}
for i, v in ipairs(workspace:GetDescendants()) do
    if v:IsA("Tool") and v:FindFirstChild("Handle") then
        table.insert(tools, v)
    end
end

Also, it will lag in a while true loop, and looping that quickly infinitely might not be the best way of doing it. If you really want to use a loop, try changing while true to

while task.wait(0.5) do
end
1 Like

Try This.

local AllTools = {}

for i, v in ipairs(workspace:GetDescendants()) do
	if v:IsA("Tool") then
		table.insert(AllTools, v)
	end
end
workspace.DescendantAdded:Connect(function(Descendant)
	if Descendant:IsA("Tool") then
		table.insert(AllTools, Descendant)
	end
end)
workspace.DescendantRemoving:Connect(function(Descendant)
	if table.find(AllTools,Descendant) then table.remove(AllTools,table.find(AllTools,Descendant)) end
end)


while true do
	wait(1.5)
	local Save = {}
	for _,Tool in pairs(AllTools) do
		local Handle = Tool:FindFirstChild('Handle')
		if Handle then
			local Distance = (script.Parent.HumanoidRootPart.Position - Handle.Position).Magnitude

			local Insert = {
				[1] = Handle,
				[2] = Distance
			}
			table.insert(Save, Insert)

		end
	end
	print(Save)
end

Your Tools will always be up to date now ā€œAllToolsā€

why do people use in pairs I don’t understand this and I’ve been scripting for 2 years :[

1 Like

oh nvm it’s the same as what I did I just had to add that while task.wait(1.5) do

1 Like

pairs() and ipairs() return an iterator function for use in a ā€˜for’ loop (quote from Studio).

Basically, they iterate (loop) through each item in the table.

If you have a table:

{
    "Hi"
}

It’s actually:

{
    [1] = "Hi"
}

In this case, 1 is the key, and ā€œHiā€ is the value. pairs() iterates through any keys (number and strings) and ipairs() iterates only through numerical keys (keys that are a number).

Why dont you use collectionservice and tag them.
Then you can get them easily via CollectionService:GetTagged(ā€œtagenameā€)
?

I have to add it to every tool and if I forgot people would just hold that item and kill the other players

run this code in the command line

for _, V in pairs(game:GetDescendants()) do
	if V:IsA("Tool") then
		game.CollectionService:AddTag(V, "Tool")
	end
end

Thus, you got all the tools tagged and can now get them via your script easily with collectionservice.

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