Player Count Leaderstats

local Store = DSS:GetDataStore("CashStore1")

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Crash"
	cash.Parent = folder
	
    local PlayerCount = Instance.new("NumberValue")
    PlayerCount.Name = "Current Players :"
    PlayerCount.Parent = player

    coroutine.wrap(function()
        while wait(1) do
        PlayerCount.Value = #game.Players:GetPlayers()
        end
     end)()

	local data
	
	local sucess, err = pcall(function()
		data = Store:GetAsync(player.UserId)
		
		if data and data ~= 0 then
			print("Data sucessfully updated for "..player.DisplayName)
			cash.Value = data
		else
			print(player.DisplayName.." doesn't have any saved data. No Data was updated.")
			cash.Value = 0
		end
	end)
	
	if sucess then
		print("Updating value complete")
	else
		warn(err)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		Store:SetAsync(player.UserId, player.leaderstats.Crash.Value)
	end)
	
	if success then
		print(player.DisplayName.."'s crash data was sucessfully saved!")
	else
		warn(err)
	end
end)

still doesn’t display the player count

It is supposed to change it every time a player gets added, weird it doesn’t work

Your for loop is done incorrectly, recheck it

I think using a for loop would work. I can’t test right now since I’m on mobile.

1 Like

I changed it every time someone joins to reduce lag

1 Like

did you try mine?
[Char Limit]

1 Like

yea I did I meant to reply to you as well, it did not work.

Wdym exactly by didnt work, the value isn’t changing?

just the leaderboard isn’t popping up that’s what’s supposed to happen, like example a simulator game has cash, gems on the same leaderboard. looks like this :
image

1 Like

I didnt make it pop up on the leaderstats, you didnt say it should appear, just change the parent and use the loop i provided earlier

1 Like

I think I missed the local dss on the last time I sent, if that has do with it.

local Store = DSS:GetDataStore("CashStore1")

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Crash"
	cash.Parent = folder
	
	local data
	
	local sucess, err = pcall(function()
		data = Store:GetAsync(player.UserId)
		
		if data and data ~= 0 then
			print("Data sucessfully updated for "..player.DisplayName)
			cash.Value = data
		else
			print(player.DisplayName.." doesn't have any saved data. No Data was updated.")
			cash.Value = 0
		end
	end)
	
	if sucess then
		print("Updating value complete")
	else
		warn(err)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		Store:SetAsync(player.UserId, player.leaderstats.Crash.Value)
	end)
	
	if success then
		print(player.DisplayName.."'s crash data was sucessfully saved!")
	else
		warn(err)
	end
end)
1 Like

oh well sorry if I wasn’t clear I wanted it to be on the leaderstats

1 Like

you have just to set the PlayerCount Value to the leaderstats Folder

local Store = DSS:GetDataStore("CashStore1")

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Crash"
	cash.Parent = folder
	
    local PlayerCount = Instance.new("NumberValue")
    PlayerCount.Name = "Current Players :"
    PlayerCount.Parent = folder

    coroutine.wrap(function()
        while wait(1) do
        PlayerCount.Value = #game.Players:GetPlayers()
        end
     end)()

	local data
	
	local sucess, err = pcall(function()
		data = Store:GetAsync(player.UserId)
		
		if data and data ~= 0 then
			print("Data sucessfully updated for "..player.DisplayName)
			cash.Value = data
		else
			print(player.DisplayName.." doesn't have any saved data. No Data was updated.")
			cash.Value = 0
		end
	end)
	
	if sucess then
		print("Updating value complete")
	else
		warn(err)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		Store:SetAsync(player.UserId, player.leaderstats.Crash.Value)
	end)
	
	if success then
		print(player.DisplayName.."'s crash data was sucessfully saved!")
	else
		warn(err)
	end
end)
2 Likes