Hi, I’m a novice developer making a ‘PLS JOIN’ sort of game where you can advertise your groups and such. My issue is my PlayerRemoving
event isn’t listening to whenever it fires so data couldn’t be saved. I’m using IncrementAsync()
instead of SetAsync()
to make sure DataStore requests aren’t throttled. Here is the script:
--Whenever I mention X0, I mean the player's GroupBux (This is to prevent exploiters from understanding what's in the client-sided scripts)
local players = game:GetService('Players')
local dss = game:GetService('DataStoreService')
local DataStores={
Raised = dss:GetOrderedDataStore('Raised');
Donated = dss:GetOrderedDataStore('Donated');
GroupBux = dss:GetOrderedDataStore('GroupBux');
FirstPlayerData = {};
}
players.PlayerAdded:Connect(function(player)
local lstats = Instance.new('Folder',player)
lstats.Name = 'leaderstats'
local donated = Instance.new('IntValue',lstats)
donated.Name = 'Donated'
local raised = Instance.new('IntValue',lstats)
raised.Name = 'Raised'
local playerData = {[1]='Raised',[2]='Donated',[3]='GroupBux'}
local received = {}
for i = 1, #playerData do
local currentDataStore = DataStores[playerData[i]]::OrderedDataStore
repeat
local options = Instance.new('DataStoreGetOptions')
options.UseCache = true
local suc, object = xpcall(function()
local result = currentDataStore:GetAsync(player.UserId,options)
received[playerData[i]] = result
return result
end,function(err)
warn(err)
wait(.1)
end)
until suc or not game.Players:FindFirstChild(player.Name,false)
end
donated.Value = received[2] or 0
raised.Value = received[1] or 0
player:SetAttribute('X0',received[3]or 0)
DataStores.FirstPlayerData[player.UserId]={Raised=received[1],Donated=received[2],GroupBux=received[3]}
end)
players.PlayerRemoving:Connect(function(player)
local leaderstats = player:FindFirstChild('leaderstats')
if leaderstats then
for key, dataStore in pairs(DataStores) do
if dataStore ~= DataStores.FirstPlayerData then
local value = leaderstats:FindFirstChild(dataStore.Name)
if value then
local difference = value.Value - DataStores.FirstPlayerData[player.UserId][dataStore.Name]
repeat
local success, err = pcall(function()
dataStore:IncrementAsync(player.UserId, difference)
end)
if not success then
warn(err)
wait(0.4)
end
until success
end
end
end
end
end)
I know this because there are no print statements raising. I would very much appreciate if you could help me out and tell me what is going on.