DataStore Set/Save data help!

UpdateEquip.UpdateWeapon = function(player, serial)
	local equipStore = Datastore2("Equipped", player); local equipStoreData = equipStore:GetTable(Equip.EquippedTable)
			
	local equipStoreData = {
		["Weapon"] = serial,
		
	}
	
	equipStore:Set(equipStoreData)
	equipStore:Save() -- Not sure if this is needed
	 
end

So I know I am being dumb right now, but for some reason this doesn’t save the data? am I doing something wrong, I have read that :Set should set the new value, I read this document and looked on this forums and google searched, but for some reason my brain won’t function for some reason, any reason why this doesn’t work? There are no errors. So any help would be appreciated.

Are you activating the function?

Yeah, I added some prints to make sure it got to the function as well. This is the code for it:

Event.OnServerEvent:Connect(function(player, item, animation, frame)
	local char = player.Character or player.CharacterAdded:Wait()
	local tool = item:Clone()
	tool.Name = "EquippedWeapon"
	tool.Parent = char
	char.Animate.toolnone:WaitForChild("ToolNoneAnim").AnimationId = "http://www.roblox.com/asset/?id="..animation
	frame.Parent = frame.Parent.Parent
	frame:TweenPosition(UDim2.new(-1.015, 0, 0.771, 0),"Out","Quart",0.1) -- Side of the screen
	for i, v in pairs(item.Stats:GetChildren()) do	
		if v.Name == "Serial" then
			Update.UpdateWeapon(player, v.Value)
		end
	end
end)

You’re over-writing the equipStoreData variable. You should just need to do equipStoreData.Weapon = serial

So I would do this?

equipStore:Set(equipStoreData.Weapon = serial)

or would I use a variable to set it.

This should work. You can’t assign a variable when calling a function like that.

UpdateEquip.UpdateWeapon = function(player, serial)
	local equipStore = Datastore2("Equipped", player); 
	local equipStoreData = equipStore:GetTable(Equip.EquippedTable)
			
	equipStoreData.Weapon = serial
	
	equipStore:Set(equipStoreData)
	equipStore:Save() -- Not sure if this is needed
	 
end
1 Like

Also, it might be better to save when the player leaves rather than each time they update their weapon. Otherwise, you might reach the limits of DataStore often and it’ll begin to error.

For some reason it doesn’t change the value, the serial is one, but it doesn’t change the table.

Should I change it so it just updates the value instead of saving it, and only save when player leaves.

The only thing is the table doesn’t change, I printed it out and the value remains the same.

You should, yeah. Also, any reason for this part? An item shouldn’t have more than one serial.

	for i, v in pairs(item.Stats:GetChildren()) do	
		if v.Name == "Serial" then
			Update.UpdateWeapon(player, v.Value)
		end
	end

You should be able to just do this

Update.UpdateWeapon(player, item.Stats.Serial.Value)
1 Like

Is there any reason that the value doesn’t update.

The Update.UpdateWeapon(player, item.Stats.Serial.Value) is way better than my current one, thank you for that.

Well, I’m not sure on the structure of your code.

Is Equip.EquippedTable a template for the saved data? How are you checking the player’s data?

This is the table I have currently for the equipped weapons:

Equipped.EquippedTable = {
	
	["Weapons"] = 0, -- Ids of weapon
	["Armour"] = 0,
	["Potions"] = 0,
	
		
}

This is in a module script inside of my datastoreHandler which uses datastore2. Oh I think I just printed the default table twice and not the players table.

Datastore2.Combine("Inventory", "Armour", "Weapons","Stats","Potions","Equipped")

local function GetData(player)
	local armourStore = Datastore2("Armour", player); local armourStoreData = armourStore:GetTable(Armour.ArmourTable)
    local potionsStore = Datastore2("Potions", player); local potionsStoreData = potionsStore:GetTable(Potions.PotionTable)
	local weaponsStore = Datastore2("Weapons", player); local weaponsStoreData = weaponsStore:GetTable(Weapons.WeaponsTable)
	local statsStore = Datastore2("Stats", player); local statsStoreData = statsStore:GetTable(Stats.StatsTable)
	local equipStore = Datastore2("Equipped", player); local equipStoreData = equipStore:GetTable(Equip.EquippedTable)
	
	give.Give("Weapons", Weapons.WeaponsTable, player, Equip.EquippedTable)
	give.Give("Armour", Armour.ArmourTable, player, Equip.EquippedTable)
	give.Give("Potions", Potions.PotionTable, player, Equip.EquippedTable)

	
end

players.PlayerAdded:Connect(GetData)

This is my datastoreHandler.

UpdateEquip.UpdateWeapon = function(player, serial)
    local equipStore = Datastore2("Equipped", player); 
	local equipStoreData = equipStore:GetTable(Equip.EquippedTable)
	print(serial)
	for i, v in pairs(Equip.EquippedTable) do
		print(i..v)
	end
	
	equipStoreData.Weapon = serial
	
	equipStore:Set(equipStoreData)
	equipStore:Save() -- Not sure if this is needed
	 
	for _, c in pairs(Equip.EquippedTable) do
		print(_..c)
	end
	
end

This is what I did to check, but I’m not sure how to check the players value. Would it check the equipStore since that is assigned to the player.

The equipStore is where the player’s data is, yeah. See if this prints the right data:

for i, v in pairs(equipStore:GetTable(Equip.EquippedTable)) do
	print(i, v)
end
1 Like

Yeah, this works, thank you for your help, I will mark your previous reply as solution as that solved the problem to my original problem. Thank you.

1 Like

No problem. Make sure in your GetData function, you’re doing this (could be wrong, but make sure to use the player’s data instead of the template!):

	give.Give("Weapons", Weapons.WeaponsTable, player, equipStoreData)
	give.Give("Armour", Armour.ArmourTable, player, equipStoreData)
	give.Give("Potions", Potions.PotionTable, player, equipStoreData)
give.Give = function(tool, tablearea, player, equiptable) -- Tool being weapon/armour/potions / Tablearea being Weapons.WeaponTable
	
	for num, item in pairs(ReplicatedStorage[tool]:GetChildren()) do  -- Checks all the weapons in the weapons folder
		for serial, name in pairs(tablearea) do -- Checks table
			if item.Name == name["Name"] and name["Quantity"] >= 1 then -- if they own it
				if item.Stats.Serial.Value == serial then -- Checks to see if it's the correct serial
		            for ser, itm in pairs(equiptable) do
						if serial == equiptable[tool] then
							print("You made it here!")
							local weap = item:Clone()
							weap = player.Character
						end
					end
					for i = 1,name["Quantity"] do
						--print("We made it here")
						adding.AddedEvent(item, tool, player)
					end
				end
			end
		end
	end
end

This is the function, so I think I will have to do the players table instead of the template.

edit:
I have changed them to this:

	give.Give("Weapons", weaponsStore:GetTable(Weapons.WeaponsTable), player, equipStore:GetTable(Equip.EquippedTable))
	give.Give("Armour", armourStore:GetTable(Armour.ArmourTable), player, equipStore:GetTable(Equip.EquippedTable))
	give.Give("Potions",potionsStore:GetTable(Potions.PotionTable), player, equipStore:GetTable(Equip.EquippedTable))
1 Like

Yes, you’ll need to make sure you use the player’s data for both tablearea and equiptable. Good luck!

Thank you for your help I really appreciate it. Have a good day.

1 Like