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

Why isn't my DataStore and Inventory Saving script working correctly?

Asked by 4 years ago

Alright, so I have a Leaderboard script and it works fine but the problem is, that it's not saving my data or other players data and another problem I'm having is the tools, they're not saving when the player leaves the game after they bought it from my shop gui, I don't know why it's not working properly. please help.

The error I keep getting in my Leaderboard script is this:

ServerScriptService.Leaderboard:17: attempt to index global 'data' (a number value)

My Leaderboard script:

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Currency")

game.Players.PlayerAdded:Connect(function(player)
    local leaderboard = Instance.new("Folder",player)
    leaderboard.Name = "Leaderboard"

    local cash = Instance.new("IntValue",leaderboard)
    cash.Name = "Cash"
    cash.Value = 50

    local recieved,failed = pcall(function()
        data = dataStore:GetAsync(player.UserId)
    end)

    if recieved then
        cash.Value = data[1]
        print("Data was received")
    else
        print("Data was not received")  
    end

    while true do wait(30)
        dataStore:SetAsync(player.UserId, cash.Value)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local leaderboard = player.Leaderboard
    local cash = leaderboard.Cash

    local saved,failed = pcall(function()
        dataStore:SetAsync(player.UserId, cash.Value)
    end)

    if saved then
        print("Data was saved")
    else
        print("Data was not saved")
    end
end)

My Inventory Saving script:

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Currency")

game.Players.PlayerAdded:Connect(function(player)
    local leaderboard = Instance.new("Folder",player)
    leaderboard.Name = "Leaderboard"

    local cash = Instance.new("IntValue",leaderboard)
    cash.Name = "Cash"
    cash.Value = 50

    local recieved,failed = pcall(function()
        data = dataStore:GetAsync(player.UserId)
    end)

    if recieved then
        cash.Value = data[1]
        print("Data was received")
    else
        print("Data was not received")  
    end

    while true do wait(30)
        dataStore:SetAsync(player.UserId, cash.Value)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local leaderboard = player.Leaderboard
    local cash = leaderboard.Cash

    local saved,failed = pcall(function()
        dataStore:SetAsync(player.UserId, cash.Value)
    end)

    if saved then
        print("Data was saved")
    else
        print("Data was not saved")
    end
end)

Any help would be appreciated.

0
Have you tried to remove the [1] on the data saver personally I never seen that been used? thecoolboycool2 18 — 4y
0
@thecoolboycool2 That [1] depends on how data is being saved, but I think he needs to remove it Leamir 3138 — 4y

Answer this question