Why can't I get the character when the player joins the game?

Hey, devforum I am using a game.Players.PlayerAdded RBX script signal. I pass in the param p (player instance) I then set a variable player to it like so player = p. Then I make another variable of character and assign it to player.Character or player.CharacterAdded:wait(). Here’s whats weird is if I run this print(player:GetFullName(), character:GetFullName()) It will return this output Players.ZombieKicker7, ZombieKicker7 and it should output Players.ZombieKicker7, game.Workspace.ZombieKicker7 why is this?
here’s my code;

game.Players.PlayerAdded:Connect(function(p)
	local player = p
	local character = p.Character or p.CharacterAdded:Wait()
	print(player:GetFullName(), character:GetFullName())
end)

btw this is in a module script that is being required in serverScriptService

im not 100% sure since i’ve never really used :GetFullName() but chatacter is a property in the player and it might be printing the property character instead of the actual character instance

‘:GetFullName()’ just gives you the full path to an instance e.g ‘workspace.Part:GetFullName()’ → ‘game.Workspace.Part’. And that property should return the character instance. That property is linked to the character model in workspace.

It’s because module scripts dont work like that. You could either move that onto a normal script [ and it’d work perfectly ]
image

OR

Do the following:

image

image
image

1 Like

Please remember, when you use Module scripts, they don’t do stuff, unless they’re being required on either the client or server.

I assume that CharacterAdded fires before the character is parented in workspace. The reference is the same, but for a tiny fraction of a second, the parent is set to nil before being set to workspace. Although if your script relies to the object parent, you could either wait until the parent is set(repeat task.wait() until character.Parent == workspace) or use the CharacterAppearanceLoaded event instead.

1 Like

Please follow these steps:

  1. Create a normal script into ServiceScriptService / workspace.
  2. Make a PlayerAdded + CharacterAdded event on that script. And inside of the playeradded calll, require the module script.
  3. On the module script, make a function that will print the full name of the player’s ancestry. [make sure to return, so it’d work for every player that will join].

Alright I can incorporate this in my server bootstrapper. Im still very confused on why a Module Script won’t work like this if I’m requiring it from a server script.

It definitely can! Look up at the images I sent to you, we require there the module whenever a player joins the game

I’m confused why as I can’t use the player added and character added RBX script signals in a module script.

Because module scripts dont do anything by themselves. They need to be required by a script or a local script

I think this thread is mainly driven by a misunderstanding making most of the replies irrelevant. You are doing the module script stuff correctly. The issue here is as @NyrionDev says. It’s because characters are briefly parented to nil when they are created. You can see this by running the following:

game.Players.PlayerAdded:Connect(function(p)
	local player = p
	local character = p.Character or p.CharacterAdded:Wait()
	print(player:GetFullName(), character:GetFullName())
	task.wait(2) --Give time for the characters parent to change
	print(player:GetFullName(), character:GetFullName())
end)

The wait is there to give time for the character to be reparented from nil. So the second print, though it is exactly the same will result with what you are looking for. If you need to know exactly when the player gets added to workspace you will need to wait for the parent change.

game.Players.PlayerAdded:Connect(function(p)
	local player = p
	local character = p.Character or p.CharacterAdded:Wait()
	character.AncestryChanged:Wait() --Parent change
	print(player:GetFullName(), character:GetFullName())
end)
2 Likes