Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I save and then load a string variable?

Hi, So I'm currently a bit confused with DataStores, and I'm trying to specifically get them to save a string variable (called 'Value') that is located in game.Workspace. I have so far created a script that should save the player's data on exit (which I will share below) however, after looking through the ROBLOX Wikia and many other places I haven't found anything that will load data.

Here is the code I created to save player data

local DataStore = game:GetService("DataStoreService"):GetDataStore("Cash")

game.Players.PlayerRemoving:connect(function(player)
	local key = "user_" .. player.userId
	DataStore:UpdateAsync(key, function(oldValue)
		local newValue = oldValue or 0
		newValue = newValue + game.Workspace.Value.Value
		return newValue
	end)
end)

Thanks for any help you can give me.

4 comments

0

:GetAsync()

0

How would I use it though?

0

Would I do something like DataStore:GetAsync() local key "user_" .. player.userId??

0

It somehow looks like youre saving number values. If you want them to save a specific string just set oldValue to the strings value.

1 answer

While data persistence is depresiated, I recomend it for a data save solution for people who are new to scripting.

To save data:

game.Players.PLAYER:SaveString("DATATITLE", "DATA CONTENTS")

To load data:

local DATA = game.Players.PLAYER:LoadString("DATATITLE"))

However, if you would like to remain with data stores, I recormend this guide by @ScriptGuider

https://scriptinghelpers.org/questions/25774/simple-datastore-introduction

I hope this helps!

4 comments

0

I wouldn't recommend it at all. I'd just learn DataStores from the start.

0

Do NOT use data persistence. There is no use learning a deprecated feature that could stop working at any minute. Use DataStores.

0

Judging by what others have said, I'm going to go with DataStores. The guide linked was very helpful and I'm pretty sure I understand it now. Thanks anyway.

0

If this answer helped, please click 'Accept Answer' below

This discussion is preserved as it was. New answers, comments, and votes are closed.