Hello there, I was making a script that copies the childs in the LocalPlayer’s character which are necessary such as accessories and face. I set the brickcolor and the face but I wasn’t able to set the accessories because it said game script timout in output. Here is the script.
local Dummy = game.Workspace.Dummy --Please don't mind the variable names. I gave them random names to save time
game.ReplicatedStorage.Character.OnServerEvent:Connect(function(plr, Character)
for i,vee in pairs(Character:GetChildren()) do
for z,uuu in pairs(Dummy:GetChildren()) do
if vee.Name ~= uuu.Name then
local eaea = vee:Clone()
eaea.Parent = Dummy
--////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
else --This Part breakes the script
if vee:IsA("MeshPart") or vee:IsA("Part") then
uuu.BrickColor = vee.BrickColor
--////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
end
if vee.Name == "Head" then
for i,decal in pairs (vee:GetChildren()) do
if decal:IsA("Decal") then
local face = decal:Clone()
if uuu.Name == "Head" then
face.Parent = uuu
end
end
end
end
end
end
end
end)
I am not able to include screenshots because studio stops giving response. I tried to change the position of the else. I tried to put the other actions into an else and I put the broken action into the main if. I tried to look at Roblox Wiki but I was not able to get any information. I saw this type of topic but I wasn’t able to get information. I don’t have any loops as you see. I don’t think LocalScript that fires the server is not necessary because it seems like it doesn’t have a problem. I just want to know what causes this else to timout the script.
As described in a topic posted last year the game script timeout error happens whenever a script runs for too long without yielding (returning or simply reaching the end). In this case you are looping through a lot of objects. Not only that, but the script is constantly Creating New Objects (Probably more than necessary).
You could probably do away with a lot of looping if you remove one of the loops and instead use Dummy:FindFirstChild(vee.Name)
local Dummy = game.Workspace.Dummy
game.ReplicatedStorage.Character.OnServerEvent:Connect(function(plr, Character)
for i,vee in pairs(Character:GetChildren()) do
local uuu = Dummy:FindFirstChild(vee.Name)
if not uuu then
local eaea = vee:Clone()
eaea.Parent = Dummy
else
if vee:IsA("MeshPart") or vee:IsA("Part") then
uuu.BrickColor = vee.BrickColor
end
if vee.Name == "Head" then
for i,decal in pairs (vee:GetChildren()) do
if decal:IsA("Decal") then
local face = decal:Clone()
face.Parent = uuu
end
end
end
end
end
end)
I was going to write something else, but I’ll write this instead because I think it’s more likely.
You are using remote functions/events. How fast are you calling this event on the players? (I don’t see anything to check out a specific player) It is probable that you are doing this, or something of the sort, is a LocalScript:
while true do
game.ReplicatedStorage.Character:InvokeServer()
end
Excuse this if it’s not correct my remote stuff is a little rusty. You should instead:
while wait() do
game.ReplicatedStorage.Character:InvokeServer()
end
Or better yet:
while wait(1) do
game.ReplicatedStorage.Character:InvokeServer()
end
Yup, to add on to @mathymods’s point, I think a big problem is that you’re constantly adding new children to Dummy with this line:
eaea.Parent = Dummy
This means that the loop
for z,uuu in pairs(Dummy:GetChildren()) do
will take longer every new time that it’s called. Imagine if Dummy started with 10 children, but if OnServerEvent is called many times, many children are added to Dummy. Eventually, Dummy could possibly have 10 million children. Then the loop above would take a long time to go through all of Dummy’s children, and might timeout every new time that OnServerEvent is called. This is especially likely to happen if the remote event is fired a lot, since every time it’s fired there are children being added (as @OneRubySky mentioned).
Your code is looping a lot more than I thought. You first loop through the Character you are cloning (fair enough), then for every part of the Character you are looping through you loop through the Dummy. Then if the parts are not the same you clone it and make it a subsidiary of the dummy. This however adds another object to the objectlist (And you do this for every single object that’s not the same (#Character-1) * (#Dummy-1)) Then you iterate to the next object inside Character, but now there are more than double the amount of objects inside dummy, multiplying with the former formula.
This gets out of hand QUICKLY.
On an exponential scale this skyrockets and the time exceeds 20 seconds with no problem.
The script I provided earlier avoids this problem by instead of iterating through every object, I search for the second instead.