GetPlayerFromCharacter returns character is not a valid member of workspace

I want to know how to fix this issue, how to wait until the character before the GetPlayerFromCharacter works, this is my code in a normal script under ServerScriptService.
local Players = game:GetService("Players") local character = workspace.RoyaleHigh_KingFB local playerFromCharacter = Players:GetPlayerFromCharacter(character) if playerFromCharacter then print(playerFromCharacter.Name) end
this returns in the output RoyaleHigh_KingFB is not a valid member of Workspace "Workspace" about the second line of code

after my character loads and stuff while i’m testing the game, i put the exact same thing in the output and it returned “RoyaleHigh_KingFB” which means it work.

so how do i wait for the character to load before the GetPlayerFromCharacter fires?
I’ve tried multiple ways like CharacterAdded and functions but I still get the same output.

You have to wait for it to exist.

workspace:WaitForChild("RoyaleHigh_KingFB")
1 Like

If it’s possible for you to do this for what you’d like to use your script for, placing your server Script inside of StarterCharacterScripts and writing:

local Players = game:GetService("Players")

local character = script.Parent

local player = Players:GetPlayerFromCharacter(character)

print(player.Name)

should work to solve your problem

is GetPlayerFromCharacter generally used for StarterCharacterScripts tho? im learning

The :GetPlayerFromCharacter method can be used anywhere really either as a check to see if the character is a player or an NPC or to fetch the player that’s controlling the character

I found the method I recommended to be a very convenient way to run server-side code which is guaranteed to run only after the player’s character has finished being created so you won’t have to worry about CharacterAdded or CharacterRemoving

It can be used in any script, really. It’s a neat way to get the player object from a player’s character.

then how do i make it wait until the character has loaded in?

Scripts placed inside of StarterCharacterScripts are automatically placed inside of the character and ran by the game engine

The script will even stop and run again if the character dies or resets so you won’t have to worry about that either

Edit: @RoyaleHigh_KingFB I made you this example place you can use to show that you won’t have to worry about waiting for the character to load when using my method: GetPlayerFromCharacterExample.rbxl (52.4 KB)

Try resetting and you’ll see that the player’s name is printed again, which means the script has successfully ran again automatically after the player’s new character has finished loading

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