Hi guys. I'm quite new to Lua and I don't fully understand DataStores. I have code to create it, but I am not sure how I would take a variable from it and change that variable externally. The code is in a Script, inside ServerScriptService.
local DataStoreService=game:GetService("DataStoreService")
local DataStore=DataStoreService:GetDataStore("Player_Data")
--[[
A player has joined the game. Load/Create their data.
--]]
game.Players.PlayerAdded:connect(function(Player)
local PlayerData; --Will hold the player's data when fetched from the datastore.
--[[
Set up the player's leaderstats and add them to the ROBLOX leaderboard.
--]]
local Stats=Instance.new('Model')
Stats.Name="leaderstats"
local Money=Instance.new('NumberValue',Stats)
Money.Name="Money"
Stats.Parent=Player
--[[
We are fetching the player's data, if it exists.
If it does not exist, we will create new data for the player as they
must logically be new to the game.
--]]
local CanSave=Instance.new('BoolValue',Player) --This is used to determine whether or not the
--player's data can be saved when they leave.
CanSave.Name="CanSaveData"
CanSave.Value=true
local DataFetchSuccess,ErrorMessage=pcall(function() --We are wrapping the datastore request in a protected function, as web requests can error.
--Safely handling datastore errors can prevent player data corruption.
PlayerData=DataStore:GetAsync(tostring(Player.UserId))
end)
if DataFetchSuccess then --The datastore GET request was successful!
if PlayerData~=nil then --The player's data exists, they have played this game before. Put their data into the leaderboard.
Player.leaderstats.Money.Value=PlayerData
else --The player's data does not exist, they're new to the game. Create their data on the leaderboard.
Player.leaderstats.Money.Value=100 --Players start out with 100 money in this example.
end
else --The GET request failed, datastores could be down.
--[[
There are many ways to handle datastore failures. In this case, we will kick the player,
letting them know their data failed to load.
Because their data wasn't loaded, we won't save their data when they leave.
]]--
Player.CanSaveData.Value=false
Player:Kick("Your data failed to load! Please rejoin.")
end
end)
--[[
A player is leaving the game. Save their data.
--]]
game.Players.PlayerRemoving:connect(function(Player)
if Player.CanSaveData.Value==false then return end --Player data can't be saved, so do nothing.
local PlayerData=Player.leaderstats.Money.Value
local DataWriteSuccess,ErrorMessage=pcall(function() --Once again, we are safely calling a web request. If it fails, we can safely handle it.
DataStore:SetAsync(tostring(Player.UserId),PlayerData)
end)
if not DataWriteSuccess then --Uh oh, player data didnt' save. Handle this error.
--We will attempt to save the player's data 5 more times. If it fails after 5 times,
--we will abort the save and the player's data will not be changed in the datastores.
local Retry_Count=0
while Retry_Count<6 do
wait(60) --Wait 1 minute between each retry
local Succeded,Error=pcall(function()
DataStore:SetAsync(tostring(Player.UserId),PlayerData)
end)
if Succeded then break end --HURRAY, DATA SAVED!
Retry_Count=Retry_Count+1
end
end
end)
If you don't understand, please ask. Thanks for helping!

2 comments
To make sure I understand your question, you'd like to make this handle more than one variable correct? So like, not just money like it handles now?
Yeah, I want it to hold multiple variables per person. Thanks