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

Help with simple DataStore error? Not saving data?

Asked by
FiredDusk 1466 Moderation Voter
8 years ago
Edited 8 years ago

**My error: ** attempt to index local 'SavedData' (a number value)

Error is on line 15*

It's not saving data, most likely from the 1 error.

local PlayerData = game:GetService('DataStoreService'):GetDataStore('PlayerData')

game.Players.PlayerAdded:connect(function(Player)

    local Stats = Instance.new('Folder', Player)
    Stats.Name = 'Stats'

    local Coins = Instance.new('IntValue', Stats)
    Coins.Name = 'Coins'

    --//DataStore
    local Key = 'id-' ..Player.userId
    local SavedData = PlayerData:GetAsync(Key)
    if SavedData then
        Coins.Value = SavedData[1]
    else
        local DataToSave = {Coins.Value}
        PlayerData:SetAsync(Key, DataToSave)
    end

    Player.CharacterAdded:connect(function(Character)
        Character:WaitForChild("Animate"):remove()
    end)
end)

game.Players.PlayerRemoving:connect(function(Player)
    local Key = 'id-' ..Player.userId
    local SaveData = Player.Stats.Coins.Value
    PlayerData:SetAsync(Key, SaveData) 
end)

2 answers

Log in to vote
1
Answered by 8 years ago

Line 15 should be: Coins.Value = SavedData; so in all it should be:

local PlayerData = game:GetService("DataStoreService"):GetDataStore("PlayerData");

game.Players.PlayerAdded:connect(function(player)
    local Stats = Instance.new("Folder", player);
    Stats.Name = "Stats";

    local Coins = Instance.new("IntValue", Stats);
    Coins.Name = "Coins";

    local Key = "user_" .. player.UserId;

    local SavedData = PlayerData:GetAsync(Key);

    if SavedData then
        Coins.Value = SavedData;
    else
        local Data = {Coins.Value};
        PlayerData:SetAsync(Key, Data);
    end

    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Animate"):Destroy();
    end);
end);

game.Players.PlayerRemoving:connect(function(player)
    local Key = "user_" .. player.UserId;
    local Data = player.Stats.Coins.Value;
    PlayerData:SetAsync(Key, Data);
end);
0
I watched a youtube tutorial on how to make this DataStore but Idk why they put "SavedData[1]" FiredDusk 1466 — 8y
Ad
Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

The problem is that you are mixing the save types, in some places you save the data as a table and in others you just save it as a single value hence the error. As the data would be saved when a player leaves the game as a single value.

This example saves the data in a array and accesses it as a array:-

local PlayerData = game:GetService('DataStoreService'):GetDataStore('PlayerData')

-- it is best to make a variable of the key prefixes so you do not confuse them 
local keyPrefix = 'id-'

-- you could also make a function to return a key 

game.Players.PlayerAdded:connect(function(Player)

    local Stats = Instance.new('Folder', Player)
    Stats.Name = 'Stats'

    local Coins = Instance.new('IntValue', Stats)
    Coins.Name = 'Coins'

    local SavedData = PlayerData:GetAsync(keyPrefix .. Player.UserId) -- this should also be UserId
    if SavedData then
        Coins.Value = SavedData[1]
    else
        PlayerData:SetAsync(keyPrefix .. Player.UserId, {Coins.Value}) -- save as an array
    end

    Player.CharacterAdded:connect(function(Character)
        Character:WaitForChild("Animate"):Destroy() -- you should not be using remove as it is depricated
    end)
end)

game.Players.PlayerRemoving:connect(function(Player)
    PlayerData:SetAsync(keyPrefix .. Player.UserId, {Player.Stats.Coins.Value})  -- save as an array
end)

You will need remove the loading data part as this will error as you currently have a single value, you can also use a different key. I hope this helps.

Answer this question