Unable to change player's character parent

Hi! I am having trouble changing my player’s character parent and I am unsure why is that so

game.Players.PlayerAdded:Connect(function(Player)
	
	Player.CharacterAdded:Connect(function(Character)
		
		Character.Parent = workspace.Players
		
	end)
	
end)

I have came across similiar post about this but I still do not understand why this method is flawed

  1. Why do you need to change the parent?

  2. If there is no need for association between the character and player, then just clone the character, and parent it to another place.

  3. You could also try removing the humanoid, but that would probably just kill the player.

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

  1. What do you mean by clashing

  2. You can just check if the model has a humanoid, or add tags to player characters when they join.

You could use :GetPlayerFromCharacter, and add tags to the players.

ohh tks, but may I know why the method I used isnt usable?

This isn’t very well documented, but I’m pretty sure one of Roblox’s core scripts change the character’s ancestry after CharacterAdded 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.

3 Likes

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