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

Datstore Script not Working "attempt to index a number value", what should I do to fix this?

Asked by 5 years ago

For some reason I get
00:10:40.513 - ServerScriptService.Stats:21: attempt to index a number value Pretty strange I dont see what Im doing wrong, Im trying to make it know if the player has played the game before if he did then set his data, or else just make a new one then set it

local DataStoreService = game:GetService("DataStoreService"):GetDataStore("PertendCodeIsHere")

game.Players.PlayerRemoving:Connect(function(LeavingPlayer)
    DataStoreService:SetAsync(LeavingPlayer.userId, LeavingPlayer.stats.Wallet.Value)
    DataStoreService:SetAsync(LeavingPlayer.userId, LeavingPlayer.stats.Bank.Value) 
end)



function onPlayerEntered(newPlayer)
    --Money--

    local stats = Instance.new("IntValue")
    stats.Name = "stats"
    stats.Parent = newPlayer

    local money = Instance.new("IntValue")
    money.Name = "Money"
    money.Parent = stats
    if DataStoreService:GetAsync(newPlayer.userId) ~= nil then
        money.Value = DataStoreService:GetAsync(newPlayer.userId, newPlayer.stats.Money.Value)[1]
    else
        local NewDataBank = money.Value + 50
        DataStoreService:SetAsync(newPlayer.userId, money.Value)
        money.Value = DataStoreService:GetAsync(newPlayer.userId, money.Value)
    end


    local bank = Instance.new("IntValue")
    bank.Name = "Bank"
    bank.Parent = stats
    if DataStoreService:GetAsync(newPlayer.userId, newPlayer.stats.Bank.Value) ~= nil then
        bank.Value = DataStoreService:GetAsync(newPlayer.userId, newPlayer.stats.Bank.Value)[1]
    else
        local NewDataBank = bank.Value + 200
        DataStoreService:SetAsync(newPlayer.userId, bank.Value)
        bank.Value = DataStoreService:GetAsync(newPlayer.userId, bank.Value)
    end


    --Pin Number--
    local PinNumber = Instance.new("StringValue")
    PinNumber.Name = "PinNumber"
    PinNumber.Value = math.random(10000,99999)
    PinNumber.Parent = newPlayer

    --Hunger--

    local Hunger = Instance.new("IntValue")
    Hunger.Name = "Food"

    local money = Instance.new("IntValue")
    money.Name = "Hunger"
    money.Value = 10

    local bank = Instance.new("IntValue")
    bank.Name = "Thirst"
    bank.Value = 10

    money.Parent = Hunger
    bank.Parent = Hunger

    Hunger.Parent = newPlayer

    --Player Can run--
    local CanRun = Instance.new("BoolValue")
    CanRun.Name = "CanRun"

    CanRun.Parent = newPlayer

    newPlayer.CanRun.Value = true

        --Jail--

    local IsInJail = Instance.new("BoolValue")
    IsInJail.Name = "IsInJail"

    IsInJail.Parent = newPlayer

    local TimeLeftInJail = Instance.new("NumberValue")
    TimeLeftInJail.Name = "TimeLeftInJail"
    TimeLeftInJail.Parent = IsInJail

        --JobValues and all that--

    local Amountoftrashthismanpickedups = Instance.new("NumberValue")
    Amountoftrashthismanpickedups.Name = "Amountoftrashthismanpickedups"

    Amountoftrashthismanpickedups.Parent = newPlayer


    local ShirtCharacterIsWearing = Instance.new("StringValue")
    ShirtCharacterIsWearing.Name = "ShirtID"

    ShirtCharacterIsWearing.Parent = newPlayer
    newPlayer.Character.Shirt.ShirtTemplate = "rbxassetid://10248083"
end





game.Players.ChildAdded:Connect(onPlayerEntered)

1 answer

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

Okay so your data store script gave me a headache so heres a new one. Its fairly simple to edit and manage. This script goes in server script service, and then you'll make a folder inside of that script, and inside of that folder you'll add any values you want to save. Make sure its named correctly, its currently set up for "money", and "bank" as values. The folder must be named "stats", unless you change the name stats inside the playeradded function to something else. If you want the player to start off with a value if no save file is detected, make the value of the int inside of the "stats" folder to whatever that default value is. To add more saved information add them inside of the "SaveTable" as I have, and also add them into the "DataInput" function as I have. If you run into and errors or have questions let me know.

Edit:: just to be clear, the structure for the stats would be -- DataStore script > stats(your folder) > money(int), bank(int), and since you have quite a few just add them in as needed and follow the other steps above.

local DataStoreService= game:GetService('DataStoreService'):GetDataStore("PertendCodeIsHere")
local RunService = game:GetService("RunService")
local db = 3

function SaveTableFunc(player)
    local SaveTable = {
        money = player.stats.money.Value,
    bank = player.stats.bank.Value,
    }
    return SaveTable
end

game.Players.PlayerAdded:connect(function(player) 
    repeat wait() until player.Character
    repeat wait() until script:FindFirstChild("stats")
    local save1 = script.stats:Clone()
    save1.Parent = player
    local stats = player.stats
    local save = SaveTableFunc(player)
    local playerkey = "player-"..player.userId
    local SaveFiles = DataStoreService:GetAsync(playerkey)
    function dataInput(player, save)
          stats.money.Value = SaveFiles.money
          stats.bank.Value = SaveFiles.bank
          print("Got saved data for "..player.Name)
    end
    local Passed, Error = pcall(function() 
            if SaveFiles == nil then
                print(player.Name.." has no save data yet")
            else
        dataInput(player, save)
            end
    end)
    if Error then
        print("***ERROR GETTING SAVE FILE FOR "..player.Name)
        local attempts = 0
        while wait(db) do
            local retries = attempts + 1
            attempts = retries
            local pass,err = pcall(function()
            if SaveFiles == nil then
                print(player.Name.." has no save data yet")
        else
        dataInput(player, save)
            end
      end)
            if pass or attempts == 5 then break end
        end
    end
end)

function SaveData(player)
    local Success,Error = pcall(function()
        local playerkey = "player-"..player.userId
        local save = SaveTableFunc(player)
        DataStoreService:SetAsync(playerkey,save)
        print("Saved data for "..player.Name)
    end)
    if Error then
        print("***ERROR SAVING DATA FOR "..player.Name)
        local attempts = 0
        while wait (db) do
            local retries = attempts + 1
            attempts = retries
            local pass,fail = pcall(function()
                local playerkey = "player-"..player.userId
                local data = SaveData(player)
                DataStoreService:SetAsync(playerkey,save)
                print("Successfully saved data for "..player.Name)
            end)
            if pass or attempts == 10 then break end
        end
    end
end

game.Players.PlayerRemoving:Connect(SaveData)

game:BindToClose(function() 
    if RunService:IsStudio() then return end
    local Players = game:GetService("Players")
    for _,player in pairs(Players:GetPlayers()) do
        SaveData(player)
        print("Saved data for "..player.Name.." on shutdown.")
    end
end)


Ad

Answer this question