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

How do you use SetAsync and GetAsync values inside folders?

I've visited the wiki, and it never comments on how set or save multiple values inside folders.

local DS = game:GetService("DataStoreService"):GetDataStore("ALPHA_MALE")
local Stands = game:GetService("ServerStorage"):WaitForChild("Stands")


game.Players.PlayerAdded:connect (function(players)
	local PlayerFolder = Instance.new("Folder", Stands)
	PlayerFolder.Name = players.Name
		local StandTypes = Instance.new("Folder", PlayerFolder)
		StandTypes.Name = "STypes"
			local Close_Range = Instance.new("IntValue", StandTypes)
			Close_Range.Name = "CR"
			local Long_Range = Instance.new("IntValue", StandTypes)
			Long_Range.Name = "LR"
			local Automatic = Instance.new("IntValue", StandTypes)
			Automatic.Name = "Auto"
			local Bound = Instance.new("IntValue", StandTypes)
			Bound.Name = "Bound"
			local Colony = Instance.new("IntValue", StandTypes)
			Colony.Name = "Colo"
			local Wearable = Instance.new("IntValue", StandTypes)
			Wearable.Name = "Wear"
			--[[local Uncontrollable = Instance.new("IntValue", StandTypes)
				Uncontrollable.Name = "Control"
			local Dead = Instance.new("IntValue", StandTypes)
				Dead.Name = "Dead"
			local Fusion = Instance.new("IntValue", StandTypes)
				Fusion.Name = "Fusion"
			local Shared = Instance.new("IntValue", StandTypes)
				Shared.Name = "Shared"]]
		local StandStats = Instance.new("Folder", PlayerFolder)
		StandStats.Name = "SStats"
			local Power = Instance.new("IntValue", StandStats)
			Power.Name = "POW"
			local Speed = Instance.new("IntValue", StandStats)
			Speed.Name = "SPD"
			local Range = Instance.new("IntValue", StandStats)
			Range.Name  = "RNG"
			local Durability = Instance.new("IntValue", StandStats)
			Durability.Name = "DUR"
			local Precision = Instance.new("IntValue", StandStats)
			Precision.Name = "PER"
			local Development_Potential = Instance.new("IntValue", StandStats)
			Development_Potential.Name = "LER"
		
end)

Basically, I've inserted two folders inside the player, both of them have multiple values. I need help better understanding on how to Set or Get these values inside the folders!

2 comments

0

I have a script, that I use if you want

0

Use a table.

2 answers

✓ Accepted answer

module script in ServerScriptService

local module = {}
local DataStoreService = game:GetService("DataStoreService") --get DataStoreService
local InventoryItems = DataStoreService:GetDataStore("InventoryItems") --Change it to whatever you want to call it e.g :GetDataStore("examplename")
AUTOSAVE_INTERVAL = 120 --should be no lower than 60

game.Players.PlayerAdded:Connect(function(Player) --When a player is added to the game, load in their data
	local Key = 'PlayerIdNum-'..Player.UserId.."_InvItemKey" --The key that the players data is saved on
	--Values to Save
	local InvData = Player:WaitForChild("Backpack"):WaitForChild("Inventory")
	local Bananas = InvData:WaitForChild("Bananas")
	local Apples = InvData:WaitForChild("Apples")
	local FirstAid = InvData:WaitForChild("FirstAid")
	local Coconuts = InvData:WaitForChild("Coconuts")
	local MetalScraps = InvData:WaitForChild("MetalScraps")
	local Logs = InvData:WaitForChild("Logs")
	local Sticks = InvData:WaitForChild("Sticks")
	--The table that is used to save it after it is all loaded in
	local StoreToSet = {}
	--Get the data and store it
	local DataToGet = InventoryItems:GetAsync(Key) --Returns the data the key stores (as a table)
	--Changing the table back into values
	if DataToGet ~= nil then --If there is a table
		if #DataToGet >= 1 then --If the table length is 1 or more
			print("Has Data")
			--For each value you have, copy the below code (MUST BE IN ORDER OF SAVED IN!!!!!)
			if DataToGet[1] ~= nil then --if the first bit of data in the table does exist
				Bananas.Value = DataToGet[1] --Set the values of bananas as that value
				table.insert(StoreToSet , DataToGet[1]) --Store it in another table so it can be saved
			end
			if DataToGet[2] ~= nil then
				Apples.Value = DataToGet[2]
				table.insert(StoreToSet , DataToGet[2])
			end
			if DataToGet[3] ~= nil then
				FirstAid.Value = DataToGet[3]
				table.insert(StoreToSet , DataToGet[3])
			end
			if DataToGet[4] ~= nil then
				Coconuts.Value = DataToGet[4]
				table.insert(StoreToSet , DataToGet[4])
			end
			if DataToGet[5] ~= nil then
				MetalScraps.Value = DataToGet[5]
				table.insert(StoreToSet , DataToGet[5])
			end
			if DataToGet[6] ~= nil then
				Logs.Value = DataToGet[6]
				table.insert(StoreToSet , DataToGet[6])
			end
			if DataToGet[7] ~= nil then
				Sticks.Value = DataToGet[7]
				table.insert(StoreToSet , DataToGet[7])
			end
		print(DataToGet[1], DataToGet[2], DataToGet[3], DataToGet[4], DataToGet[5], DataToGet[6], DataToGet[7] .. " Data") --Print the data values
		end
	else
		DataToGet = {} --Create a table, since there was no data or table returned in the GetAsync (this only happens with new players)
		DataToGet[1] = Bananas.Value --The first bit of data in the table is equal to Bananas.Value
		Bananas.Value = 0 --Make sure it is 0
		table.insert(StoreToSet , DataToGet[1]) --Insert it into table, so it can be saved, so next time they join, it loads a table
		DataToGet[2] = Apples.Value
		Apples.Value = 0
		table.insert(StoreToSet , DataToGet[2])
		DataToGet[3] = FirstAid.Value
		FirstAid.Value = 0
		table.insert(StoreToSet , DataToGet[3])
		DataToGet[4] = Coconuts.Value
		Coconuts.Value = 0
		table.insert(StoreToSet , DataToGet[4])
		DataToGet[5] = MetalScraps.Value
		MetalScraps.Value = 0
		table.insert(StoreToSet , DataToGet[5])
		DataToGet[6] = Logs.Value
		Logs.Value = 0
		table.insert(StoreToSet , DataToGet[6])
		DataToGet[7] = Sticks.Value
		Sticks.Value = 0
		table.insert(StoreToSet , DataToGet[7])
		print(DataToGet[1], DataToGet[2], DataToGet[3], DataToGet[4], DataToGet[5], DataToGet[6], DataToGet[7] .. " New Data") --Print new data (all should be 0)
	end
	InventoryItems:SetAsync(Key, StoreToSet) --Set the data to the key as a table, so next time they join there is a table to load in
end)

