Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

My code only reads nil from datastore, help?

Asked by
Necro_las 412 Moderation Voter
4 years ago
Edited 4 years ago

Hi guys. Im trying to create a position saver with datastore. This part below is how i saved the data. The second part is where it reads nil aways instead of the saved value.

Why is this happening?

-- saving:
local playerUserId = "Player_"..player.UserId
    local character = sessionData[playerUserId].Char
    sessionData[playerUserId].X = character.HumanoidRootPart.CFrame.Position.X
    sessionData[playerUserId].Y = character.HumanoidRootPart.CFrame.Position.Y
    sessionData[playerUserId].Z = character.HumanoidRootPart.CFrame.Position.Z
    sessionData[playerUserId].x = character.HumanoidRootPart.CFrame.LookVector.X
    sessionData[playerUserId].y = character.HumanoidRootPart.CFrame.LookVector.Y
    sessionData[playerUserId].z = character.HumanoidRootPart.CFrame.LookVector.Z
    print("saved X =",sessionData[playerUserId].X)
        local tries = 0 
        local success
        local errormsg
        repeat
            tries = tries + 1
            success, errormsg = pcall(function() playerData:SetAsync(playerUserId, sessionData[playerUserId]) end)

-- reading:
local data 
    local sussess, errormsg = pcall(function() data = playerData:GetAsync(playerUserId) end)
    if data then -- se tiver
        warn("DataStore downloaded for", player)
        sessionData[playerUserId] = data
        print("reading X =", sessionData[playerUserId].X) -- prints nil

    spawn(function() -- this part is constanty atualizing character info for when player leaves.
        while saving and player.Character do
            if character.Humanoid.SeatPart ~= nil then
                seat = character.Humanoid.SeatPart
            end
            if seat then
                sessionData[playerUserId].Vehicle = character.Humanoid.SeatPart.Parent.Name
            else
                sessionData[playerUserId].Vehicle = "None"
            end
            sessionData[playerUserId].Char = player.Character
            print(character, "saving")
        wait(2) 
        end
    end)

Notes: I checked that "sessionData[playerUserId].Char" is saving corectly with all infos needed. Also I tried putting a generic number instead of Position.X and it keeps returning nil instead of the number, to make sure I can save Position.XYZ in datastore. The "data" variable is sussessfuly returning a table. All the data saved comes just as "nil nil nil nil".

Is it possible to be corrupt data or something like this?

1 answer

Log in to vote
-1
Answered by 4 years ago

Do this script:

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("PositionSave")

game.Players.PlayerAdded:Connect(function(Player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player

    local Pos = Instance.new("CFrameValue")
    Pos.Name = "PlayerPosition"
    Pos.Parent = leaderstats
    Pos.Value = ds1:GetAsync(Player.UserId) or CFrame.new(workspace.StartPart) --Make a Part in Workspace called StartPart and put it where players spawn if it's their first time playing.
    Player.Character.Head.CFrame = CFrame.new(Pos.Value)
    ds1:SetAsync(Player.UserId, Pos.Value)
    print(Player.Name.."'s Position was successfully loaded!"
end)

game.Players.PlayerRemoving:Connect(function(Player)
    Pos.Value = Player.Character.Head.Position
    ds1:SetAsync(Player.UserId, Pos.Value)
    print(Player.Name.."'s Position was successfully saved!")
end)

It may or may not work, by mark as solved if it did! If it didn't get word to me and I'll try help again.

0
you cant save a Vector3 value in datastore =\ Necro_las 412 — 4y
0
and moving a players head like that causes instant death Necro_las 412 — 4y
0
Just try it and I can try something else if it fails JB_SuperGamer 165 — 4y
Ad

Answer this question