I’ve been making a leave a note system like the one in “A dream you’ve had before” but I’m unsure on how to save the data like the note text, note writer, note position and rotation.
I’ve never used datastores a day in my life and I’m still learning new things so any help would be appreciated.
Using these you can make some primitive data stores like this:
--| This script will teleport the player to the location that they previously left at
--| Services
local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
--| Variables
local ds = DSS:GetDataStore("Notes")
--| Events
Players.PlayerAdded:Connect(function(plr)
local data = nil
local success, errorMsg = pcall(function()
data = ds:GetAsync(plr.UserId)
end)
if success then
if not data then
-- player's first time playing
else
local char = plr.Character or plr.CharacterAdded:Wait()
char.HumanoidRootPart.CFrame = CFrame.new(data[1], data[2], data[3]) * CFrame.Angles(math.rad(data[4]), math.rad(data[5]), math.rad(data[6]))
end
else
warn(errorMsg)
end
while task.wait(1) do -- hacky way
if not plr.Character then continue end
ds:SetAsync(plr.UserId, {
plr.Character.HumanoidRootPart.Position.X,
plr.Character.HumanoidRootPart.Position.Y,
plr.Character.HumanoidRootPart.Position.Z,
plr.Character.HumanoidRootPart.Rotation.X,
plr.Character.HumanoidRootPart.Rotation.Y,
plr.Character.HumanoidRootPart.Rotation.Z
})
end
end)
--[[
Players.PlayerRemoving:Connect(function(plr) -- this is how you'd save the data upon leaving, can't do it bc character gets deleted after leaving and cframe will be nil
ds:SetAsync(plr.UserId, { -- arrays and dictionaries are supported
plr.leaderstats.Coins.Value,
plr.leaderstats.Gems.Value
})
end)
]]
But these primitive data stores aren’t that safe and that’s why you should use data store modules like Suphi’s DataStore Module (SDM). The setup is a bit more complex, but still fairly simple.
depends what you want stored, think outside the box
do you need to store parts? size vec3 etc? you’d need to collect all relevant datas in the datastore. you are basically writing the part hierarchy as if it were in workspace.
I’m aware of how to save it with the example you gave me but I’m a bit confused at the part when it comes to loading it in. You saved the player with a key which was their userid but it’s a tad bit different saving a group of parts. I’ve had it so the part is set async when it’s placed down but is there a way to loop through a data store and place the parts accordingly l.
Why do you need to loop DataStore?
When you get data, it should includes all the data you saved before.(Vector3, CFrame or else) You can just use the data when player joins again.
I’m not trying to save a player’s data I’m saving different parts that I want to load in at the same position they were before. In the example the other user made, they used the player id to find specific data but I want the blocks to all load in and not just one from the data store. Sorry if I don’t make sense I’m kinda new to this
Along with all the useful replies on this post, I highly recommend you use a Datastore editor plugin like DataDelve.
(A long time ago, when I was still learning, I had a very hard time understanding how DataStores worked and were structured since they seemed like a black box. Thanks to plugins like DataDelve, I was able to visualize how my data actually got put in the DataStore, and it gave me a better understanding than any tutorial ever could.)
I’m trying to create a system where players can place notes down (I already have that completed) but I want the notes placed to be saved across servers (the notes are parts with surfacegui) but if I have multiple notes with setasync what would the key be?
For this, you’d need to learn about cross-server communication using MessagingService.
Only the server where the note was originally placed should send a SetAsync.
I’d say the key should be a random value. You can generate a different random value every time using HttpService:GenerateGUID(), and you could call the keys something like "note-" .. HttpService:GenerateGUID()
Thanks to this, now I understood what you want.
When player put a note, it should be immediately appear in all the servers, isn’t it?
I agree with his solution, so you can follow him.
I was more referring of when a server loads up all of the notes would load but now that we’ve spoken about it more I realize it would load all the notes from the data store either way. I’m just stuck on how to load the notes
There shoud be limit for how old notes are loaded.
If all the past notes are loaded, it cost more and more time for loading as many players write notes.
As Ransomwave said, when player write a note, the data should be saved only by the server that the player is in. At the same time, use MessagingService to inform to the other servers.
When server starts, just load data of notes from DataStore().
How do you load all of the data of all of the notes though? Whenever StarvinMarvin created an example of how you would save player data getAsync() is only able to get one key. If all of the notes are saved under different keys how would you load all of them?
local function createNote(player: Player, noteCFrame: CFrame, noteText: string): Part
local note = ...
note.SurfaceGui.TextLabel.Text = noteText
note.CFrame = noteCFrame
note.Parent = workspace
note.Name = player.UserId --// use an identifier to delete notes if you want to
end
local function loadData(player: Player)
local data = ...
for _, v in data do
local noteCFrame = v.cframe
local noteText = v.text
createNote(player, noteCFrame, noteText)
end
end
it’d still need a key (thats how you save and receive data), how you implement that key is up to you. If you want notes to only load for each player, use the players UserId as a key.
If you want a global datastore for everything, just use anything, all it needs to do is match.
As for actually saving it, you only need one key for everything. Datastores can store tables, and those tables can then contain other tables.
Each block of cframes and texts in the example I shown was an entirely different note, really you could copy-paste it nearly as much as you’d want.
Of course, there are datastore limits, so you’ll have to keep those in mind, but overall one key, or some kind of dynamic key, would be fine.
From memory, the limit is around 4mb per key, but I haven’t researched it much so don’t take my word as fact.
Use UpdateAsync, you should be able to just update the data store whenever someone places down a note. All of the data should be in an array and each element should also be an array, consisting of the text, position, rotation and player id (to report inappropriate notes).
Whenever a server starts you should get the data store and loop thru it, reading the data and placing down the notes.