Adding leaderstats when a player joins the game

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!

You can just add 1 to the data when it was loaded.

save2.Value = GetSaved[1] + 1

“Joins” isn’t the right key here, you need something with a UserId like you did before.

Also I don’t understand this:

if GetSaved then
else
	local NumberForSaving = {save2.Value}
	ds2:GetAsync(plrkey, NumberForSaving)
end

The things you do if there is no data don’t make sense, you should set the NumberValue to a default value.

1 Like

it saves data auto then player leaves that means you can do anything like: if player touches a part then he gets a coin and then he leaves it will auto save it and you dont need to use async you can just use part.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then game.Players:FindFirstChild(hit.Parent.Name).leaderstats.Coins.Value += 1 end end)

1 Like

wow…it really was that simple. I should try to learn more before making posts like these lol. thank you so much that worked perfectly. and about that last part. idk much about that either. im pretty sure i got this datastore from a youtube video a year ago before i knew much about scripting and didnt question it. it works so i guess its all good?

I was considering doing this, but I preferred to have a cleaner script and not as much mess or script usage in the game. my game currently has 2500+ scripts in it and im trying what i can to not add anymore, instead just add to the existing. I appreciate the suggestion though!

The code you have isn’t really that good and could be improved much. If you use code from other people anyways, you can also directly use professional modules that handle datastores for you. I suggest checking out ProfileService.

1 Like

alright i will look into it, thank you