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

HELP WITH DATA STORES? [WONT WORK, PLEASE HELP!]?

Asked by
Oficcer_F 207 Moderation Voter
5 years ago
Edited 5 years ago

ERROR: Attempt to index local 'savedValues' (a number value)?

DataStore = game:GetService("DataStoreService"):GetDataStore("PeasStore")


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

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

local points = Instance.new("IntValue", stats)
points.Name = "Points" 

local coins = Instance.new("IntValue", stats)
coins.Name = "Coins" 

local key = ("player-" .. player.UserId)
local savedValues = DataStore:GetAsync(key)

if savedValues then
    points.Value = savedValues[1]
    coins.Value = savedValues[2]
else
    local valuesToSave = {points.Value}
    DataStore:SetAsync(key, valuesToSave)





end

end)

Error at line 19

``PLEASE HELP ME!

0
Line 22. You make a table with curly braces {} not brackets () User#19524 175 — 5y
0
Still doesnt work. Oficcer_F 207 — 5y
0
try dis: points.Value = savedValues[1] , coins.Value = savedValues[2] mixgingengerina10 223 — 5y
0
That is what I wrote.. Oficcer_F 207 — 5y
View all comments (5 more)
0
No commas... See before you judge mixgingengerina10 223 — 5y
0
So? Did it work or what? mixgingengerina10 223 — 5y
0
Hello?? mixgingengerina10 223 — 5y
0
I will try. But how will I write it? Oficcer_F 207 — 5y
0
I wrote it with commas, and on the same line. But I just got an error. Thanks for the help anyways! :) Oficcer_F 207 — 5y

1 answer

Log in to vote
1
Answered by
zblox164 531 Moderation Voter
5 years ago
Edited 5 years ago

You need to save when the player leaves the game too. I also added some things to the script that will help.

Try this:

-- Define variables
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

-- You should put your data store as a separate variable then the service one 
local DataStore = DataStoreService:GetDataStore("NameHere")

-- Loads the data back to the player
local function Load(plr)
    -- Creates the stats
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr

    local points = Instance.new("IntValue")
    points.Name = "Points"
    points.Parent = leaderstats

    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Parent = leaderstats

    -- Don't give a value yet
    local SavedStats
    local Key = "plr-"..plr.UserId -- key

    pcall(function() -- Stops errors
        -- Data Stores can error so thats why I did this
        SavedStats = DataStore:GetAsync(Key)
    end)

    -- Gives the player the data if they have data else create a datastore for them
    if SavedStats then
        points.Value = SavedStats[1]
        coins.Values = SavedStats[2]
    else
        local Values = {
            points.Value;
            coins.Value
        }

        pcall(function() -- Stops errors
            DataStore:SetAsync(Key, Values)
        end)
    end
end

-- Saves on leaving
local function Save(plr)
    local Key = "plr-"..plr.UserId

    local Values = {
        plr.leaderstats.Points.Value;
        plr.leaderstats.Coins.Value
    }

    pcall(function() -- Stops errors
        DataStore:SetAsync(Key, Values)
    end)
end

Players.PlayerAdded:Connect(Load)
Players.PlayerRemoving:Connect(Save)

I know it may look intimidating since it has 63 lines but it's just an upgraded version to what you had. Also make sure you have datastore enabled for your place. If you don't you can enable it by going to your place and clicking on the tree dots. Next press configure this place. Click on Games. Then press the Allow this place to be updated using the Save Place API in your game. checkbox.

Hope this helps!`

0
Could you explain what the ';' does at line 53? Thanks! Oficcer_F 207 — 5y
0
Semi-colon Oficcer_F 207 — 5y
0
The semi colon does the same thing as a comma in this case. You can use commas instead of the semi colon (in this case). zblox164 531 — 5y
0
Ok thanks! Oficcer_F 207 — 5y
View all comments (2 more)
0
Could you please edit out the pcalls, they seem to not work for me. Then I will accept the answer, thanks! Oficcer_F 207 — 5y
Ad

Answer this question