So I have this data store script. It works but I was simply wondering if I could try something different for it. Here’s the script:
local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("PlayerData")
local function saveData(player)
local Table = {player.leaderstats.Coins.Value}
local success, errormessage = pcall(function()
DataStore:SetAsync(player.UserId, Table)
end)
if success then
print("Successfully saved your data!")
else
print("An error occurred while saving your data. Please try again later.")
warn(errormessage)
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
coins.Value = 0
local data
local success, errormessage = pcall(function()
data = DataStore:GetAsync(player.UserId)
end)
if success and data then
coins.Value = data[1]
print("Successfully found your data")
print(DataStore, data)
else
print("We couldn't find any data for you.")
warn(errormessage)
end
while wait(120) do
saveData(player)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
saveData(player)
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
saveData(player)
end
end)
What I’m want to do for the script is to replace the regular protected call. I’m just asking if it’s possible to use xpcall()
instead of pcall()
for the script.