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

How to use DataStore for Teams? [closed]

Asked by 3 years ago

I am trying to make an obby with similar concepts to "The Impossible Obby"; and one of those concepts is that your Team data saves, so when you leave the game you can rejoin at the same checkpoint that you left at. There is basically a checkpoint, that when touched assigns you to a Team, which I can do. Using Datastore is the problem though. I do not know at all what to do, I watched tutorials after my own trial and error and it doesn't work. I could really use a script or two, i usually ask for hints/help, but a script would be really appreciated for this case. The teams are named with colors by the way.

Closed as Not Constructive by JesseSong

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

I would normally close this question, since it's more of a request, but I had a lot of issues finding a simple tutorial on data stores, as well, when I was learning. Some of the tutorials on the Wiki are really drawn out and, although the more complex examples lead to more effective data stores, it's neither necessary nor helpful for someone trying to learn.

The basics of a datastore are as follow. Through the DataStoreService, we use the :GetAsync() method to retrieve any existing data for the given key (unique or otherwise). When you want to save or overwrite pre-existing data, you use the :SetAsync() method. One of this method's arguments is the key that I mentioned. You could, for example, have a unique key for each player (ie Player.UserId .. "_Key").

Tying this all together, we can create a simple data store. In the example below, I'll create a data store for leaderstats.

Run through a ServerScript...

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local LeaderboardData = DataStoreService:GetDataStore("LeaderboardData") --returns a GlobalDataStore, through which we run :GetAsync() and :SetAsync()

Players.PlayerAdded:Connect(function(Player)
    local SavedData = LeaderboardData:GetAsync(Player.UserId .. "_Key") -- retrieve entire table of saved data
    local leaderstats = Instance.new("Model")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player
    local Kills = Instance.new("IntValue")
    Kills.Name = "Kills"
    Kills.Parent = leaderstats
    local Deaths = Instance.new("IntValue")
    Deaths.Name = "Deaths"
    Deaths.Parent = leaderstats
    if SavedData then
        Kills.Value = SavedData[1] or 0 -- remember, we set the "Kills" value to the first table entry
        Deaths.Value = SavedData[2] or 0 --we set "Deaths" to the second table entry or 0 if nil
    end
end)

Players.PlayerRemoving:Connect(function(Player)
    local leaderstats = Player:FindFirstChild("leaderstats")
    if leaderstats then
        local Kills = leaderstats.Kills
        local Deaths = leaderstats.Deaths
        local DataToSave = {Kills.Value,Deaths.Value} -- place all data in a table, so we can save it all at once
        LeaderboardData:SetAsync(Player.UserId .. "_Key",DataToSave) -- give datastore a unique key and save table
    end
end)

Of course, you can create more complex, more secure data stores, which you would definitely want to look into if you were handling a popular game. You can find a simple tutorial to data stores here, and a more complex tutorial here

Good luck!

Note: be sure to enable "Studio Access to API Services" through your game settings before setting data stores!

1
Thank you so much, it genuinely helps. I had no understanding of Datastores and you really explained it well. Much appreciated! NightWarrior1717 14 — 3y
Ad