I regretfully bought Datastore 2 Confusion

I hired a scripter to make me an inventory and shop system with Datastore 2 and it was… well intentioned :laughing:

Being 6 months into scripting I can’t even begin to read his shorthand scripting. Unfortunately its been hard for him to find the time come finish the changes I need. I’m trying to absorb what I can to read it but I’m still a long ways off.

I have an intVariable called MaxStorage that’s in a folder called PlayerInfo in Player.

I need to do two things. Define MaxStorage using the BackpacksEquipped intValue (also in PlayerInfo) when the data is loaded and changed, but I have no idea what code block to even look in to do this properly. I’d also like to figure out the appropriate way to equip the backpack to the player using the datastore2.

I have a super hacky server script that I was using to define a bool, but its really ineffective and I cannot define player for a .changed:Connect() It tries to run before the datastore finishes loading so it only works with a…frankly embarassing, Wait(12) in the beginning of the function.

Can anyone offer some tips?

local Players = game:GetService("Players")

local function defineMaxValue(player)

--local playerInfo = player:WaitForChild("PlayerInfo")	
	wait(12)
	local maxStorage = player.PlayerInfo:WaitForChild("MaxStorage")
	local backpack = player.PlayerInfo:WaitForChild("BackpacksEquipped")
	local mStorage = Instance.new("IntValue")
	--print("Nick sees backpack is")
	--print(backpack.Value)
	--print("Now DataStore 2 Replaces it and")
	--wait(5)
	--print("Nick sees backpack is now")
	--print(backpack.Value)
	
	if backpack.Value == 1 then maxStorage.Value = 1 end  --WORKS NOW
	if backpack.Value == 2 then maxStorage.Value = 5 end
	if backpack.Value == 3 then maxStorage.Value = 10 end
	if backpack.Value == 4 then maxStorage.Value = 25 end
	if backpack.Value == 5 then maxStorage.Value = 100 end
	if backpack.Value == 6 then maxStorage.Value = 200 end
	if backpack.Value == 7 then maxStorage.Value = 500 end
	if backpack.Value == 8 then maxStorage.Value = 1500 end
	if backpack.Value == 9 then maxStorage.Value = 5000 end	
	
	
end
	
	

local function onPlayerJoin(player)


	--We need to define MaxStorage based on currently equipped backpack and update on .Changed
	--We need to display equipped backpack by inserting accessory into player

	local cd = Instance.new("BoolValue")
	cd.Name = "Cooldown"
	cd.Parent = player
	cd.Value = false

	player.CharacterAppearanceLoaded:Connect(function(defineMaxValue)
		
	end)
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerAdded:Connect(defineMaxValue)

This is the Datastore 2, not the main module, which I assume is associated with this number.
Use require(1936396537) to have an updated version of DataStore2.

I have a feeling somewhere in this shorthand is where I should define MaxStorage appropriately and
reflect a change.

local DataStore2 = require(script.MainModule)
local LeaderStats = require(script.LeaderStats)
														--MaxStorage NEEDS to be determined from EquippedBackpack
local defaultData = {Weapons = 1,Backpacks=1,Gold = 0,Level = 1, Points = 0, MaxStorage=0, Storage=0, Progress1 = 0, Progress2=0, Progress3=0,WeaponsEquipped=1,BackpacksEquipped=1}
local defaultWeapons = {{Parent={Name=1,Value="RedScrapShard"}}} --reset to 0 weapons
local defaultBackpacks = {{Parent={Name=1,Value="Backpack1"}}}  --and backpack come from dialogue progress1 var at 2 and  3 on level 1.
																	--MaxStorage NEEDS to be determined from EquippedBackpack

