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

DataStore not saving any of the values?

Asked by
Ben_B 9
4 years ago
Edited 4 years ago

Im trying to save 3 values; 2 in startergui that are IntValues, and one BoolValue inside ReplicatedStorage.

local DataStoreService = game:GetService("DataStoreService")
local inventory = DataStoreService:GetDataStore("Inventory")
local values = game.StarterGui.ShopValues.FirstWalkspeed
local koalaInv = game.StarterGui.HatValues.KoalaInventoryCount
local koalaQue = game.StarterGui.QuestHolder.KoalaCount








game.Players.PlayerAdded:Connect(function(player)
    local Data
    local Data2
    local Data3
    local success, err = pcall(function()
        Data = inventory:GetAsync(player.UserId.."-WalkSpeed", values.Value)
        Data2 = inventory:GetAsync(player.UserId.."-KoalaInv", koalaInv.Value)
        Data3 = inventory:GetAsync(player.UserId.."-KoalaQuest", koalaQue.Value)
    end)
    if success then
        values.Value = Data
        koalaInv.Value = Data2
        koalaQue.Value = Data3

    else
            print("Error getting data")
            warn(err)
    end


end)


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


    local success, err = pcall(function()
        inventory:SetAsync(player.UserId.."-WalkSpeed", values.Value)
        inventory:SetAsync(player.UserId.."-KoalaInv", koalaInv.Value)
        inventory:SetAsync(player.UserId.."-KoalaQuest", koalaQue.Value)
    end)

    if success then
        print ("data saved")
    else
            print("There was an error")
            warn(err)
    end

end)

1 answer

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

Hello.

Problem:

You tried setting the DataStore multiple times at once. This wouldn't work as DataStore's are designed to store 1 value.

Solution:

Instead, save a table containing the values.

Fixed Code:

local DataStoreService = game:GetService("DataStoreService")
local inventory = DataStoreService:GetDataStore("Inventory")
local values = game.StarterGui.ShopValues.FirstWalkspeed
local koalaInv = game.StarterGui.HatValues.KoalaInventoryCount
local koalaQue = game.StarterGui.QuestHolder.KoalaCount

game.Players.PlayerAdded:Connect(function(player)
    local Data

    local success, err = pcall(function()
        Data = inventory:GetAsync(player.UserId.."_Data", values.Value)

    end)
    if success then
        values.Value = Data[1]
        koalaInv.Value = Data[2]
        koalaQue.Value = Data[3]

    else
            print("Error getting data")
            warn(err)
    end


end)


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

    local toSave = {values.Value, koalaInv.Value, koalaQue.Value}


    local success, err = pcall(function()
        inventory:SetAsync(player.UserId.."-Data", toSave)

    end)

    if success then
        print ("data saved")
    else
            print("There was an error")
            warn(err)
    end

end)

0
Would this still work as im saving values in StarterGui? Ben_B 9 — 4y
0
Use PlayerGui instead of StarterGui. youtubemasterWOW 2741 — 4y
0
it doesnt recognise it Ben_B 9 — 4y
Ad

Answer this question