I have a script that loops through parts in a folder, and I am wondering, if I have close to a thousand parts in the folder, will it take a long time to loop through all parts?
Its based on what u will do with these parts
I am making a character jump when they touch a part, so I am worried that if I have too many parts in the folder it will take longer to loop through all the parts, thus making the player wait for a bit before it makes them jump
U could detect what player is touching and if part.Parent == Folder then it will make player jump
So u dont will need a loop for this
That would mean there would be a script in every part right? If so then I can’t do that because it makes it pretty laggy
There will be 1 script that will give all parts that player touched and when part is in this folder then it will make player jump
Could you possibly give an example of this code by chance?
First, how many parts are there? (you can print #folder:GetChildren()
to know)
Second,
There is no need to loop through every part. As @MrKlocek2 said, you just put a single script in the folder with parts.
i mean you still will loop through every part but atleast it is one-time so shouldn’t cause performance issues
local Players = game:GetService("Players")
local folder = script.Parent
for i, inst:BasePart in ipairs(folder:GetChildren()) do
if inst:IsA("BasePart") then
inst.Touched:Connect(function(other)
if other.Parent:IsA("Model") then
local plr = Players:GetPlayerFromCharacter(other.Parent)
if plr then
--jump
end
end
end)
end
end
Well that should work ^ but I would make touch event on player not all parts.
First- I don’t currently have all the parts in a folder yet (due to I have to change them all from models to their own parts) So my guess was close to a thousand.
Second, my code is pretty much the same as yours and I was told that it was looping through all the parts by another person on here.
Here is my code, in one script, inside the folder
local starterGreenPads = workspace:WaitForChild("StarterGreenPads")
for i, pad in pairs(starterGreenPads:GetChildren()) do -- Loop through the StarterGreenPads
if pad:IsA("BasePart") then -- If it's a part
pad.Touched:Connect(function(hit) -- Touched
local humanoid = hit.Parent:FindFirstChild("Humanoid") -- Find the Humanoid
if humanoid then -- If the humanoid is a humanoid
print (#starterGreenPads:GetChildren())
humanoid.Jump = true -- humanoid jumps!
end
end)
end
end
Where is the script located?
It only run once so there is no loop so all should be fine
inside the folder
(30 wordles)
Is it printing anything? And also, what is the issue exaclty? Does the player not jump?
Nope. You just need one script that makes use of CollectionService, or, as you do, a folder. Once you’ve iterated through them once to establish connections, there will be negligible lag.
local folder = ...
for i, v, in folder:GetChildren() do
v.Touched:Connect(function()
-- Once this is established, you don't need to run this loop again.
-- Connections remain until the object is destroyed
-- or when they are :Disconnect-ed
end)
end
Also, not sure about Humanoid.Jump
. Perhaps try using: hum:ChangeState(Enum.HumanoidStateType.Jumping)
?
What is the difference between
for i, v, in folder:GetChildren() do
and
for i, pad in pairs(starterGreenPads:GetChildren()) do -- Loop through the StarterGreenPads
?
the player jumps so I don’t thinks that’s an issue
It works fine with a few parts in the folder, but I am wondering if I am able to add close to a thousand parts into the folder without it being slow. Or if i have to make multiple folders and split the parts through them
It will not be slow.
Since it executes just once, it may lag when the game starts, but then it will work just fine.