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

How to save more than one data in one script?

Asked by 4 years ago

I'm really interested in datastores, and for my game i need to save multiple things. Tables came to my mind.

It did not work.

"Does anyone out there know how to save multiple data in one script?"

HERES MY FAILED ATTEMPT TO SAVE MULTIPLE DATA;

the only difference here is on line 35 and 36 and on line 50 and 51.

-- services --

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

-- variables

local RemoteEvent = game.ReplicatedStorage.RemoteEvent

-- leaderstats

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

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

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

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

    -- getasync functions

    local PlayerUserId = "Player_"..player.UserId

    local Data
    local sucess, errormessage = pcall(function()
        Data = SaveGameData:GetAsync(PlayerUserId)
    end)

    -- setting data

    if sucess then
            SavedGames.Value = Data
            OtherData.Value = Data
        end


    end)

    -- when the player leaves do

    game.Players.PlayerRemoving:Connect(function(player)
        -- setasync functions

        local PlayerUserId = "Player_"..player.UserId

        local Data = {
        Games = player.leaderstats.SavedGames.Value;
        OtherData = player.leaderstats.SavedGames.Value;
    }

        local sucess, errormessage = pcall(function()
            SaveGameData:SetAsync(PlayerUserId, Data)
        end)

        -- if the data saved

        if sucess then
            print("Data saved sucessfully")
        else
            print("Data did not save because...")
            warn(errormessage)
        end
    end)

1 answer

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

Hello. You need the name of the dictionary's value. For example, instead of writing SavedGames.Value = Data you'd write SavedGames.Value = Data.Games. Try this:

-- services --

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

-- variables

local RemoteEvent = game.ReplicatedStorage.RemoteEvent

-- leaderstats

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

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

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

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

    -- getasync functions

    local PlayerUserId = "Player_"..player.UserId

    local Data
    local sucess, errormessage = pcall(function()
        Data = SaveGameData:GetAsync(PlayerUserId)
    end)

    -- setting data

    if sucess then
        if Data then
             SavedGames.Value = Data.Games
                OtherData.Value = Data.OtherData
        end
else
    warn(errormessage)
        end


    end)

    -- when the player leaves do

    game.Players.PlayerRemoving:Connect(function(player)
        -- setasync functions

        local PlayerUserId = "Player_"..player.UserId

        local Data = {
            Games = player.leaderstats.SavedGames.Value;
            OtherData = player.leaderstats.SavedGames.Value;
    }

        local sucess, errormessage = pcall(function()
            SaveGameData:SetAsync(PlayerUserId, Data)
        end)

        -- if the data saved

        if sucess then
            print("Data saved sucessfully")
        else
            print("Data did not save because...")
            warn(errormessage)
        end
    end)

Please upvote and accept this answer if it helps.

0
This will work, but you should also add an if statement to see if they actually have data when trying to load, because if a new player joins the game and they don't have any data, it will error. xInfinityBear 1777 — 4y
0
Yes, do that too. youtubemasterWOW 2741 — 4y
0
Okay, thanks for replying! maxpax2009 340 — 4y
0
is it common that it does not work in studio? and i have got API on. maxpax2009 340 — 4y
View all comments (2 more)
0
Yes, it is common. youtubemasterWOW 2741 — 4y
0
Okay, thanks! No wonder you have so much reputation. maxpax2009 340 — 4y
Ad

Answer this question