Hello I am currently making a game and wanted to know how to make a leaderboard that shows the amount of the studs someone has climbed.
You can take the ground level height of your map (let’s say the player’s character’s torso’s height while standing on the default Baseplate) and then get the user’s position in every frame, only minding the Y axis.
For the leaderboard, see this (if you don’t want to make the system itself from scratch): Coding the Leaderboard | Roblox Creator Documentation
how do I get the torsos height.
You get the Character model of the Player, and find LowerTorso, UpperTorso, Torso, or more universally, HumanoidRootPart (recommended).
Example:
local Players = game:GetService("Players")
local Torso = Players.YourPlayerHere.Character:FindFirstChild("Any of the torso names I mentioned")
if Torso then
local Position = Torso.Position
-- your other stuff...
end
Hi I kind of want the script to work on anyone and not just 1 player is there a way to do this?
You loop through the children (or players) of the Players service.
for _, Player in ipairs(Players:GetPlayers()) do
-- your code...
end
Ok so I can get the position of every character how do I get the y axis and put into a leaderboard.
You start by having a Script in the ServerScriptService, and for every player joining add a Folder named “leaderstats” (as is, no slight variation).
Then, create an IntValue, and round your Y value as well if you don’t want the decimals to be visible. Use math.ceil
or math.round
.
Finally, set that IntValue’s Value property to the rounded height of the player.
Look into the documentation article I sent earlier for more info.
how do i only get the position of the y axis.
If you’re handling Vector3’s, which is basically what Part.Position is, do this:
Position.Y
If you need to, you can also access X and Z.
it currently only works for 1 person in the game and stops working when someone else joins and it works for them how do I fix this.
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local studs = Instance.new("IntValue")
studs.Name = ("Studs")
studs.Parent = leaderstats
wait(10)
while true do
wait(1)
local Players = game:GetService("Players")
for _, Player in ipairs(Players:GetPlayers()) do
local Torso = Player.Character:FindFirstChild("HumanoidRootPart")
if Torso then
local Position = Torso.position.Y
local num = Position
print(math.ceil(num))
studs.Value = math.ceil(num)
end
end
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
Everytime somebody joins, you have a while loop wherein every 1 second, every single player’s current Y value is set to that player’s studs.
Try this code out instead:
local PS = game:GetService("Players")
local function round(number: number, digits: number)
local num = number * 10 ^ digits
return (if num >= 0 then math.floor(num + .5) else math.ceil(num - .5)) / (10 ^ digits)
end
local function onPlayerJoin(player: Player)
local leaderstats: Folder = Instance.new("Folder") do
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
end
local studs: IntValue = Instance.new("IntValue") do
studs.Name = ("Studs")
studs.Parent = leaderstats
end
local char = player.Character or player.CharacterAdded:Wait()
local HRP = char:WaitForChild("HumanoidRootPart")
task.wait(10) -- each player has 10 seconds before the studs are calculated
studs.Value = round(HRP.Position, 5)
end
PS.PlayerAdded:Connect(onPlayerJoin)
There’s an error.
Oh wait, sorry right.
Replace studs.Value = round(HRP.Position, 5)
with studs.Value = round(HRP.Position.Y, 5)
.
do you know how to make it so the studs keep updating on the leaderboard.
So every 10 seconds the leaderboard (for all players) updates?
like every 1 second cuz it shows how high they are on a ladder.
You can just do a while loop but outside of the function instead.
local PS = game:GetService("Players")
local function round(number: number, digits: number)
local num = number * 10 ^ digits
return (if num >= 0 then math.floor(num + .5) else math.ceil(num - .5)) / (10 ^ digits)
end
local function onPlayerJoin(player: Player)
local leaderstats: Folder = Instance.new("Folder") do
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
end
local studs: IntValue = Instance.new("IntValue") do
studs.Name = ("Studs")
studs.Parent = leaderstats
end
end
PS.PlayerAdded:Connect(onPlayerJoin)
while task.wait(1) do
for _,player: Player in ipairs(PS:GetPlayers()) do
local char = player.Character or player.CharacterAdded:Wait()
local HRP = char:WaitForChild("HumanoidRootPart")
local leaderstats = player:WaitForChild("leaderstats")
local studs = leaderstats:WaitForChild("Studs")
studs.Value = round(HRP.Position, 5)
end
end
Thank you this works perfectly.