First time Data Store

Hi, I’m trying to create data store in my game but it’s my first time and my script looks like that:

local UserID = game.Players.LocalPlayer.UserId
local DataStoreService = game:GetService("DataStoreService")
local OBandPoints = DataStoreService:GetDataStore("OBandPoints")


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

	local points = Instance.new("IntValue", leaderstats)
	points.Name = "Points"
	local success, help = pcall(function()
		OBandPoints:GetAsync(UserID)
	end)
	if success then
		print(123)
	end
	points.Value = 0
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, helpHereToo = pcall(function()
		OBandPoints:SetAsync(UserID, player.leaderstats.Points.Value)
	end)
	if success then
		print(1234)
	end
end)

I have read many websites about Data Stores | Roblox Creator Documentation but I can’t understand it. If anyone could explain to me what I’m doing wrong. I don’t understand pcalls primarily…

Wait did I used LocalPlayer in Script?

Well, pcall stands for protective call or something. It’s used to prevent errors from going out into the console and stopping the current script. It returns 2 things:

  • A bool indicating if the block of code in the call ran successfully. (true if successful, false if not and errored)
  • An error message that came from the block of code if it didn’t run successfully and errored.

Usage:

local SUCCESS, ERROR = pcall(function()
     --your block of code you want to "protect"
end)

if (SUCCESS) then
    --if the call was executed successfully
else
    --the call must have not been successful so there must be an error
    warn(ERROR) -- logs the error into the console. That error tells you why the call failed.
end

OH YES.
P.S - You can return values to the call

local SUCCESS, OTHER = pcall(function()
     --your block of code you want to "protect"
     return "hi"
end)

if (SUCCESS) then
    --if the call was executed successfully
    print(OTHER) -- prints "hi" because you returned "hi" to the variable
else
    --the call must have not been successful so there must be an error
    warn(OTHER) -- logs the error into the console. That error tells you why the call failed.
   -- When it fails, "OTHER" becomes the error
end

Now onto the Datastore.

I see you did this, :GetAsync() returns the data you saved when the player left. So, you should either return the data to the call or define a variable outside and set the data to that.

Example:

--Variable
local Data
--pcall blah
Data = Datastore:GetAsync(ID)

--If the call was successful, you have the data you saved in the variable called "Data"

--OR using the returned method

local success, data = pcall(function()
    return Datastore:GetAsync(ID)
end)

if (success) then
    --poof, data is now the data you saved
    print(data)
else
    --ouch, data is an error now, we need to print it to find out why the call failed
    warn(data)
end

Some information might be wrong, if there’s any misleading info, please tell me so I can fix it.

Sorry I couldn’t go to :SetAsync(). I’m currently in a rush. I’ll come back to tell you about it if someone else doesn’t.

And yep, use the parameter from PlayerAdded.

I USED CONSOLE, IT’S MEANT TO BE OUTPUT.

3 Likes

Pro tip, don’t use SetAsync(), use UpdateAsync().

Article is here.

I know that from Data Stores | Roblox Creator Documentation idk why i didn’t used that

This scripts work sorry if I can’t format it correctly. Let me know if you have any questions

local DataStoreService = game:GetService(“DataStoreService”)
local OBandPoints = DataStoreService:GetDataStore(“OBandPoints”)

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"

local points = Instance.new("IntValue", leaderstats)
points.Name = "Points"

local UserId = "PlayerID:"..player.UserId

local GetData

local success, help = pcall(function()
	GetData = OBandPoints:GetAsync(UserId)
end)

if success then
	points.Value = GetData
else
	print("Error loading player data "..help)
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local UserId = “PlayerID:”…player.UserId

local success, helpHereToo = pcall(function()
	OBandPoints:SetAsync(UserId, player.leaderstats.Points.Value)
end)

if success then
	print("Data saved successfully")
else
	print("Error saving data.."..helpHereToo)
end

end)

Thank you for dedicated time it will definitely be useful

1 Like

I’ll try it thanks for dedicated time too :smile: