Pcall Problem, trying to find the best way to use the pcalls I have to optimise my game

Hi, I am a new scripter and I wanted to know what would be the best way to use these pcalls (I have to many I think)

Right now my script is like this

pcall(function()
player_data = dataStores:GetAsync(player.UserId…“-Coins”)
end)

pcall(function()
	player_data = dataStores:GetAsync(player.UserId.."-Wins")
end)

pcall(function()
	weaponsData = dataStores:GetAsync(player.UserId.."-Weps")
end)

pcall(function()
	equippedData = dataStores:GetAsync(player.UserId.."-EquippedValue")
end)

Would it be better if it was

pcall(function()

	player_data = dataStores:GetAsync(player.UserId.."-Coins")
	player_data = dataStores:GetAsync(player.UserId.."-Wins")
	weaponsData = dataStores:GetAsync(player.UserId.."-Weps")
	equippedData = dataStores:GetAsync(player.UserId.."-EquippedValue")
end)

Or a different way, I need to optimise my game and also not lose any data (I have a bind to close event)

It would be better if you did
pcall(dataStores.GetAsync,dataStores,player.UserId)
And would save all data in a table.

1 Like

How do I save them in a table, could you give can example please.

You can get the returns from a pcall.
local _, playerData = pcall(dataStores.GetAsync, dataStores, player.UserId)
And you you would save data like this instead.
pcall(dataStores.SetAsync, dataStores, player.UserId, playerData)
where playerData is a table containing whatever you want to save.

1 Like

local _, playerData = pcall(dataStores.GetAsync, dataStores, player.UserId)
pcall(dataStores.SetAsync, dataStores, player.UserId, playerData)

how do i define whats inside of playerData, like how to do i say what i want to save

sorry, new to scripting…

No worries.
So in your case, I would use a dictionary, which is a table of key-value pairs. It’s like a collection of objects that I can look up by name. Since I don’t have the part of your script where you are saving data, this is a rough example of how you can use them.

local playerData = {}
playerData.weapons = --your weaponsData
playerData.equipped = --your equippedData
playerData.player = --your player_data

And to access them, it’s just the reverse. Pretty simple. I can’t make a proper example here because I have no clue what the data actually is, but hopefully you’ll understand anyways.

playerData.player.money = playerData.player.money + 100

I am confused as to what I would replace “–your weaponsData” with

like this?

local _, playerData = pcall(dataStores.GetAsync, dataStores, player.UserId)
pcall(dataStores.SetAsync, dataStores, player.UserId, playerData)
local playerData = {}
playerData.weapons = “-Weps”
playerData.equipped = “-Equipped”
playerData.player = “-Coins”

end

btw my part of my script that saves datastores is in post 1,

No. It would be the data itself. Show me where you are saving (or using) the data and I can provide a better example.

pcall(function()
player_data = dataStores:GetAsync(player.UserId…“-Coins”)
end)

pcall(function()
Winsdata = dataStores:GetAsync(player.UserId…“-Wins”)
end)

pcall(function()
weaponsData = dataStores:GetAsync(player.UserId…“-Weps”)
end)

pcall(function()
equippedData = dataStores:GetAsync(player.UserId…“-EquippedValue”)
end)

local playerData = {}
playerData.weapons = weaponsData
playerData.equipped = equippedData
playerData.player = player_data
playerData.wins = Winsdata

Also, a bit of feedback, you might want to standardize your naming conventions.
player_data should be playerData to match the rest, and Winsdata should be winsData.

2 Likes

Thank you! I will do a test to see if this works.

local _, playerData = pcall(dataStores.GetAsync, dataStores, player.UserId)
	pcall(dataStores.SetAsync, dataStores, player.UserId, playerData)
	
	local playerData = {}
	playerData.weapons = weaponsData
	playerData.equipped = equippedData
	playerData.player = coinsData
	playerData.wins = winsData
	

umm where is the function()
because i tested and i think its not working because of no function

Yeah still no change. help? anybody?

umm… anybody, willing to give me a hand?

You’re using data stores incorrectly, you don’t save right after you loaded the data.

@JarodOfOrbiter was trying to say something like this:

To set up

local playerData = {}
playerData.weapons = weaponsData -- set these as the appropriate value, don't copy and paste
playerData.equipped = equippedData
playerData.player = player_data
playerData.wins = Winsdata

To load

local success, data = pcall(dataStores.GetAsync, "key", "value")
-- replace "key" and "value" with the data store's keysand value

You don’t use “function” here because the function is DataStore.GetAsync/Data.SetAsync. You only use function when you’re doing something like this:

local success, err = pcall(function()
    return nil
end)

It just creates an anonymous function.

To save

local success, message = pcall(dataStores.SetAsync, "key", "value")
-- like the previous one, replace "key" and "value" with the key and value

Your end script should look something like this:

-- services to use
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStores = dataStoreService:GetDataStore("data store name here")

-- functions to get/save player data
local function SavePlayerData(player: Player)
    local playerData = {}
    playerData.weapons = -- the player's weapons
    playerData.equipped = -- the equipped weapons the player has
    playerData.player = {} -- the player's data here (coins etc): example: playerData.player = { money = 100, cash = 50 }
    playerData.wins = -- the number of wins the player has

    local success, message = pcall(dataStores.SetAsync, player.UserId, playerData) -- the playerdata to the player's userid data store

    if not success then -- checks if it was able to successfully save the data
       warn("unable to save data: " .. message)
    end
end

local function GetPlayerData(player: Player)
    local success, result = pcall(dataStores.GetAsync, player.UserId)

    if not success then -- like save async, this checks if getting data was successful
       warn("unable to load data: " .. result)
    else
       if result ~= nil then
          -- do stuff with the player's data
       end
    end
end

-- connect the functions to the events
players.PlayerAdded:Connect(GetPlayerData)
players.PlayerRemoving:Connect(SavePlayerData)

-- incase something happens while the game is shutting down
game:BindToClose(function()
   for _, plr in pairs(players:GetPlayers()) do -- loop through the players
      coroutine.wrap(function() -- this is so the thread doesn't yield
          SavePlayerData(plr) -- save their data
      end)() 
   end
end)
1 Like