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

DataStoreService not working, can someone explain why?

Asked by
thePyxi 179
7 years ago
Edited 7 years ago

I am attempting to create a data sore script in which to save just three leaderstats. Everything seems to be working fine except when it gets to line 22, where the script crashes. I know I am bad at errors, which I am still learning thoroughly, but can anyone explain to me why this problem occurred?

This is the error:

Workspace.PlayerEntered:22: attempt to index local ‘savedValues’ (a number value)

local DataStore = game:GetService("DataStoreService"):GetDataStore("Stats")

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

    local stats = Instance.new("IntValue", player)  
    stats.Name = "leaderstats"

    local win = Instance.new("IntValue", stats)
    win.Name = "Wins"

    local kill = Instance.new("IntValue", stats)
    kill.Name = "Kills"

    local death = Instance.new("IntValue", stats)
    death.Name = "Deaths"

    local key = "player-"..player.UserId

    local savedValues = DataStore:GetAsync(key)

    if savedValues then
        win.Value = savedValues[1]
        kill.Value = savedValues[2]
        death.Value = savedValues[3]
    else
        local winToSave = (win.Value)
        local killToSave = (kill.Value)
        local deathToSave = (death.Value)
        DataStore:SetAsync(key, winToSave)
        DataStore:SetAsync(key, killToSave)
        DataStore:SetAsync(key, deathToSave)
    end

end)

This code is the save.

local DataStore = game:GetService("DataStoreService"):GetDataStore("Stats")

game.Players.PlayerRemoving:connect(function(player)
    local short = player.leaderstats
    local key = "player-"..player.UserId

    local winToSave = (short.Wins.Value)
    local killToSave = (short.Kills.Value)
    local deathToSave = (short.Deaths.Value)

    DataStore:SetAsync(key, winToSave)
    DataStore:SetAsync(key, killToSave)
    DataStore:SetAsync(key, deathToSave)
end)
0
Did you forget to add a SetAsync() code block? Which saves the code. KingLoneCat 2642 — 7y
0
On line 17 of the first script, and line 5 of your second script, I think it's suppose to be userId, not UserId User#11440 120 — 7y
0
you cant access datastores in studio, unless u go into edit mode so it should work in game QuantumToast 261 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

You're trying to access the values in a table, but you didn't save them in a table.

After line 25 of the first script, you save the values very weird. I would suggest making a table and saving the table to the DataStore, as this would probably fix your problem,

local DataStore = game:GetService("DataStoreService"):GetDataStore("Stats")

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

    local stats = Instance.new("IntValue", player)  
    stats.Name = "leaderstats"

    local win = Instance.new("IntValue", stats)
    win.Name = "Wins"

    local kill = Instance.new("IntValue", stats)
    kill.Name = "Kills"

    local death = Instance.new("IntValue", stats)
    death.Name = "Deaths"

    local key = "player-"..player.UserId

    local savedValues = DataStore:GetAsync(key)

    if savedValues then
        win.Value = savedValues[1]
        kill.Value = savedValues[2]
        death.Value = savedValues[3]
    else
        local SaveValues = {
        win.Value;
        kill.Value;
        death.Value
    }
        DataStore:SetAsync(key, SaveValues)
    end

end)

Now you should do the same for the second script,

local DataStore = game:GetService("DataStoreService"):GetDataStore("Stats")

game.Players.PlayerRemoving:connect(function(player)
    local short = player.leaderstats
    local key = "player-"..player.UserId

    local SaveValues = {
        player.leaderstats.win.Value;
        player.leaderstats.kill.Value;
        player.leaderstats.death.Value
    }

    DataStore:SetAsync(key, SaveValues)
end)

I hope this works and helps.

Good Luck!

Ad

Answer this question