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

Can't save/load a table?

Asked by
aiuis 85
6 years ago
Edited 6 years ago
local dss=game:GetService'DataStoreService':GetDataStore'Data'

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local getdata=dss:GetAsync(player.UserId)
        if getdata then
            character.HumanoidRootPart.CFrame=CFrame.new(getdata[1],getdata[2],getdata[3])
        end
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    local savedata={
        player.Character.HumanoidRootPart.Position.X,
        player.Character.HumanoidRootPart.Position.Y,
        player.Character.HumanoidRootPart.Position.Z
    }
    dss:SetAsync(player.UserId,savedata)
end)

I'm trying to get player's x, y and z positions, store them in a table and load them when player joins with them saved. However, I can't make it work and the output says nothing... I'm very green with data stores and I would very appreciate any help.

2 answers

Log in to vote
1
Answered by
Azarth 3141 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

Character is printing nil. You could try tracking your character's position using a value instead and update based on that.

game.Players.PlayerRemoving:Connect(function(player)
    local key = string.format("%s_%s", player.Name, player.UserId)
    if player.Character then 
        local root_part = player.Character:findFirstChild("HumanoidRootPart")
        if root_part then 
            if pcall(function()
                dss:SetAsync(key, root_part.Position)
            end) then 
                print(string.format("Saved %s position: %s", player.Name, root_part.Position))
            else
                print(string.format("Couldn't save %s position.", player.Name))
            end
        else
            print(string.format("Couldn't find %s torso.", player.Name))
        end
    else
        print(player.Name, "/Character is nil")
    end
end)
0
Thanks for the answer but that doesn't seem to be the problem... :/ aiuis 85 — 6y
0
If it's not saving, it's not going to load. Otherwise there is an error and you should be able to see it in the output. StarterServer > 1 player. Leave the window your character has loaded in, look in server output. Azarth 3141 — 6y
0
Thanks a bunch! I got it to work now c: aiuis 85 — 6y
Ad
Log in to vote
1
Answered by 6 years ago

I personally don't have much experience working with data stores, but I can offer some advice that might help out.

  1. According the wiki, data store keys should be strings, but you are passing integers (player IDs) instead. Consider using something like "player" .. player.UserId as a key instead.

  2. Keep in mind that a character's default position may be (unfortunately) assigned after the player.CharacterAdded has been fired, in which case, the position you have assigned to the character will be overridden. There really isn't a clean way to deal with this other than to wait a brief period of time before assigning the character's position so that the character's default position can be assigned first.

  3. Calling GetAsync every time a player's character loads is inefficient. You should only call it once when the player joins the game and then store its value in a variable for later use.

  4. Calls to data store functions can fail due to networking issues. See here for a better explanation.

Hope this helps.

0
I still can't make it to work. Thanks for the point one though, I didn't knew that. I'm pretty sure the problem is at lines 5-8 because if I add a print after line 8 it won't print it... aiuis 85 — 6y

Answer this question