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.
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)
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
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