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?

01-- saving:
02local playerUserId = "Player_"..player.UserId
03    local character = sessionData[playerUserId].Char
04    sessionData[playerUserId].X = character.HumanoidRootPart.CFrame.Position.X
05    sessionData[playerUserId].Y = character.HumanoidRootPart.CFrame.Position.Y
06    sessionData[playerUserId].Z = character.HumanoidRootPart.CFrame.Position.Z
07    sessionData[playerUserId].x = character.HumanoidRootPart.CFrame.LookVector.X
08    sessionData[playerUserId].y = character.HumanoidRootPart.CFrame.LookVector.Y
09    sessionData[playerUserId].z = character.HumanoidRootPart.CFrame.LookVector.Z
10    print("saved X =",sessionData[playerUserId].X)
11        local tries = 0
12        local success
13        local errormsg
14        repeat
15            tries = tries + 1
16            success, errormsg = pcall(function() playerData:SetAsync(playerUserId, sessionData[playerUserId]) end)

01-- reading:
02local data
03    local sussess, errormsg = pcall(function() data = playerData:GetAsync(playerUserId) end)
04    if data then -- se tiver
05        warn("DataStore downloaded for", player)
06        sessionData[playerUserId] = data
07        print("reading X =", sessionData[playerUserId].X) -- prints nil
08 
09    spawn(function() -- this part is constanty atualizing character info for when player leaves.
10        while saving and player.Character do
11            if character.Humanoid.SeatPart ~= nil then
12                seat = character.Humanoid.SeatPart
13            end
14            if seat then
15                sessionData[playerUserId].Vehicle = character.Humanoid.SeatPart.Parent.Name
View all 23 lines...

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:

01local DataStore = game:GetService("DataStoreService")
02local ds1 = DataStore:GetDataStore("PositionSave")
03 
04game.Players.PlayerAdded:Connect(function(Player)
05    local leaderstats = Instance.new("Folder")
06    leaderstats.Name = "leaderstats"
07    leaderstats.Parent = Player
08 
09    local Pos = Instance.new("CFrameValue")
10    Pos.Name = "PlayerPosition"
11    Pos.Parent = leaderstats
12    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.
13    Player.Character.Head.CFrame = CFrame.new(Pos.Value)
14    ds1:SetAsync(Player.UserId, Pos.Value)
15    print(Player.Name.."'s Position was successfully loaded!"
View all 22 lines...

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