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

Why is this Datastore not working? (Answered)

Asked by
Grynn 5
8 years ago

Hello. Currently i am want to set up a Build/Adventure game, where you have to fight agains NPC and mine Ores for getting a Income to Build Buildings. I have set up 2 Scripst for that.

PlayerEntered

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

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

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

    local OreMass = Instance.new("IntValue", stats)
    OreMass.Name = "Mass"

    local Bkills = Instance.new("IntValue", stats)
    Bkills.Name = "Botkills"

    local mined = Instance.new("IntValue", stats)
    mined.Name = "Oresmined"

    local key ="player-"..player.userId

    local savedValues = DataStore:GetAsync(key)

    if savedValues then
        --Save format: (Bkills, mined)
        Bkills.Value = savedValues(1)
        mined.Value = savedValues(2)
    else
        local valuesToSave = {Bkills.Value, mined.Value}
        DataStore:SetAsync(key, valuesToSave)
    end

end)

The Values called Bkills and mined should Save, when the Player is leaving.

Script 2 Player Leaving

l~~~~~~~~~~~~~~~~~ local DataStore = game:GetService("DataStoreService"):GetDataStore("CoreStorage")

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

local key ="player-"..player.userId

--Save key:(Bkills, mined)
local valuesToSave = {player.leaderstats.Botkills.Value, player.leaderstats.Oresmined.Value}
DataStore:SetAsync(key, valuesToSave)

end) ~~~~~~~~~~~~~~~~~

But it wont. So i ask, what did i made wrong.

0
Also sry for my Bad Grammar. I am not that good in speaking English. Grynn 5 — 8y

1 answer

Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

When checking for values in a table, square brackets '[]' should be used, not parentheses. On lines 23 and 24 you used parentheses.

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

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

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

    local OreMass = Instance.new("IntValue", stats)
    OreMass.Name = "Mass"

    local Bkills = Instance.new("IntValue", stats)
    Bkills.Name = "Botkills"

    local mined = Instance.new("IntValue", stats)
    mined.Name = "Oresmined"

    local key ="player-"..player.userId

    local savedValues = DataStore:GetAsync(key)

    if savedValues then
        --Save format: (Bkills, mined)
        Bkills.Value = savedValues[1]
        mined.Value = savedValues[2]
    else
        local valuesToSave = {Bkills.Value, mined.Value}
        DataStore:SetAsync(key, valuesToSave)
    end
end)

Other than that, it looks alright. Edit your question if you have any other issues. Hope this helped!

0
Now its working. Thx for the fast Help. Grynn 5 — 8y
0
No problem. Pyrondon 2089 — 8y
Ad

Answer this question