Hello, i have question: Is it possible to save Vector3 Value? Because i tried and getting error " ServerScriptService.Core:24: bad argument #3 to 'Value' (Vector3 expected, got number)"
Here is code from DataStore:
local Stats = Instance.new('Folder', game.ReplicatedStorage) Stats.Name = "PlayerStorage" local DataStore = game:GetService("DataStoreService"):GetDataStore("BangBangStore") game.Players.PlayerAdded:connect(function(player) local plrStats = Instance.new('NumberValue', game.ReplicatedStorage.PlayerStorage) plrStats.Name = player.Name local plrMoney = Instance.new('NumberValue', plrStats) plrMoney.Name = "PointsStorage" local plrScale = Instance.new('Vector3Value', plrStats) plrScale.Name = "ScaleStorage" local plrCharacter = Instance.new('StringValue', plrStats) plrCharacter.Name = "DuckStorage" local playerCharacter = game.Players:FindFirstChild(player.Name) if playerCharacter ~= nil then playerCharacter.CanLoadCharacterAppearance = false end if script.Parent.SettingsTab.StatsSave.Value == true then pcall(function() local store = DataStore:GetAsync("u_"..player.UserId) if store then print("Found data store") plrMoney.Value = store plrScale.Value = store else print("No data found") end end) end end) game.Players.PlayerRemoving:connect(function(player) local plrStats = game.ReplicatedStorage.PlayerStorage:FindFirstChild(player.Name) local plrMoney = Instance.new('NumberValue', plrStats) plrMoney.Name = "PointsStorage" local plrScale = Instance.new('Vector3Value', plrStats) plrScale.Name = "ScaleStorage" local plrCharacter = Instance.new('StringValue', plrStats) plrCharacter.Name = "DuckStorage" if script.Parent.SettingsTab.StatsSave.Value == true then pcall(function() DataStore:SetAsync("u_"..player.UserId, plrMoney.Value, plrScale.Value) print("Saved") end) end if plrStats ~= nil then plrStats:Destroy() end end)
Or maybe Vector3.Value can be saved in different values ?
All values have a type in lua. A vector3's type is an userdata. You can know an value's type by calling the type function:
print(type(Vector3.new(0,0,0))) -- will print userdata
No, you cannot store values of the type userdata in datastores. Datastores only accepts strings, numbers, booleans & tables. Vector3's are objects, so you can't store them.
What you should do is make a new value of a different type that represents the original vector3. Lets call this conversion.
Our goal should be to convert the value in the way where it can always be unconverted back to the original. We will save the converted value. When we load it, we will convert it back to the original.
Lets write some functions for converting vector3s to different types and back.
Vector3s to strings
--vector3s to strings local VectorToString = function(vec) return vec.X..' '..vec.Y..' '..vec.Z end local StringToVector = function(str) local tab = {} for a in string.gmatch(str,"%d+") do table.insert(tab,a) end return Vector3.new(tab[1],tab[2],tab[3]) end
Vector3s to Tables
local VectorToTable = function(vec) return {vec.X,vec.Y,vec.Z} end local TableToVector = function(tab) return Vector3.new(tab[1],tab[2],tab[3]) end
Saving & Loading
--save local vector = vector3.new(1,1,1) datastore:SetAsync('hi',VectorToTable(vector)) --load local data = datastore:GetAsync('hi') print(TableToVector(data))