I am getting the error: “LoadCharacter can only be called when player is in the world” even after i have check the player exists, here is the code
if player then
player:LoadCharacter()
player.Character.HumanoidRootPart.CFrame = targetSpawner.CFrame
end
so to my knowledge, player should be nil if the player is not in the world, why would i get this error?
3 Likes
Player is the actual players instance. You want to check for the character, replace your if statement with this:
if player and player.Character then
1 Like
Are you sure you identified the player from before?
will give that a shot thanks for the quick response
1 Like
yeah i did pass the player in, it usually works it only errors occasionally so i thinks it happening when players leave the game before the respawn delay.
This is the whole function, wiht the suggested change above.
--// CustomSpawn
function PlayerSpawnService:CustomSpawn(player)
local spawnGroupName = PlayerSpawnService.PlayerSpawnSettigns[player.UserId].CurrentSpawn
local spawnerGroup = PlayerSpawnService.SpawnerGroups[spawnGroupName]:GetChildren()
local randPick = math.random(1, #spawnerGroup)
local targetSpawner = spawnerGroup[randPick]
wait(respawnDelay)
if player then
player:LoadCharacter()
player.Character.HumanoidRootPart.CFrame = targetSpawner.CFrame
end
end
1 Like
So the error occurs when the player leaves before the delay is done, right?
Did you pass the player argument in when firing this?
BTW that wasnt right, you cant check for the character before its loaded, that statement would always be false.
i cant be 100% sure that when i get the error but its probably the case since the respawn delay seems to be enough time for a player to leave, (like after dying).
otherwise i’m certain i pass the player object in, but i guess i could perform another check at the head of the function.
like maybe the player object is nil when the function fires?
Yes I do see it, but when you fire this argument (Handler:CustomSpawn(player
)
1- Are you sure you passed it there?
2- This is a second option if so, try printing. Use print everywhere, this is called the process of debugging.
It’ll look something like this: print(player.Name)
and if the player is actually there, then it’d print his name, otherwise it’ll give an error.
sure i know, but the statement is only supposed to run if the player is there:
if player then
player:LoadCharacter()
player.Character.HumanoidRootPart.CFrame = targetSpawner.CFrame
end
very simply, if player is nil, that block should not run.
I can add a print I know, but I would have to be in a server when it happens to see the print and the error coinciding, so that wont be a very effective way to solve this right now.
Please put a print(player.Name)
before the wait. It’ll do this whole thing for you.
Try this sanity check.
if player.Parent ~= game.Players then
local p = game.Players:WaitForChild(player.Name, 5)
if not p then warn(player.Name .. " not loaded in world") return false end
end
1 Like
i don’t think you understand my last post. This is happening in a LIVE GAME and it only happens occasionally.
I added the print, but my question remains, if player is nil, then that block should not run.
print aside, thats how lua works as far as i know.
1 Like
i will try that, but when is a player ever not parented game.Players?
I want to understand whats going on here, does the player object parent ever get changed? i never saw that before.
1 Like
I just searched for the problem, which other devs have had. They said the cause was that player was not in the game, either not fully loaded in or just leaving. I think the odds are impractical, but they claimed that this was their problem. I wrote that code though, I didn’t get it from them.
1 Like
i added that block of code and will continue to test, thank you! I will mark your response as the solution for now.
thanks everyone!
Did it end up fixing the problem? Ive had a similar one
yes that did fix it so far, though my error was intermittent so im not 100% sure yet