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

Why is the datastore save flipping values?

The time I first went on, I had 50 iron, and 2 gold. The next time, now I have 50 iron and 2 tin. The gold somewhat changed to tin, and I can't find the problem whatsoever.

This is what i'm talking about here Look at this: LINK The location of those is at ReplicatedStorage, if it helps.

SCRIPT BELOW

local DSService = game:GetService('DataStoreService'):GetDataStore('stpre')
local Items = game.ReplicatedStorage.CONFIG.Materials:GetChildren()
game.Players.PlayerAdded:connect(function(plr)
	
	local key = 'id'..plr.userId
	
	local savedvalues = DSService:GetAsync(key)
	
	local main = Instance.new('Model', plr)
	main.Name = 'inventory'
	local inv = Instance.new('Model', main)
	inv.Name = 'materials'
	local wepinv = Instance.new('Model', main)
	wepinv.Name = 'weapons'
	local utils = Instance.new('Model', main)
	utils.Name = 'utilities'
	
	for _,v in pairs(Items) do
		local ins = Instance.new('IntValue', inv)
		ins.Name = v.ItemName.Value
	end	
	
	if savedvalues then
		for _,v in pairs(Items) do
			for _,vv in pairs(inv:GetChildren()) do
				if vv.Name == v.ItemName.Value then
					vv.Value = savedvalues[v.ID.Value]
				end
			end
		end
	else
		local vts = {}
		for _, v in pairs(Items) do
			for _, vv in pairs(inv:GetChildren()) do
				if vv.Name == v.ItemName.Value then
					table.insert(vts,vv.Value)
				end
			end
		end
		DSService:SetAsync(key, vts)
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local key = 'id'..plr.userId
	local items = plr.inventory.materials
	local vts = {}
	for _, v in pairs(Items) do
		for _, vv in pairs(items:GetChildren()) do
			if vv.Name == v.ItemName.Value then
				table.insert(vts,vv.Value)
			end
		end
	end
	DSService:SetAsync(key, vts)
end)

1 answer

✓ Accepted answer

I would have uploaded the code for the problem. but here is a reason I can guess with the information you gived. When you get list of items using GetChildren() and its getting the childrens over again but they are not inorder cause when u create new lets say Model with name of A1 and other one A2 then you save them in a table Example

local oldMaterials = {A1 =, A2, A3}
 same has Old but this time lets say new Model was added Named AB1
local newMaterials = {AB1, A1, A2, A3}

When a new object or a Name that is Alpbatical order starting with A if their no letter after A then it  order letters then Numbers A0  becomes first unless their was AA or AA0 or AB or AB0  my point is you 
might want to create a order list that will load them inorder when saving and loading.
I could create a simple/complex script for that but I am sure their a better way of doing this I am kinda new to Roblox LUA. 

4 comments

0

Is there any ways to counter this? This is gonna be a problem to me :/

0

Do you want the database to save the highest amount of ore they have first ? if so just get their ore amount then set that as PlayerData[ORE AMOUNT] = {["Type"] = "ORE NAME", ["Amount"] = AMOUNT HERE}

0

Nevermind, i figured it out :D

0

I am new to Roblox LUA the first time I started using DataStore I had the same thing happen to me. If u need any more help just ask.

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