Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I save and then load a string variable?

Asked by
o_Blyzo 12
7 years ago

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.

0
:GetAsync() OldPalHappy 1477 — 7y
0
How would I use it though? o_Blyzo 12 — 7y
0
Would I do something like DataStore:GetAsync() local key "user_" .. player.userId?? o_Blyzo 12 — 7y
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. qVision 12 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

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!

0
I wouldn't recommend it at all. I'd just learn DataStores from the start. TheHospitalDev 1134 — 7y
0
Do NOT use data persistence. There is no use learning a deprecated feature that could stop working at any minute. Use DataStores. Disillusions 61 — 7y
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. o_Blyzo 12 — 7y
0
If this answer helped, please click 'Accept Answer' below AlphaGamer150 101 — 7y
Ad

Answer this question