DataStores w/ Example


  • DataStores are one of the most useful things in Roblox. They are used to save data upon the user leaving the game. To get started, insert a Script into ServerScriptService.
    I say you should name the script something like DataStores to keep track of what it is.

    Next, open the script and set a variable:
    local datastore = game:GetService("DataStoreService")
    Then, create the DataStore:
    local data1 = game:GetDataStore("Data1")

    Now you will need a function to load and save the players stats, but first, you will need a leaderboard:

    game.Players.PlayerAdded:Connect(function(player)
        local stats = Instance.new("Folder", player)
        stats.Name = "leaderstats"
    end)
    

    Now that we have the leaderboard set up, add some variables. We will use Coins as an example:

    game.Players.PlayerAdded:Connect(function(player)
        local stats = Instance.new("Folder", player)
        stats.Name = "leaderstats"
    
        local var1 = Instance.new("IntValue", stats)
        var1.Name = "Coins"
        var1.Value = 0
    end)
    

    Now, we have an item to load into the game. But, we will have to replace the value of the Coins, A.K.A var1.
    Put the following into the code, after var1.Value =

    game.Players.PlayerAdded:Connect(function(player)
        local stats = Instance.new("Folder", player)
        stats.Name = "leaderstats"
    
        local var1 = Instance.new("IntValue", stats)
        var1.Name = "Coins"
        var1.Value = data1:GetAsync(player.UserId, var1.Value) or 0
    end)
    

    This will now save it, but not fully. It won't load your stuff into the game, so you need to :SetASync()

    game.Players.PlayerAdded:Connect(function(player)
        local stats = Instance.new("Folder", player)
        stats.Name = "leaderstats"
    
        local var1 = Instance.new("IntValue", stats)
        var1.Name = "Coins"
        var1.Value = data1:GetAsync(player.UserId, var1.Value) or 0
        data1:SetAsync(player.UserId, var1.Value)
    end)
    

    To make sure it always does save, and always loads ASAP, you need to save for when a it changes and the player leaves.

    game.Players.PlayerAdded:Connect(function(player)
        local stats = Instance.new("Folder", player)
        stats.Name = "leaderstats"
    
        local var1 = Instance.new("IntValue", stats)
        var1.Name = "Coins"
        var1.Value = data1:GetAsync(player.UserId, var1.Value) or 0
        data1:SetAsync(player.UserId, var1.Value)
    
        var1.Changed:Connect(function()
            print("Saving. . .")
            date1:SetAsync(player.UserId, var1.Value)
    	print(player.Name.. "'s data of"..box.Value.." "..box.Name.." has been saved!")
        end)
    end)
    

    Now for PlayerRemoving:

    game.Players.PlayerAdded:Connect(function(player)
        local stats = Instance.new("Folder", player)
        stats.Name = "leaderstats"
    
        local var1 = Instance.new("IntValue", stats)
        var1.Name = "Coins"
        var1.Value = data1:GetAsync(player.UserId, var1.Value) or 0
        data1:SetAsync(player.UserId, var1.Value)
    
        var1.Changed:Connect(function()
            print("Saving. . .")
            date1:SetAsync(player.UserId, var1.Value)
    	print(player.Name.. "'s data of"..box.Value.." "..box.Name.." has been saved!")
        end)
    end)
    
    wait(0.1)
    
    game.Players.PlayerRemoving:Connect(function(player)
    	print("Saving. . .")
    	data1:SetAsync(player.UserId, player.leaderstats.Coins.Value)
    	print(player.Name.. "'s data of"..box.Value.." "..box.Name.." has been saved!")
    end)
    

    Now your code will fully save, on join and leave!
    Feel free to use this code to learn off of, but if you use it in your game, give me Credit for the DataStore, and teaching you to use it.

    If there are any errors, or you have a question, comment on this and show me with an example.


  • Great work! I still don't understand completely datastores because roblox wiki's tutorials are useless so I'll make sure to read this post :P

Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.