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

Values are not saving in DataStore?

Asked by
Ben_B 9
4 years ago
Edited 4 years ago

Im trying to make a DataStore for all the values in ReplicatedStorage in my game. I get no error but none of the values save. help

local DataStoreService = game:GetService("DataStoreService")
local inventory = DataStoreService:GetDataStore("Inventory")
local values = game.StarterGui.ShopValues.FirstWalkspeed
local koalaInv = game.ReplicatedStorage.HatValues.KoalaInventoryCount
local koalaQue = game.ReplicatedStorage.HatValues.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
    print("loaded")
        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)

1 answer

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

Hello.

Solution:

Use Player.PlayerGui instead of game.StarterGui as StarterGui is the container for gui you start with. Gui's in StarterGui get cloned into the player's PlayerGui. PlayerGui is the container for the actual gui itself.

Fixed Code:

local DataStoreService = game:GetService("DataStoreService")
local inventory = DataStoreService:GetDataStore("Inventory")

game.Players.PlayerAdded:Connect(function(player)
    local values = player:WaitForChild("PlayerGui").ShopValues.FirstWalkspeed
    local koalaInv = game.ReplicatedStorage.HatValues.KoalaInventoryCount
    local koalaQue = game.ReplicatedStorage.HatValues.KoalaCount

    local Data

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

    end)
    if success then
    print("loaded")
        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 values = player.PlayerGui.ShopValues.FirstWalkspeed
    local koalaInv = game.ReplicatedStorage.HatValues.KoalaInventoryCount
    local koalaQue = game.ReplicatedStorage.HatValues.KoalaCount

    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
Nah it still does not work, its the Values that are not saving :/ Ben_B 9 — 4y
0
??? Ben_B 9 — 4y
0
Oh. youtubemasterWOW 2741 — 4y
0
Are you testing in-game? youtubemasterWOW 2741 — 4y
Ad

Answer this question