this should be deleted but it says i dont have permission
OrderedDataStore’s is the typical approach to making a leaderboard. You will need to track how many studs a player has walked and update their entry in the ordered data store you create for the leaderboard. This could be done periodically while in game, or just upon exiting the game. For a introduction to DataStores, see this link: Data Stores
local Players = game:GetService("Players")
local DataStores = game:GetService("DataStoreService")
local DataStore = DataStores:GetOrderedDataStore("DataStore")
local ProtectedCall = pcall
local DoNotSavePlayers = {}
local function RetryLoad(Player)
local Success, Result = ProtectedCall(function()
return DataStore:GetAsync(Player.UserId)
end)
if Success then
Player.leaderstats.Studs.Value = Result or 0
table.remove(DoNotSavePlayers, table.find(DoNotSavePlayers, Player))
else
warn(Result)
end
end
local function CreateStats(Player)
local _Stats = Instance.new("Folder")
_Stats.Name = "leaderstats"
_Stats.Parent = Player
local Studs = Instance.new("IntValue")
Studs.Name = "Studs"
Studs.Parent = _Stats
return true
end
local function LoadData(Player)
local Success, Result = ProtectedCall(function()
return DataStore:GetAsync(Player.UserId)
end)
if Success then
Player.leaderstats.Studs.Value = Result or 0
else
table.insert(DoNotSavePlayers, Player)
warn(Result)
end
end
local function SaveData(Player)
local Success, Result = ProtectedCall(function()
return DataStore:SetAsync(Player.UserId, Player.leaderstats.Studs.Value)
end)
if Success then
print(Result)
else
warn(Result)
end
end
local function OnCharacterAdded(Character)
local Humanoid = Character:WaitForChild("Humanoid")
local HRP = Character:WaitForChild("HumanoidRootPart")
local OldPosition = HRP.Position
local function OnHumanoidMoved(Speed)
if Speed > 0 then
local Studs = (HRP.Position - OldPosition).Magnitude
local Player = Players:GetPlayerFromCharacter(Character)
if Player then
Player.leaderstats.Studs.Value += math.floor(Studs)
end
OldPosition = HRP.Position
end
end
Humanoid.Running:Connect(OnHumanoidMoved)
end
local function OnCharacterRemoving(Character)
local Player = Players:GetPlayerFromCharacter(Character)
if Player then
Player.leaderstats.Studs.Value = math.floor(Player.leaderstats.Studs.Value / 10)
end
end
local function OnPlayerAdded(Player)
Player.CharacterAdded:Connect(OnCharacterAdded)
Player.CharacterRemoving:Connect(OnCharacterRemoving)
local _Stats = CreateStats(Player)
repeat
task.wait()
until _Stats
LoadData(Player)
end
local function OnPlayerRemoving(Player)
if table.find(DoNotSavePlayers, Player) then
return
end
SaveData(Player)
end
local function OnServerEnd()
for _, Player in ipairs(Players:GetPlayers()) do
if table.find(DoNotSavePlayers, Player) then
return
end
SaveData(Player)
end
end
task.spawn(function()
while true do
if #DoNotSavePlayers > 0 then
for _, Player in ipairs(DoNotSavePlayers) do
task.wait(60 / #DoNotSavePlayers)
RetryLoad(Player)
end
end
end
end)
Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
game:BindToClose(OnServerEnd)
This section isn’t really for requesting scripts but I made one anyway.