How do i add a pcall function to my data save?

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)```
1 Like

can you put that in the three ` please? i cant really copy that :rofl:

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)

sorry it kept deleting it idk why heres the full line I think this is what you ment right?

it also cut a line of cod I typed sorry I added the small part of cod that missed in the first 1 intothe second code

i think it worked! im getting less lag when getting data added.

great to hear!! btw I dont really know how to make the lag less than that sorry

never mind, nothing saved, hmmmm :rofl:

oof ha ha sorry idk maybe I typed a line of code wrong maybe check for that in the output

no output, just didnt save when i joined back

It did save with the old script right?

maybe someone sees an error in the script I just made bc I believe it should save idk what where I made a typo

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)
2 Likes

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.

My bad wrong player … @ xordium

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.