I feel like this script was too easy to script

Hi! I want to make a DataStore saving and setting script, so I just put it along inside the leaderstats script. This was my first time doing it by myself, and I thought it was too… easy. Nothing is wrong with it in game, I just feel like theres a protection or something like that to add. Heres the script:

local dss = game:GetService("DataStoreService")
local moneyDss = dss:GetOrderedDataStore("Money")

game.Players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	
	local latestStage = Instance.new("IntValue", plr)
	latestStage.Name = "latestStage"
	
	local stage = Instance.new("IntValue", leaderstats)
	stage.Name = "Stage"
	
	local money = Instance.new("IntValue", leaderstats)
	money.Name = "Money"
	
	local moneyAmt = moneyDss:GetAsync(plr.UserId)
	money.Value = moneyAmt
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local success, err = pcall(function()
		moneyDss:SetAsync(plr.UserId, plr.leaderstats.Money.Value)
	end)
	
	if success then
		print("Player Stats Saved Successfully!")
	else
		warn(err)
	end
	
end)


So yeah, thats the script. Just setting it and saving it. No big deal to me.

3 Likes

There’s a couple of things you can add, but one of the more important things is a pcall around the DataStore:GetAsync call:

local success, output = pcall(function()
   return moneyDss:GetAsync(plr.UserId) -- returns the amount of money the player has (if any)
end)

if success then
   if output then -- check if the player has any money in their datastore
       money.Value = output -- if the call was successful, `output` becomes the returned value (the player's money)
   end
else
   warn(output) -- output is the error message
end

Arguably, by the way your script is written, adding a pcall to the GetAsync call wouldn’t actually be any beneficial unless you decide to add more code beneath it.

3 Likes

If you really wanted to, you could do this:

local Folders = {"leaderstats","latestStage"} -- Folders to Create
local Values  = { -- List of Values to Apply
    Stage = {"leaderstats", "IntValue"},
    Money = {"leaderstats", "IntValue"},
}

local function createScore(main)
    for _,v in Folders do -- Iterates through Folders tabkle
       local Folder = Instance.new("Folder"); Folder.Name = v -- Creates Folders
       Folder.Parent = main -- Applys Parent
    end
    for i,v in Values do -- Iterates through values
       local Folder = Instance.new(v[2]); Folder.Name = i -- Applies Properties based of of info
       Folder.Parent = main[v[1]] -- looks for Instance under Name
    end
end

game.Players.PlayerAdded:Connect(function(plr)
    createScore(plr) -- fires function
end)

The System is very fast, while yes the other is faster, this will automatically create them, plus its usually a .0003 difference in speed, so you dont need to worry About it being slow, the only thing would probably be errors, which can easily be fixed.

A benefitical addition to GetAsync would be with pcall() as was stated above, pcall() helps you handle errors, since GetAsync can fail sometimes, Although the Method Above is valid, you can do this;

local success, data = pcall(moneyDss.GetAsync, moneyDss, plr.UserId)

Which you can apply as such:

game.Players.PlayerAdded:Connect(function(plr)
    createScore(plr) -- fires function

    local success, data = pcall(moneyDss.GetAsync, moneyDss, plr.UserId) -- Gets Data

    if success then -- if GetAsync succeeded
        if data then -- if Player has Data
        else -- if Player has no Data
        end
    else -- if failed
    end
end)
2 Likes

In addition, I highly recommend you implement a BindToClose to your script, so the data saves when a player loses connection or when the server shuts down.

tutorial:

You should wrap the data store requests in a pcall() to reassure No data gets wiped

You might wanna try using ProfileService as well, it’s a very nice module and is very easy to use.

It offers a lot of DataStore loss protection and DataStore v2 APIs under the hood.