(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)
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.
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.
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.