Player data not saving

Your script crashes the game (in studio) every time I stop play-test the game, so that ain’t good.

Also what’s up with that?

And all that?

Scrap all that or simplify it!

Anyways here’s a fix* (1):

local DataStoreService = game:GetService("DataStoreService")
local GameDataStore = DataStoreService:GetDataStore("GameSaveData")

game.Players.PlayerAdded:Connect(function(plr)
	local playerid = plr.UserId
	local data = GameDataStore:GetAsync("Player_"..playerid)
	
	local folder = Instance.new("Folder",plr)
	folder.Name = "leaderstats"

	local TotalClicks = Instance.new("NumberValue",folder)
	TotalClicks.Value = 0
	TotalClicks.Name = "Total Clicks"

	local Clicks = Instance.new("NumberValue",folder)
	Clicks.Value = 0
	Clicks.Name = "Clicks"

	local Level = Instance.new("NumberValue",folder)
	Level.Value = 1
	Level.Name = "Level"

	local success, err = pcall(function()
		if data == nil then
			warn("Warning, the data is nil!")
		end
	end)
	
	if success ~= true then
		warn("Error while getting data: "..err)
	else 
		return
	end
	
	print(success) -- is considered true, luckily
	print(err) -- is considered nil, luckily
end)

game.Players.PlayerRemoving:Connect(function(plr: Player)
	--Values--
	local F = plr:FindFirstChild("leaderstats")
	local Value1 = F.Clicks
	local Value2 = F["Total Clicks"]
	local Value3 = F.Level
	----------
	GameDataStore:SetAsync(tostring(plr.UserId),{
		Value1.Value,
		Value2.Value,
		Value3.Value
	})
end)

I tested myself and it worked fine* (2).

Blurred words about what they mean in certain terms

Fix (1): Script that fixes the bug you have.
Fine (2): Without having any errors or relying on the script to not work