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

Saving Vectors and Loading Vectors not working, any help?? (rep+upvote)

(Rep+Upvote for thorough answer) I am very interested in data stores and I was planning to save a couple of vectors and load a couple of vectors into a vector3 value called SaveRequest.

So far, my script has no errors but my script isn't working either. It prints everything and it even creates multiple SaveRequests just like I want it to but, I am unsure if I am loading it correctly or saving it correctly since Datastore is my weak topic.

things you should know: The player has a folder called HiddenStats The folder has ItemPos in it The SaveRequests are Vectors that go in ItemPos I use a different way to get the player and it works.

Here is the code:

repeat wait() until game:GetDescendants()
wait(.5)
local DataStore = game:GetService("DataStoreService"):GetDataStore("test")
local lastPos = Vector3.new(0,0,0)


-- getting player
for _, v in pairs(workspace:GetChildren()) do
	if v:FindFirstChild("Player") then
		local name = v.Player.Value
		print(name.." has joined the game :D")
		Plr = game.Players:FindFirstChild(name)
	end
end

-- fetching data
print("Player Fetched")
print("Fetching data")
local b = 0
repeat b = b + 1
	wait()
	if Plr then
	local inst = Instance.new("Vector3Value", Plr.HiddenStats.ItemPos)
	inst.Name = "SaveRequest"..b
	DataStore:GetAsync("last"..b)
	print("Datastore "..b)
end
until b == 30
print("Loaded everything")
-- setting data
while wait(5) do
print("Setting Data")
	local a = 0
	if Plr then
		for _, v in pairs(Plr.HiddenStats.ItemPos:GetChildren()) do
			a = a + 1
			print(v.Value)
			lastPos = v.Value
			DataStore:SetAsync("last"..a,tostring(lastPos))
			print("Datastore "..a)
		end
	else
		wait()
	end
	print("Data has been Set")
end

Thank you for any help!

(Rep + Upvote)

3 answers

First of all, just reading through it, you are making WAAAYYYY too many Datastore get's and set's. It has limits that can be found here (about halfway down the page): robloxdev.com/articles/Data-store#Limitations

You want to try to limit the amount of set's, get's and update's your datastore saving technique does or else you'll cap out and it will prevent your entire server from saving until the limits reset.

Here you are running a GetAsync 30 times, if you are in a 1 player game, your max requests would be 70 per minute, you are already using up almost half of them on a single save.

local b = 0
	repeat b = b + 1
	    wait()
	    if Plr then
	    local inst = Instance.new("Vector3Value", Plr.HiddenStats.ItemPos)
	    inst.Name = "SaveRequest"..b
	    DataStore:GetAsync("last"..b)
	    print("Datastore "..b)
	end
	until b == 30

Next, from what it looks like you are trying to save a Vector3 value. You can't save that type of data in a Datastore. They can only save Booleans, Integers, Numbers, Strings, and Tables. So in order to save a Vector3, you would need to extract the X,Y,Z and save it in either a string or table.

5 comments

0

So then how can I save it more efficiently??? and make it work because I have a much much much larger script that doesn't work either

0

I need some examples dude

0

And by the way I am using ToString to save the vectors

0

Am I right in saying you are saving all the player's data in seperate keys for example you save the postion in one datastore key and maybe the player name in another?

0

If you are, you want to structure your save like this Datastore:SetAsync(key,{pos,name,otherdata,moredata}) So then when you load it, you only need 1 load, and when you save it, you only need 1 save.

Hey there!

You cannot save Vector3s directly to DataStores, instead you should store their components in an array.

Example:

local vec = Vector3.new(5, 2, 3)

--defining datastore
local dss = game:GetService("DataStoreService")
local store = dss:GetDataStore("ExampleStore")

--saving v3 as table
store:SetAsync("testvec", {vec.X, vec.Y, vec.Z})

--retrieving table
local data = store:GetAsync("testvec")

--converting table to v3
vec2 = Vector3.new(data[1], data[2], data[3])

print(vec2) --> 5, 2, 3

In the saving, I created a new table, and set the X, Y, and Z components of the Vector3 as the contents of that table.

When retrieving, the components were still in order so I was able to create a new vector in line 14.

2 comments

0

Wait a second, can you show me how to load it back into a vector then? Because that is also confusing

0

I do that in line 14. Because I saved the components in order, (x, y, and z) are at table positions (1, 2, and 3), respectfully. Therefore i am able to access them by doing data[1], data[2], data[3]

I think you should have read the website's guide to posting good questions and answers more thoroughly before posting this question. A link can be found here. You will notice as you read down the list, that under further explanation of rules it says: Providing compensation for answers is not allowed.

The goal of Scripting Helpers is to shed light on the world of ROBLOX Lua scripting for everyone. Those who post answers here should be posting to share information with no compensation. The community may give the poster reputation if they feel the answer was helpful, which will allow him to further help the community by gaining more privileges.

In the future please consider each of the guidelines before posting a question which violates the rules and mission of this website.

1 comment

0

Wow this totally helped me......

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