player.Character = nil

I am trying to make a position saving system using suphis datastore module. It works fine in studio but when testing it in the real game im getting the error attempt to index nil with humanoid root part. I put a few print statements in the code to see if I could track down the problem and found that player.Character = nil. I know for a fact the character exists when this code runs because the code only runs when you press a play button long after you have already joined the game. Here is the code I am struggling with


local function LoadPosition(player)
	local dataStore = DataStore.find("Player", player.UserId)
	if dataStore == nil then print("dtaastore dont exist") end
	if dataStore.State ~= true then print("state dont true") end
	
	if dataStore.Value.EverLoadedIn == false then
		print(game.Workspace.Bed.TestBed.CFrame.Position)
		print(dataStore.Value.EverLoadedIn)
		print(player.Character.HumanoidRootPart.Position)
		player.Character.HumanoidRootPart.Position = game.Workspace.Bed.TestBed.CFrame.Position
		dataStore.Value.EverLoadedIn = true
		dataStore.Value.CurrentlyLoaded = true
	elseif dataStore.Value.EverLoadedIn == true then
		print(player)
		print(player.Character) --it errored on this line.
		print(player.Character.HumanoidRootPart)
		player.Character.HumanoidRootPart.Position = Vector3.new(dataStore.Value.Cframe.Position.x, dataStore.Value.Cframe.Position.y, dataStore.Value.Cframe.Position.z)
		--dataStore.Value.CurrentlyLoaded = true
	end

end

Also, I am not sure if this helps but the first time you join it doesn’t give this error. It only gives the error if you leave and rejoin.

2 Likes

Have you tried to add a Wait function or wait until your character has loaded in? it could be that the script is running so fast it can’t detect the player when they joined.

This is most likely a race condition. You said it worked the first time you join, this is most likely because the script has to go through multiple lines before it reaches the player.character script. The 2nd time it doesn’t work is because the script no longer has to pass through those couple like before it is on the Player.Character line and at the same time the script runs faster than the player able to spawn the character.

What you can do is try getting the character again or load the character manually.

You might wanna put:
local character = player.Character or player.CharacterAdded:Wait()

2 Likes

hmm. I seem to get the error, HumanoidRootPart is not a valid member of model “Workspace.ordinalDerp_2024”(username). Any ideas?

just keep on adding a wait like:

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitforChild("HumanoidRootPart")

in that way, it will wait for the script for the HumanoidRootPart to load before running the script

i managed to fix this by putting a character removed event in a playeradded event

1 Like

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