Help with Pcall

My pcall function isn’t with my leaderstats! No output errors

local ClicksDS = game:GetService("DataStoreService"):GetDataStore("ClicksDS")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local Clicks = Instance.new("IntValue", leaderstats)
	Clicks.Name = "Clicks"
	
	local ClicksData
	local Success,Error = pcall(function()
		ClicksData = ClicksDS:GetAsync(player.UserId)
	end)
	
	if Success then
		Clicks.Value = ClicksData
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ClicksDS:SetAsync(plr.UserId, plr.leaderstats.Clicks.Value)
end)

Wdym? In the title it says “Help with Pcall” and then you say that it’s working?

If it is working why is the title “Help with Pcall”

I meant isn’t working @bansa168

I’ve meant to say isn’t working, I had a type. Aplogies

Can you just print out the ClicksData and tell me what it outputs?

It prints out as 0, oh wait! I think my stats accidently resetted to 0. I will check if thats the problem.

In the future make sure you use prints to debug they help a ton with this kinda stuff.

Yeah, I accidently reset my stats. Although I did print success I never printed ClicksData. Thank you sir.

1 Like

I have some additional details:

in:

game.Players.PlayerRemoving:Connect(function(plr)
	ClicksDS:SetAsync(plr.UserId, plr.leaderstats.Clicks.Value)
end)

replace with:

game.Players.PlayerRemoving:Connect(function(plr)
        local data = {}

	    data.Clicks = plr.leaderstats.Clicks.Value

        local s, e = pcall(function()
            ClickDS:SetAsync(plr.UserId, data) --probably data needs to be a table
        end)
 
       if(s) then
          print("Succes!")
       else
          warn("We Didnt Save Because: "..e)
       end
end)

Ive added that already. But however I didn’t use a table/array.