Can you datasave strings?

I want to make a save for some text that is important to my game. Is it possible to save strings just by their string or do I have to achieve certain steps. I made a script, It didn’t work.

local data = game:GetService("DataStoreService"):GetDataStore('NoteText')
game.Players.PlayerAdded:Connect(function(p)
script.Parent.Text = data:GetAsync(p.UserId) or ""
script.Parent.Changed:Connect(function()
data:SetAsync(p.UserId, script.Parent.Text)
end)
end)
game.Players.PlayerRemoving:Connect(function(p)
data:SetAsync(p.UserId, script.Parent.Text)
end)

That is because GUIs are entirely client-side. The server cannot detect input for anybody. The client has to send that information to the server with the use of Remotes.
Preferably, you only want to send the change and have the server save only when they leave (or autosave) as this would lag the server and cause request queue spam if someone decides to spam type into the text box.
Also, you can store tables as well. I am not sure why people don’t know this right off the bat, but you can store more than just text in one datastore key.

{
    Coins = 50,
    Level = 3,
    Text = "hello world"
}

can be validly stored

1 Like

You could try using StringValues instead, hook up the StringValue to the Text, and save the text from the StringValue.

EDIT: this does not work I guess.

That will also not work because it would not replicate either. Like I said, GUIs are client-side, and also things that the client changes will not replicate to the server (ignoring physics/animation/sound replication for now). The only solution is remote events or remote functions.

3 Likes

Adding on to this, if your queue becomes too large it is possible to lose data as requests are cancelled.

P.S.

I recommend using DataStore2 instead. It’s a module created by Kampfarren which uses the best method of preventing data loss. It is actually quite easy to learn, its guide is very well written.