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