Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to save more than one data in one script?

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

✓ Accepted answer

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.

6 comments

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.

0

Yes, do that too.

0

Okay, thanks for replying!

0

is it common that it does not work in studio? and i have got API on.

0

Yes, it is common.

0

Okay, thanks! No wonder you have so much reputation.

This discussion is preserved as it was. New answers, comments, and votes are closed.