game.Players.PlayerAdded:Connect(function(plr)
	local PlayerData = {Data = DataStore2("Data", plr)}
	
	local folder = Instance.new("Folder",plr);folder.Name = "PlayerInfo"
	for i,v in pairs(defaultData)do
		local value = Instance.new("IntValue",folder);value.Name=i
		value.Value=v
	end
	
	local function DataUpdate(DataValue)
		local UpdatedData = PlayerData.Data:Get(DataValue)or DataValue
		for i,v in pairs(UpdatedData)do
		folder[i].Value = v
		end
	end
	
	DataUpdate(defaultData)

	PlayerData.Data:OnUpdate(DataUpdate)
	
	local Weapons = function()  --weapons folder
		local folder2 = Instance.new("Folder",folder.Weapons);folder2.Name="WeaponNumbers"

			local Set = DataStore2("WeaponSet",plr)
			
			local UpdateWeapons = function(WeaponValue)
				local WeaponsInSet = Set:Get(WeaponValue)or WeaponValue
				folder2:ClearAllChildren()
			for i,v in pairs(WeaponsInSet)do
					local value = Instance.new("StringValue",folder2)
					value.Name=v["Parent"]["Name"]
				value.Value=v["Parent"]["Value"]
				print(value.Name,value.Value)
						for i2,v2 in pairs(v)do
							print(i2)
							if i2 ~= "Parent" then 
							local value = Instance.new("StringValue",value);
							value.Name=i2;value.Value=v2
							end
						end 
					end
			end
			
		UpdateWeapons(defaultWeapons)
		
			Set:OnUpdate(UpdateWeapons)  
	end
	local Backpacks = function()
		local folder2 = Instance.new("Folder",folder.Backpacks);folder2.Name="BackpackNumbers"

		local Set = DataStore2("BackpackSet",plr)

		local UpdateBackpacks = function(WeaponValue)
			local BackpacksInSet = Set:Get(WeaponValue)or WeaponValue
			folder2:ClearAllChildren()
			for i,v in pairs(BackpacksInSet)do
				local value = Instance.new("StringValue",folder2)
				value.Name=v["Parent"]["Name"]
				value.Value=v["Parent"]["Value"]
				print(value.Name,value.Value)
				for i2,v2 in pairs(v)do
					print(i2)
					if i2 ~= "Parent" then 
						local value = Instance.new("StringValue",value);
						value.Name=i2;value.Value=v2
					end
				end 
			end
		end

		UpdateBackpacks(defaultBackpacks)

		Set:OnUpdate(UpdateBackpacks)
	end
	Weapons()
	Backpacks()	
	LeaderStats.Setup(plr,folder)
	game.Players.PlayerRemoving:Connect(function(plr)
		
		local LeaveData = {}
		for i,v in pairs(folder:GetChildren())do
			LeaveData[v.Name]=v.Value
		end
		PlayerData.Data:Set(LeaveData)
		
		local WeaponsLeaveData = function()
					local folder2 = folder.Weapons.WeaponNumbers
						local Set = DataStore2("WeaponSet",plr)
						local WeaponsTable = {}
			
						for i,v in pairs(folder2:GetChildren())do
							local Table = {}
							for i2,v2 in pairs(v:GetChildren())do
								Table[v2.Name]=v2.Value
							end
							Table["Parent"]={Name=v.Name,Value=v.Value}
							WeaponsTable[#WeaponsTable+1]=Table
						end
					Set:Set(WeaponsTable)
		end
		local BackpacksLeaveData = function()
			local folder2 = folder.Backpacks.BackpackNumbers
			local Set = DataStore2("BackpackSet",plr)
			local BackpacksTable = {}

			for i,v in pairs(folder2:GetChildren())do
				local Table = {}
				for i2,v2 in pairs(v:GetChildren())do
					Table[v2.Name]=v2.Value
				end
				Table["Parent"]={Name=v.Name,Value=v.Value}
				BackpacksTable[#BackpacksTable+1]=Table
			end
			Set:Set(BackpacksTable)
		end
		BackpacksLeaveData()
		WeaponsLeaveData()
	end)
end)

I’m open to any suggestions on my hack script to make it more effective and capable to connect when changed but I also thought I would post the Datastore script as well. :peace_symbol:

I suggest you break this problem into parts.
Part 1. Find out what is contained in the playerInfo folder after the player has appeared.
You should be able to do that simply by using the Explorer and looking in the player in workspace.
Once that is done you can concentrate on just getting the value of the BackpacksEquiped intValue using a print in a simple script.
There are a few posts that have been raised asking How do I know when the player has appeared in workspace? so do a search for that and create a simply script to do the print.
Please let me know if this helps and how you got on.

Thank you for the suggestion. Unless I do a long wait, the datastore 2 replaces the variable that I need to read to find the proper value, and the variable that I need that stores the proper value. So unfortunately waiting for character to appear in the workspace isn’t a long enough wait.

Could you create an object in the PlayerInfo folder indicating that the datastore process has completed and then where you want to wait do so until that object appears.
For example after the for loop of UpdatedData in datastore playeradded add
local datastoreCompleted = Instance.new(“BoolValue”,PlayerInfo);datastoreCompleted.Name =“datastoreCompleted”;datastoreCompleted.Value = true

1 Like

That’s brilliant! I will try that a little later.