I can save objects/models in-game and their positions. I want to know how I can have these objects destroyed/removed from the data-store and not reappear anymore. Basically, I want to purge/omit/overwrite the saved model of data from the game/date-store.
So the following code below here is what let's me gather the "deployables", which would be models that a player had placed in the game. That deployable will be saved for position and the next time a game starts, the duplicate template will be cloned to take place for that saved data position of the original deployable.
Of course, this is only an example of how it saves the deployable. I do not have any sample codes that can destroy/remove the saved data of the deployable. Which is why I want to make a code that allows me to do just that.
Note: I did not create this code, I'm a mere editor of the code.
local DataStore = game:GetService("DataStoreService"):GetDataStore("Deployables") --The magical realm of data!
local ServerScriptService = Game:GetService("ServerScriptService") --The operating service for scripts in the server!
local ReplicatedStorage = Game:GetService("ReplicatedStorage") --A storage, that's all.
local EncodeInstance = require(ServerScriptService.EncodeInstance) --Transforming it better to save in data store.
local Deployables = Workspace.DeployableInstances --The models themselves
local DeployableTemplates = ReplicatedStorage.Items --The templates that are duplicated for the models.
local function UpdateList() --The stuff that it will update the process of it.
local DeployableList = DataStore:GetAsync("DeployableList")
local BrandNewDataTable = {}
for _,DeployableSaveData in pairs(ReplicatedStorage.NewDeployables:GetChildren()) do
-- Save the value using the encoder!
local SaveData = EncodeInstance.Encode(DeployableSaveData)
-- Insert it into the table that we got from DataStore:GetAsync("DeployableList")
if DeployableList then
table.insert(DeployableList, SaveData)
else
table.insert(BrandNewDataTable, SaveData)
end
DeployableSaveData:Destroy() --Purge
end
-- Save it to the DataStore!
if DeployableList then
DataStore:SetAsync("DeployableList", DeployableList)
else
DataStore:SetAsync("DeployableList", BrandNewDataTable)
end
-- Done!
print("Saved Instances")
end
local function InstanceNewDeployableFromDataStore()
local DeployableList = DataStore:GetAsync("DeployableList")
if DeployableList then
for _,DeployableEntry in pairs(DeployableList) do
DeployableEntry = EncodeInstance.Decode(DeployableEntry)
for _,ItemFolder in pairs(DeployableTemplates:GetChildren()) do
if ItemFolder.Name == DeployableEntry.Name then
-- Create a deployable based on the saved value!
local NewDeployable = ItemFolder.Assets.Variation1:Clone()
NewDeployable.Name = ItemFolder.Name
NewDeployable.Parent = Deployables
NewDeployable:SetPrimaryPartCFrame(DeployableEntry.Value)
end
end
end
end
-- Can't be bothered updating each and every local script for this, so I'll do it here instead.
Deployables.ChildAdded:connect(function(Child)
if Child:IsA("Model") and Child.PrimaryPart ~= nil then
local SaveValue = Instance.new("CFrameValue")
SaveValue.Name = Child.Name
SaveValue.Value = Child.PrimaryPart.CFrame
SaveValue.Parent = ReplicatedStorage.NewDeployables
end
end)
print("Loaded Instances") --Yeah, just a message to let ya know when it loaded the instances.
end
InstanceNewDeployableFromDataStore()
while Wait(5) do --Waiting before updating the list!
UpdateList() --Ok, it updates the list.
end --The end.
How to approach on this based on the code provided. Which is what helps saves the object(s) and process the data into the data store.
This might be too vague to answer but any sort of guidance or information that will help me figure a way to solve my problem will be much appreciated.
Update 1: The encode script (There's actually more scripts within the encodeinstance script that helps encode the data.)
EncodeInstance:
local BitBuffer = require(script.BitBuffer)
local BitEncodeInstance = require(script.BitEncodeInstance)
local EncodeInstance = {}
function EncodeInstance.Encode(instance)
local buffer = BitBuffer.Create()
BitEncodeInstance.Write(buffer, instance)
return buffer:ToBase64()
end
function EncodeInstance.Decode(text)
local buffer = BitBuffer.Create()
buffer:FromBase64(text)
return BitEncodeInstance.Read(buffer)
end
return EncodeInstance
The Bitbuffer instructions (without the actual code) script below.
======================================================================= API
Constructor: BitBuffer.Create()
Read/Write pairs for reading data from or writing data to the BitBuffer: BitBuffer:WriteUnsigned(bitWidth, value) BitBuffer:ReadUnsigned(bitWidth) Read / Write an unsigned value with a given number of bits. The value must be a positive integer. For instance, if bitWidth is 4, then there will be 4 magnitude bits, for a value in the range [0, 2^4-1] = [0, 15]
BitBuffer:WriteSigned(bitWidth, value)
BitBuffer:ReadSigned(bitWidth)
Read / Write a a signed value with a given number of bits. For
instance, if bitWidth is 4 then there will be 1 sign bit and
3 magnitude bits, a value in the range [-2^3+1, 2^3-1] = [-7, 7]
BitBuffer:WriteFloat(mantissaBitWidth, exponentBitWidth, value)
BitBuffer:ReadFloat(mantissaBitWidth, exponentBitWidth)
Read / Write a floating point number with a given mantissa and
exponent size in bits.
BitBuffer:WriteFloat32(value)
BitBuffer:ReadFloat32()
BitBuffer:WriteFloat64(value)
BitBuffer:ReadFloat64()
Read and write the common types of floating point number that
are used in code. If you want to 100% accurately save an
arbitrary Lua number, then you should use the Float64 format. If
your number is known to be smaller, or you want to save space
and don't need super high precision, then a Float32 will often
suffice. For instance, the Transparency of an object will do
just fine as a Float32.
BitBuffer:WriteBool(value)
BitBuffer:ReadBool()
Read / Write a boolean (true / false) value. Takes one bit worth
of space to store.
BitBuffer:WriteString(str)
BitBuffer:ReadString()
Read / Write a variable length string. The string may contain
embedded nulls. Only 7 bits / character will be used if the
string contains no non-printable characters (greater than 0x80).
BitBuffer:WriteBrickColor(color)
BitBuffer:ReadBrickColor()
Read / Write a roblox BrickColor. Provided as an example of
reading / writing a derived data type.
BitBuffer:WriteRotation(cframe)
BitBuffer:ReadRotation()
Read / Write the rotation part of a given CFrame. Encodes the
rotation in question into 64bits, which is a good size to get
a pretty dense packing, but still while having errors well within
the threshold that Roblox uses for stuff like MakeJoints()
detecting adjacency. Will also perfectly reproduce rotations which
are orthagonally aligned, or inverse-power-of-two rotated on only
a single axix. For other rotations, the results may not be
perfectly stable through read-write cycles (if you read/write an
arbitrary rotation thousands of times there may be detectable
"drift")
From/To pairs for dumping out the BitBuffer to another format: BitBuffer:ToString() BitBuffer:FromString(str) Will replace / dump out the contents of the buffer to / from a binary chunk encoded as a Lua string. This string is NOT suitable for storage in the Roblox DataStores, as they do not handle non-printable characters well.
BitBuffer:ToBase64()
BitBuffer:FromBase64(str)
Will replace / dump out the contents of the buffer to / from
a set of Base64 encoded data, as a Lua string. This string
only consists of Base64 printable characters, so it is
ideal for storage in Roblox DataStores.
Buffer / Position Manipulation BitBuffer:ResetPtr() Will Reset the point in the buffer that is being read / written to back to the start of the buffer.
BitBuffer:Reset()
Will reset the buffer to a clean state, with no contents.
Example Usage:
local function SaveToBuffer(buffer, userData)
buffer:WriteString(userData.HeroName)
buffer:WriteUnsigned(14, userData.Score) --> 14 bits -> [0, 2^14-1] -> [0, 16383]
buffer:WriteBool(userData.HasDoneSomething)
buffer:WriteUnsigned(10, #userData.ItemList) --> [0, 1023]
for _, itemInfo in pairs(userData.ItemList) do
buffer:WriteString(itemInfo.Identifier)
buffer:WriteUnsigned(10, itemInfo.Count) --> [0, 1023]
end
end
local function LoadFromBuffer(buffer, userData)
userData.HeroName = buffer:ReadString()
userData.Score = buffer:ReadUnsigned(14)
userData.HasDoneSomething = buffer:ReadBool()
local itemCount = buffer:ReadUnsigned(10)
for i = 1, itemCount do
local itemInfo = {}
itemInfo.Identifier = buffer:ReadString()
itemInfo.Count = buffer:ReadUnsigned(10)
table.insert(userData.ItemList, itemInfo)
end
end
--...
local buff = BitBuffer.Create()
SaveToBuffer(buff, someUserData)
myDataStore:SetAsync(somePlayer.userId, buff:ToBase64())
--...
local data = myDataStore:GetAsync(somePlayer.userId)
local buff = BitBuffer.Create()
buff:FromBase64(data)
LoadFromBuffer(buff, someUserData)

1 comment
"I can save objects/models in-game and their positions." Can you show me an example of how to do this ? I cannot find one ANYWHERE could really use some help