game.Players.PlayerRemoving:Connect(function(Player) --When the player leaves the game, save their data
	local Key = 'PlayerIdNum-'..Player.UserId.."_InvItemKey" --"This is the key you use to save players data on (must be no longer than 50 characters and must be unique)
	--The key is also what you use to load data as well so it MUST be the exact same variable in the other parts of the script
	--Values to Save
	local InvData = Player:WaitForChild("Backpack"):WaitForChild("Inventory")
	local Bananas = InvData:WaitForChild("Bananas")
	local Apples = InvData:WaitForChild("Apples")
	local FirstAid = InvData:WaitForChild("FirstAid")
	local Coconuts = InvData:WaitForChild("Coconuts")
	local MetalScraps = InvData:WaitForChild("MetalScraps")
	local Logs = InvData:WaitForChild("Logs")
	local Sticks = InvData:WaitForChild("Sticks")
	--Put what you need to save here (THE ORDER YOU USE IN THIS IS THE ORDER YOU MUST USE WHEN THEY JOIN)
	local DataToSave = {
		Bananas.Value,
		Apples.Value,
		FirstAid.Value,
		Coconuts.Value,
		MetalScraps.Value,
		Logs.Value,
		Sticks.Value,
	}
	InventoryItems:SetAsync(Key, DataToSave)
end)

--Autosaving
spawn(function()
	while wait(AUTOSAVE_INTERVAL) do
		for i, Player in pairs (game:GetService("Players"):GetChildren()) do
			print("Auto-saving " .. Player.Name .. "'s Data")
			local Key = 'PlayerIdNum-'..Player.UserId.."_InvItemKey"
			local InvData = Player:WaitForChild("Backpack"):WaitForChild("Inventory")
			local Bananas = InvData:WaitForChild("Bananas")
			local Apples = InvData:WaitForChild("Apples")
			local FirstAid = InvData:WaitForChild("FirstAid")
			local Coconuts = InvData:WaitForChild("Coconuts")
			local MetalScraps = InvData:WaitForChild("MetalScraps")
			local Logs = InvData:WaitForChild("Logs")
			local Sticks = InvData:WaitForChild("Sticks")
			local DataToSave = {
				Bananas.Value,
				Apples.Value,
				FirstAid.Value,
				Coconuts.Value,
				MetalScraps.Value,
				Logs.Value,
				Sticks.Value,
			}
			InventoryItems:SetAsync(Key, DataToSave)
			print("Successfully Auto-saved " .. Player.Name .. "'s Data")
		end
	end
end)

return module

script inside the module script (child of it):

require(script.Parent)

5 comments

0

Thanks so much, I def can use this

0

just make sure you read all the comments on the script

0

Accept the answer if it worked for you//helped you solve the problem

0

How does one do that

0

below the question there should be a thing that says "Accept question"

It is IMPOSIBLE to save multiple values with ONE data store UNLESS you save a table

2 comments

0

Thanks for that, alright if I use a table containing the values(DUR,POW, etc. etc.) is it possible to store i(Value name) and v(Value number) inside a folder? (Just for neatness)

0

Could use the scopes for taht.

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