my gane is experiencing lag due to large amounts of data being stored, for example the top player in my game has 3QI+ cash and is experiencing alot of lag, can someone help me add a pcall function to my data save script:
local leaderstatsDataStore = dataStoreService:GetGlobalDataStore("PlayerStats")
local loaded = {}
game.Players.PlayerAdded:connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
if player.UserId > 0 and player.Parent then
local leaderstatsData = leaderstatsDataStore:GetAsync(player.UserId)
if leaderstatsData ~= "Request rejected" then
if leaderstatsData then
for i, stat in ipairs(leaderstats:GetChildren()) do
local value = leaderstatsData[stat.Name]
if value then
stat.Value = value
end
end
end
loaded[player] = true
end
end
end)
game.Players.PlayerRemoving:connect(function(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
if loaded[player] then
local leaderstatsData = {}
for i, stat in ipairs(leaderstats:GetChildren()) do
leaderstatsData[stat.Name] = stat.Value
end
leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData)
end
end
loaded[player] = nil
end)```
local leaderstatsDataStore = dataStoreService:GetGlobalDataStore("PlayerStats")
local loaded = {}
game.Players.PlayerAdded:Connect(function(player)
local success, error = pcall(function()
local leaderstats = player:WaitForChild("leaderstats")
if player.UserId > 0 and player.Parent then
local leaderstatsData = leaderstatsDataStore:GetAsync(player.UserId)
if leaderstatsData ~= "Request rejected" then
if leaderstatsData then
for i, stat in ipairs(leaderstats:GetChildren()) do
local value = leaderstatsData[stat.Name]
if value then
stat.Value = value
end
end
end
loaded[player] = true
end
end
end)
if not success then
warn("Error while processing PlayerAdded for", player, error)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, error = pcall(function()
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
if loaded[player] then
local leaderstatsData = {}
for i, stat in ipairs(leaderstats:GetChildren()) do
leaderstatsData[stat.Name] = stat.Value
end
leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData)
end
end
loaded[player] = nil
end)
if not success then
warn("Error while processing PlayerRemoving for", player, error)
end
end)
It’s not a typo per se, but you wouldn’t use error as one of the variable names for the pcall function because error exists as a Lua function.
Rather, I would recommend to change error to something like err0r or err.
I have amended the code to include this change:
local leaderstatsDataStore = dataStoreService:GetGlobalDataStore("PlayerStats")
local loaded = {}
game.Players.PlayerAdded:Connect(function(player)
local success, err0r = pcall(function()
local leaderstats = player:WaitForChild("leaderstats")
if player.UserId > 0 and player.Parent then
local leaderstatsData = leaderstatsDataStore:GetAsync(player.UserId)
if leaderstatsData ~= "Request rejected" then
if leaderstatsData then
for i, stat in ipairs(leaderstats:GetChildren()) do
local value = leaderstatsData[stat.Name]
if value then
stat.Value = value
end
end
end
loaded[player] = true
end
end
end)
if not success then
warn("Error while processing PlayerAdded for", player, err0r)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err0r = pcall(function()
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
if loaded[player] then
local leaderstatsData = {}
for i, stat in ipairs(leaderstats:GetChildren()) do
leaderstatsData[stat.Name] = stat.Value
end
leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData)
end
end
loaded[player] = nil
end)
if not success then
warn("Error while processing PlayerRemoving for", player, err0r)
end
end)
Too much inside the pcall(). Use that to save/load the data and end it . The rest of that can go after if the pcall was successful. Ideally just used for the data save/load process.
oh thanks haha i didnt know I used a lua function sicne I typed the script in here it didnt show me I typed a function by accident but thx for the info