I wanted to move all the players character into one folder so that it will be easier to access and would avoid their models from clashing with my other models
This isn’t very well documented, but I’m pretty sure one of Roblox’s core scripts change the character’s ancestry afterCharacterAdded fires. Theoretically, we should be able to yield the script until the core script does the reparenting:
local plrs = game:GetService'Players';
local folder = workspace:WaitForChild'players';
plrs.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
char.AncestryChanged:Wait(); --this line waits for the core script to do the reparenting
char.Parent = folder;
end);
end);
But this doesn’t work. It seems that the reparenting still occurs too early to prevent the core script from overriding it, so all I can think of is repeatedly setting the parent until the change actually occurs:
local plrs = game:GetService'Players';
local runSvc = game:GetService'RunService';
local folder = workspace:WaitForChild'players';
plrs.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
repeat
char.Parent = folder;
runSvc.Heartbeat:Wait();
until char.Parent == folder;
end);
end);
This is still a bit hacky (at least not as hacky as adding static yields to your code) but should never fail. I should note that, like @UnTagifier said, reparenting characters should be extremely unnecessary in almost all cases where it may seem useful. If you tell me your use case, I can probably provide a better alternative.