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

Value2 keeps overwriting both values, is there something I'm missing?

This is the code I use to save in a datastore, the prints were to check what was going on & when Value was saving it was completely fine, but after Value2 saved it not only overwrote Value2 (which is meant to happen) but it overwrote Value aswell (which shouldn't happen)

if stat.Parent.Name:match("Form") then
				if stat.Name == "Value" then
					Data[stat.Parent.Name] = stat.Value
					print(stat.Name.." "..stat.Value.." "..stat.Parent.Name)
				end
				if stat.Name == "Value2" then
					Data["Hair "..stat.Parent.Name] = stat.Value
					print(stat.Name.." "..stat.Value.." "..stat.Parent.Name)
				end
			end
			print(Data["Hair Form3"])
		print(Data["Form3"])

(In studio screenshot: https://gyazo.com/06f164a9b6113944985f6f61ec36fe18)

I need to know if this is something with roblox or something that I'm missing cause it has had me stuck for a few days.

1 answer

You can't save more than 1 value in a datastore. Assembly that in a table, encode it into JSON formatted string and save it, and then get it from the datastore, decode it from JSON formatted string and make the values of the table overwrite the current values just like that: (I don't get where your values are located, so I'll just get a template and you can make your own one). Here is the script:

local HttpService = game:GetService("HttpService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TableTestDataaStore")
game.Players.PlayerAdded:Connect(function(player)
	local key = player.Name.."-"..player.UserId
	local encodedData = DataStore:GetAsync(key) -- This might give an error if it's the first time a player joins the game, so make sure this is only a script with this code. But atleast it works.
	local data = HttpService:JSONDecode(encodedData)
	-- Set the values from the decoding
	value1goeshere = data[1]
	value2goeshere = data[2]
	print("Loaded all the values")
end)
game.Players.PlayerRemoving:Connect(function(player)
	local data  = {
    	value1goeshere,
	value2goeshere
	}
	local key = player.Name.."-"..player.UserId
	local normalData = HttpService:JSONEncode(data)
	DataStore:SetAsync(key,normalData)
end)

I hope this worked.

1 comment

0

The values are both in a table, the code I posted was just to try get the values saved into the actual datastore (Data is just a table for reference)

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