I am attempting to make a script which adds +1 point to a specific leaderstat whenever someone joins the game. simply put, this is for a “Joins” leaderstat which displays how many times a user has joined the game. I have not been able to find much useful information in devforum about this, and so far none of the things I have tried work. Here is my datastore, which works perfectly:
local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrkey = "id_"..plr.userId
local save1 = plr.leaderstats.Time
local GetSaved = ds:GetAsync(plrkey)
if GetSaved then
save1.Value = GetSaved[1]
else
local NumberForSaving = {save1.Value}
ds:GetAsync(plrkey, NumberForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
ds:SetAsync("id_"..plr.userId, {plr.leaderstats.Time.Value})
end)
local ds2 = game:GetService("DataStoreService"):GetDataStore("JoinCount")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrkey = "id_"..plr.userId
local save2 = plr.leaderstats.Joins
local GetSaved = ds2:GetAsync(plrkey)
if GetSaved then
save2.Value = GetSaved[1]
else
local NumberForSaving = {save2.Value}
ds2:GetAsync(plrkey, NumberForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
ds2:SetAsync("id_"..plr.userId, {plr.leaderstats.Joins.Value})
end)
and here are the things I have tried:
increment async, i placed after the playeradded if statement and also inside of it, neither worked.
ds2:IncrementAsync("Joins", 1)
and then this short function which a friend gave to me, i placed at the bottom of my datastore aside from everything else. no errors in any of this but none of it works.
game.Players.PlayerAdded:Connect(function(player)
local ldb = player:WaitForChild('leaderstats')
local Joins_ = ldb:WaitForChild('Joins')
local data = ds2:GetAsync(player.UserId)
local success, err = pcall(function()
end)
if success then
Joins_.Value = data[1]+Joins_.Value
else
print("The player has no data!") -- The default will be set to 0
end
end)
any explanation or help would be greatly appreciated!