Leaderstats assistance

I am trying to make a leaderboard which awards the players a point for every second they are in a specific area with data storing.
Can someone help me regarding this?

To Make A Leaderboard with DataStore,
You need to Use a script Like This :

local DataStoreService = game:GetService("DataStoreService") -- Define DataStore
local CurrencyDataStore = DataStoreService:GetDataStore("currencyPoints") -- Create DataStore

game.Players.PlayerAdded:Connect(function(Player)
	local Folder = Instance.new("Folder")
	Folder.Name = "leaderstats" -- Folder Name
	Folder.Parent = Player

	local PointsValue = Instance.new("IntValue")
	PointsValue.Name = "Points" -- Currency Name
	PointsValue.Parent = Folder

	local CurrentData -- Blank Var
	local Success,ErrorMessage = pcall(function()
		CurrentData = CurrencyDataStore:GetAsync(Player.UserId) -- We Get Data With UserId
	end)
	if Success and CurrentData ~= nil then
		PointsValue.Value = CurrentData -- We set it to Data
	else
		PointsValue.Value = 0 -- No Data So we Set it As 0
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local Success,Errormsg = pcall(function()
		CurrencyDataStore:SetAsync(Player.UserId,Player.leaderstats.Points.Value) -- Save the Points with UserId
	end)
	if not Success then
		warn(Errormsg)
	end
end)

game.ReplicatedStorage.AddPoint.OnServerEvent:Connect(function(Player)
	Player.leaderstats.Points.Value += 1
end)

local RunService = game:GetService("RunService")
local PlayersService = game:GetService("Players")
local Players = PlayersService:GetPlayers()

game:BindToClose(function()
	if RunService:IsStudio() then -- Running in Studio we Dont want to Multi Save.
		wait(1) -- Wait 1 Second so We can Save From the PlayerRemvoing function.
	else
		for _, LocalPlayer in pairs(Players) do
			local UserId = LocalPlayer.UserId

			local Success,Errormsg = pcall(function()
				CurrencyDataStore:SetAsync(UserId,LocalPlayer.leaderstats.Points.Value) -- Save the Points with UserId
			end)
			if not Success then
				warn(Errormsg)
			end

		end
	end
end)

Now To Give the Player A Point Every Second, You need to Use
Region 3 To Check the Player is in A Region and then Use a RemoteEvent To
Add it on the Server

So Add A Remote Event in ReplicatedStorage.
Call It AddPoint.

Add A Folder in Workspace Call The Folder RegionParts.

Now add Parts Which will be the Areas where the Player gets A Point every Second.
Make it Big and Set CanCollide to false and Transparency to 1.

Drag the Part inside the Folder.
Now This Should be A LocalScript in StarterGUI.

local GiveEvery = 1 -- 1 Second
local Found = false
local Player = game.Players.LocalPlayer

while wait(GiveEvery) do
	for _, Region in pairs(game.Workspace.RegionParts:GetChildren()) do
		Found = false
		local RegionArea = Region3.new(Region.Position - (Region.Size / 2), Region.Position + (Region.Size / 2))
		local Parts = game.Workspace:FindPartsInRegion3WithWhiteList(RegionArea,game.Players.LocalPlayer.Character:GetDescendants())

		for i,SinglePart in pairs(Parts) do
			if SinglePart:FindFirstAncestor(game.Players.LocalPlayer.Name) then
				Found = true
				break
			else
				Found = false
			end	
		end

		if Found == true then
			game.ReplicatedStorage.AddPoint:FireServer()
			break
		end		

	end
end

I tested this Out on A Baseplate and it Worked Perfectly Fine
For Me.

1 Like

Works perfectly, thanks for a detailed answer!

1 Like