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
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
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ā
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).