What you need to know
- DataStoreService
- Variables
- Strings
- Functions
- pcall
- return
- typeof (optional)
Saving CFrames
Using DataStoreService you can only save strings or numbers. So how can we use this to save CFrames? Well, we can use the tostring() to convert the CFrame to a string. When we print this out:
local stringCFrame = tostring(CFrame.new(0, 0, 0))
print(stringCFrame)
We get:
"1, 2, 3, 1, 0, 0, 0, 1, 0, 0, 0, 1"
(Don’t worry about the numbers after 3 they are just about the rotation)
As you see we get a total of 12 numbers. And since this is a string we can successfully save this to DataStoreService without any problems. We can also use typeof() to see if it is a string or a cframe. So this is what it would like like to save:
--// Services
local DataStoreService = game:GetService("DataStoreService")
--// DataStores
local cframeDataStore = DataStoreService:GetDataStore("CFrame")
--// Services
local DataStoreService = game:GetService("DataStoreService")
--// DataStores
local cframeDataStore = DataStoreService:GetDataStore("CFrame")
--// CFrame to save
local stringCFrame = tostring(CFrame.new(1, 2, 3))
print(stringCFrame,typeof(stringCFrame))
--// Save
local success, result = pcall(function() -- Wrap in pcall if error
cframeDataStore:SetAsync("CFrame",stringCFrame)
end)
if success then -- If saving unsuccessfull then warn server
print("Saved CFrame!")
else
warn(result)
end
Okay now we have saved the CFrame but how do we turn the CFrame string back into a CFrame? Well we can use the string.split() and we can separate it by “,” to get a table of all 12 numbers in order, the table will look something like this:
local table = {1, 2, 3, 1, 0, 0, 0, 1, 0, 0, 0, 1}
So now if we get the data and convert it to a cframe and print it out it will look like this:
--// Services
local DataStoreService = game:GetService("DataStoreService")
--// DataStores
local cframeDataStore = DataStoreService:GetDataStore("CFrame")
--// CFrame to save
local stringCFrame = tostring(CFrame.new(1, 2, 3))
print(stringCFrame,typeof(stringCFrame))
--// Save
local success, result = pcall(function() -- Wrap in pcall if error
cframeDataStore:SetAsync("CFrame",stringCFrame)
end)
if success then -- If saving unsuccessfull then warn server
print("Saved CFrame!")
else
warn(result)
end
--// Function to convert back to CFrame
local function convertToCFrame(String)
local splittedString = string.split(String,",")
local newCFrame = CFrame.new(
splittedString[1],
splittedString[2],
splittedString[3],
splittedString[4],
splittedString[5],
splittedString[6],
splittedString[7],
splittedString[8],
splittedString[9],
splittedString[10],
splittedString[11],
splittedString[12]
)
return newCFrame
end
--// Get saved cframe
local savedCFrame
local success, result = pcall(function() -- Wrap in pcall if error
savedCFrame = cframeDataStore:GetAsync("CFrame")
end)
if success then -- If saving unsuccessfull then warn server
savedCFrame = convertToCFrame(savedCFrame) -- Convert to cframe
print(savedCFrame,typeof(savedCFrame))
else
warn(result)
end
And there you have it! That’s how you save CFrames.
Saving Positions
Now if you learned what we did on saving CFrames we can use the same technique on saving positions. Except it’s easier converting because we only have 3 numbers instead of 12! So the function of converting it will look like this:
local function convertToPosition(String)
local splittedString = string.split(String,",")
local position = Vector3.new(
splittedString[1],
splittedString[2],
splittedString[3],
)
return position
end)
Saving Color3’s
Now Colors are a bit different because when you use Color3.fromRBG it won’t actually print out number 0 - 255 but instead of a number between 0 and 1. A number 0 - 1 is actually a Color.new not Color3.fromRBG. So the function will look like this instead:
local function convertToColor3(String)
local splittedString = string.split(String,",")
local position = Color3.new(
splittedString[1],
splittedString[2],
splittedString[3],
)
return position
end)
Conclusion
In this tutorial you have learned how to convert Numbers to strings and back to Numbers! I hope you have learned something from this, as this is actually my first tutorial! And have a good rest of your day! (Also you feedback would be great!)
- Terrible
- Bad
- Okay
- Decent
- Good
- Very Good
- Perfect
0 voters