Help on this datastore script

So I’ve made a script which saves a bool value when player leaves the game to determine if to wipe the players data if they are playing for first time or not, but for some reason it doesn’t work please help!

	local swipe = Instance.new("BoolValue")
    swipe.Name = "Purged"
    swipe.Parent = Player

    local Data = {}
    
    local Succes, Error = pcall(function()
          Data = WeaponData:GetAsync(Player.UserId)
    end)

    if Succes then
		Player.Purged.Value = Data
    else
        print("Fail")
        warn(Error)
    end
	
	wait(1)
	if Player:WaitForChild("Purged").Value == false then
			Player.Stats.Coins.Value = 500
		else
			print("Clear")
		end
end)
game.Players.PlayerRemoving:connect(function(Player)
	
	local Data = {}
	
    local Success, Error = pcall(function()
       WeaponData:SetAsync(Player.UserId, Data)
    end)

	if Success then
		Data["Purged"] = true
	else
		print('Error')
	end
end)

Your setting async before you change their data so your just setting a blank table

what do you mean by that I cant put Succes before

You first defined Data in what I’m assuming is a missing

game.Players.PlayerAdded:connect(function(Player)

at the top. Then in the PlayerRemoving block, you define Data again, which shadows the original one. Therefore, you’re saving an empty table.

The script works to save and all but these lines

	if Player:WaitForChild("Purged").Value == false then
			Player.Stats.Coins.Value = 500
		else
			print("Clear")
		end

Are the lines that don’t seem to work

The answer is: You can’t save boolvalues.
The solution is the store it in a other way like 1 or 0 or “true” or “false”

I have saved bool values in my previous projects but I saved like more than 1

Can you print the error from the pcall?

Edit: if it does even error

There is no error in the code.

Try adding

game:BindToClose(function()
  wait(5)
end)

The more I read your post, the less sense it makes. Could you reiterate what exactly should happen and what exactly is happening?

The bool value (Purged) saves through datastores but when I try to use it’s values to determine what happens when you join the game those lines don’t do anything

What I said in my first post is that Purged never saves. You’re saving an empty table. Therefore, when you rejoin the game,

Data = WeaponData:GetAsync(Player.UserId)

is retrieving that empty table. Then by doing

Player.Purged.Value = Data

Purged will always be true because lua considers all values to be true unless they are explicitly false or nil. And if Purged is always true, then you never hit

Player.Stats.Coins.Value = 500

You used SetAsync before actually setting any data in the table Data:

local Data = {}
	
local Success, Error = pcall(function()
   WeaponData:SetAsync(Player.UserId, Data)
end)

if Success then
	Data["Purged"] = true -- You set it after SetAsync
else
	print('Error')
end

Simply move it back to fix it:

local Data = {}
Data["Purged"] = true -- It's here now

local Success, Error = pcall(function()
   WeaponData:SetAsync(Player.UserId, Data)
end)

if Success then
	 print("success")
else
	print('Error')
end