I am making “an app” like steam and I am using BoolValues to check if the player owns the game/dlc. For some reason, even if I wasn’t in the game, the BoolValue was still true. Here is the code:
Database:
local DSS = game:GetService("DataStoreService")
local SuperFunObby = DSS:GetDataStore("SuperFunObby")
game.Players.PlayerAdded:Connect(function(plr)
local pSuperFunObby = SuperFunObby:GetAsync(plr.UserId)or false ---Here is a problem (or false)
local leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"
local SuperFunObbyValue = Instance.new("BoolValue")
SuperFunObbyValue.Name = "SuperFunObby"
SuperFunObbyValue.Value = pSuperFunObby
SuperFunObbyValue.Parent = leaderstats
SuperFunObbyValue.Changed:Connect(function(z)
SuperFunObby:SetAsync(plr.UserId, z)
print("SuperFunObby Saved!")
end)
end)
When the player buys the game (there is a GUI and a localscript triggers the event):
Event.OnServerEvent:Connect(function(Plr)
local SuperFunObby = Plr:WaitForChild("leaderstats"):WaitForChild("SuperFunObby")
SuperFunObby.Value = true ---And the value doesn't go to true for some reason...
end)
Thank you so much! This worked well. I already had the value to true, probably from the previous script, I am not sure, I am gonna have to test that. But after I set it to false, everything worked flawlessly. Once again, thank you!
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local SuperFunObby = DSS:GetDataStore("SuperFunObby")
Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local SuperFunObbyValue = Instance.new("BoolValue")
SuperFunObbyValue.Name = "SuperFunObby"
SuperFunObbyValue.Parent = leaderstats
local Success, Result = pcall(function()
return SuperFunObby:GetAsync(Player.UserId)
end)
if Success then
if Result then
SuperFunObbyValue.Value = Result
else
SuperFunObbyValue.Value = false
end
else
warn(Result)
end
SuperFunObbyValue.Changed:Connect(function(NewValue)
local Success, Result = pcall(function()
return SuperFunObby:SetAsync(Player.UserId, NewValue)
end)
if Success then
if Result then
print(Result)
end
else
warn(Result)
end
end)
end)
You should wrap DataStore requests inside pcall() calls as they are prone to errors. I’ve also made some other minor optimisations like utilising the “new value” parameter of the “.Changed” event.