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

How can I use Data Stores like this?

Asked by
Zerio920 285 Moderation Voter
9 years ago

New to using Data Stores. My question is two-fold:

The first time the player enters the game, the game checks to see if he already has an entry in the datastore. If he doesn't, the game makes a new one and sets his value to "0". If he does, the entry is left alone.

After playing the game for a bit, the player will do something that adds 1 to the value. But if he does this again, another 1 won't be added to the value. The entry will be left alone.

Any help would be appreciated.

0
It would help if you did what you could. Obviously you should know you'll start out with the PlayerAdded event. alphawolvess 1784 — 9y
1
You should post the code Goulstem 8144 — 9y

1 answer

Log in to vote
1
Answered by
IXLKIDDO 110
9 years ago

You could probably set up a datastore key to check if it is "nil" or nothing at all. If it is, then it sets the user value to 0. After the event happens, you could most likely set the value to 1. Therefore, if it happens again, it'll stay 1. Some code you can use is:

Set DataStore. This one is fairly easy and all I do is make it a local variable. The format for this would be:

local Datastore = game:GetService("DataStoreService"):GetDataStore("INSERT NAME HERE")

If the player is new to the Datastore, set it to 0. GetAsync fetches the data (since there is nothing there for the player, it is nil), while SetAsync sets the new value of it for the player.

game.Players.PlayerAdded:connect(plr)
    if Datastore:GetAsync(plr.userId) == nil then
        Datastore:SetAsync(plr.userId, 0)
    end
end

If the player does an event, set it to 1.

DataStore:SetAsync(plr.userId, 1)

Similarly, you can also set the Datastore's to different things as well. You could probably set it to 2 if you want to. As always, you can change the local variable name or the key. For example, if you wanted the key to be: user_[insert playerID]. Then you would replace plr.userId in the sample codes with "user_ .. plr.userId". I recommend setting it so that it is the userId rather than the name itself due to roblox name changes. I haven't tested this script yet (no access to computer so tell me if the tabs seem okay) so tell me if there's any problems.

Ad

Answer this question