Saving a vector3 to a datastore

I am trying to make a datastore system to save the player’s position when they touch a check point, but roblox does not allow saving vector3 values to datastore, I have no idea whow i could save it

local DataStoreService = game:GetService("DataStoreService")
local SpawnStore = DataStoreService:GetDataStore("SpawnPosition")

game.Players.PlayerAdded:Connect(function(Player)
	local success , err = pcall(function()
		return SpawnStore[Player.UserId]
	end)
	
	if success then
		Player.CharacterAdded:Wait()
		Player.Character:SetPrimaryPartCFrame(CFrame.new(success))
	else
		warn(err)
		
		SpawnStore:SetAsync(Player.UserId , Vector3.new(-12, 4.6, 16))
		Player.Character:SetPrimaryPartCFrame(CFrame.new(SpawnStore[Player.UserId]))
	end
end)

Just serialize the Vector3 object into an array.

local Position = {Pos.X, Pos.Y, Pos.Z}

SpawnStore:SetAsync(Player.UserId, Position)

Then when you want to load the position, you just take the 3 values in the table and construct a Vector3 object.

local success, response = pcall(function()
    return SpawnStore[Player.UserId]
end)

if success then
    local Pos = Vector3.new(table.unpack(success))
end
3 Likes

Ok, but how can i convert vector3 to table, for when I want to save the position .

I just showed you. Just construct a new table with the coordinates of the Vector3.

where Pos is some Vector3 object.

Oh ok thank you
:shallow_pan_of_food: