Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local dataStoreService=game:GetService("DataStoreService") -- Getting/ Requiring the Service and it's Data Model.
- -- This allows us to use it's Methods :GetDataStore(), :GetOrderedDataStore(), GetRequestBudgetForRequestType()
- local dataStore=dataStoreService:GetDataStore("PlayerData") -- Asking the dataStoreService for this Table of contents.
- -- If the above Table of contents doesn't yet exist one will be created with the name provided, PlayerData.
- -- This table has Functions already preset by the functions of the Data Store Service Model.
- -- Methods for this table: dataStore:GetAsync(), dataStore:SetAsync(), dataStore:RemoveAsync(),
- -- dataStore:UpdateAsync(), dataStore:IncrementAsync()
- --local SD=game:GetService("ReplicatedStorage"):WaitForChild("Remotes").SaveData -- I've set up a RemoteEvent Instance in this path.
- -- You don't need this but it could be helpful if you wanted to allow the player to manually save their data.
- game:GetService("Players").PlayerAdded:Connect(function(player) -- Whenever a player joins the game we'll do this...
- local leaderstats=Instance.new("Folder") -- Creating an Instance with the Class Folder.
- leaderstats.Name="leaderstats" -- For any numerical values store within it to be shown on the CoreGUI Leaderboard you need to
- -- Set the name to exactly, "leaderstats".
- -- If you have a custom GUI that displays this information then the name can be anything. Using leaderstats will..
- -- only be useful if you want the values of the Instances below to be shown on the default leaderstats
- leaderstats.Parent=player -- Setting the Folder Instances parent to the Player.
- local Coins=Instance.new("IntValue") -- Creating an Instance with the Class IntValue.
- Coins.Name="Coins" -- name it anything, Cash, cash, Money, money, Moneyz, BigMan420, doesn't matter.
- Coins.Parent=leaderstats -- Putting this IntValue inside the Folder we made above for the player.
- local CoinData -- Just a temporary holder for any Values returned by the PCall Below.
- local success, errormessage=pcall(function() -- PCalls or Protected Calls allow us to call another function/ Method...
- -- Without the risk of crashing, creating errors without our scripts main function, etc.
- -- Basically this just means that if the function errors for any reason we can handle is 'gracefully'
- -- Meaning we know the error occured and was expecting it so now we can make/ execute our fallback plan.
- -- Which I setup a tiny bit of Error handling down below without first encountering an error, I used error prevention.
- -- Of course if it does for some odd reason fail, it will print the reason for failing as we state below.
- -- and yes, you can stack multiple PCalls within a single one if you really wanted or needed to...
- CoinData=dataStore:GetAsync(player.UserId.."-Coins") -- This is the :GetAsync() function/ Method I mentioned...
- -- At the start of the script. This allows us to call a function predetermined by Roblox that..
- -- will get data from a table, the table is the one we created called PlayerData.
- -- This table is stored within the DataStoreService and can get accessed by the :GetDataStore()
- -- Method/ Function I listed at the start of the script.
- -- The :GetAsync() Function requires a Key to index the value of. ~To get the value of the key, provide the key~
- -- Our Keys should be unique since every players data will be saved within this table.
- -- My key is the Players User ID plus -Coins so.. (1234567890-Coins)
- -- Until we set the Value of the Key then it will be equal to nothing or nil.
- -- We can use any Value Type we want, we could even have it return a function if we needed too...
- -- For now we're using normal ole IntValues or Numerical Values.
- end)
- if success and CoinData~=nil then -- If the Protected/ PCall function from above was successful at returning the data we requested...
- -- and the Value isn't equal to nothing at all then we'll set the leaderstats object(s) we created earlier to represent the value...
- -- Returned by the PCall/ Protected Function, A.K.A. The CoinData, that local we made to temporarily store the data...
- -- It's now useful like so...
- warn("The player "..player.Name.." has gotten the coins value data using the key: "..tostring(player.UserId).."-Coins")
- warn("Key = Value")
- warn("The Key used in the table: "..tostring(player.UserId).."-Coins")
- warn("The Value of the Key: "..tostring(CoinData))
- -- Ignore the warnings I made above this was also to help out but they can be removed.
- Coins.Value=CoinData -- This is where we modify that IntValue we created earlier...
- -- We're changing it's Value to the CoinData or Data returned by the PCall using our Temporary local/ placeholder.
- elseif CoinData==nil then -- Error Prevention, Preventing it from doing nothing if it's nil.
- warn("This player doesn't have a value stored on the key: "..tostring(player.UserId).."-Coins")
- local success, errormessage=pcall(function() -- Another PCall to protect the PCall it's inside of...
- dataStore:SetAsync(player.UserId.."-Coins",0) -- Setting a value that isn't nil...
- end)
- if success then
- warn("We've set a numerical value of zero to hold the place of nil instead.")
- Coins.Value=CoinData -- If we was able to succesfully set the Value of the Key we passed then we'll...
- -- Set the IntValue on the leaderstats to this number.
- else
- warn(errormessage) -- If the SetAsync Fails it'll warn the error specific message now.
- end
- else
- warn(errormessage) -- If the GetAsync Fails it'll warn the error specific message now.
- end
- end)
- game:GetService("Players").PlayerRemoving:Connect(function(plr)
- -- Same as PlayerAdded just for whenever a player is leaving...
- -- Same information applies here...
- local success, errormessage=pcall(function()
- dataStore:SetAsync(plr.UserId.."-Coins",plr:WaitForChild("leaderstats"):FindFirstChild("Coins").Value)
- end)
- if success then
- warn(plr.Name.." has left and their Coins data was saved! ")
- warn("Key = Value")
- warn("The Key used in the table: "..tostring(plr.UserId).."-Coins")
- warn("The New Value of the Key: "..tostring(plr:WaitForChild("leaderstats"):FindFirstChild("Coins").Value))
- else
- warn(errormessage)
- end
- end)
- --[[SD.OnServerEvent:Connect(function(Plr)
- local success, errormessage=pcall(function()
- dataStore:SetAsync(Plr.UserId.."-RoleplayName",Plr.leaderstats.Coins.Value)
- end)
- if success then
- else
- warn(errormessage)
- end
- end)]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement