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.