Search
 
SCRIPT & CODE EXAMPLE
 

LUA

datastore roblox

-- This for lazy people who just came to copy & paste, not to learn the code...

-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore") -- This can be changed to whatever you want

local function saveData(player) -- The functions that saves data
	
	local tableToSave = {
	    player.leaderstats.Money.Value; -- First value from the table
	    player.leaderstats.Coins.Value -- Second value from the table
	}
	
	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)
	
	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end

game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game
	
-- // Assigning player stats //
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = leaderstats
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	
	local data -- We will define the data here so we can use it later, this data is the table we saved
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
		
	end)
	
	if success and data then -- If there were no errors and player loaded the data
		
		Money.Value = data[1] -- Set the money to the first value of the table (data)
		Coins.Value = data[2] -- Set the coins to the second value of the table (data)
		
	else -- The player didn't load in the data, and probably is a new player
		print("The player has no data!") -- The default will be set to 0
	end

end)

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
    local success, err  = pcall(function()
	    saveData(player) -- Save the data
    end)

    if success then
        print("Data has been saved")
    else
        print("Data has not been saved!")
    end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
	         saveData(player) -- Save the data
        end)

        if success then
            print("Data has been saved")
        else
            print("Data has not been saved!")
        end
	end
end)
Comment

PREVIOUS NEXT
Code Example
Lua :: check if player owns gamepass 
Lua :: get index of value in table lua 
Lua :: convert string to lowercase lua 
Lua :: lua table to json 
Lua :: lua empty table 
Lua :: roblox studio pause physics 
Lua :: FiveM Lua How to create table of all online player id 
Lua :: lua split 
Lua :: lua table functions 
Lua :: Print when a player joined roblxo 
Lua :: How to make a working gun in lua code 
Lua :: pico8 poke 
Lua :: roblox studio buying robux 
Lua :: animation event firing too many times roblox 
Matlab :: matlab if not true 
Matlab :: matlab plotting multiple lines on one graph 
Matlab :: what is java_home 
Basic :: cmd cd not working 
Basic :: visual basic how to determine if an array already contains an item 
Elixir :: how to split string in elixir 
Elixir :: elixir ecto pluck ids 
Elixir :: elixir nested if 
Scala :: Category Theory laws in scala 
Actionscript :: hello world in actionscript 
Excel :: excel conditionally highlight rows based on column 
Perl :: perl while loop 
Pascal :: pascal read 
Powershell :: takeown command 
Clojure :: how to make a range clojure 
Assembly :: docker cannot stop container 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =