Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I make a leaderboard that calculates the distance of each player from a specific point?

Asked by 4 years ago

So what I'm trying to do is make a leaderboard that calculates the distance from the player to a certain point and displays the higher distance. So if I'm a user and I walk 100 paces from a point but then I walk 50 paces back towards the point, the leaderboard would still display 100. So far, I have this as my leaderboard script.


function onPlayerEntered(newPlayer) local stats = Instance.new("IntValue") stats.Name = "leaderstats" local c = Instance.new("IntValue") c.Name = "Distance"

spawn(function() while(true)do c.Value = print(onPlayerEntered():DistanceFromCharacter(Vector3.new(-208, 0.5, 1018))) end end)

c.Parent = stats stats.Parent = newPlayer

newPlayer.Changed:connect(function(property) end) end game.Players.PlayerAdded:connect(onPlayerEntered)

Can someone explain to me how to correctly make my leaderboard? Thank you.

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Here, I wrote up a simple script:

local position = Wherever you want the point to be

local plrs = {}

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

    local distanceValue = Instance.new("IntValue")
    distanceValue.Parent = leaderstats
    distanceValue.Name = "Distance (Studs)"

    table.insert(plrs,plr)
end)

game:GetService("RunService").Heartbeat:Connect(function()--Connects it to the Heartbeat, or after the physics render
    for i = 1,#plrs do--Loops through all the current players
        local plr = plrs[i]
        if plr ~= nil then
            local distanceValue = plr.leaderstats["Distance (Studs)"]
            local distance = plr:DistanceFromCharacter(position)--Distance from point
            if distance > distanceValue.Value then
                distanceValue.Value = distance
            end
        else
            table.remove(plrs,i)--Removes this from the plrs list
        end
    end
end)
0
Thank you so much! I really appreciate it. It works just swell. chitterness 4 — 4y
0
I hope you learned something :D firestarroblox123 440 — 4y
0
Haha. Sure did! Thanks again chitterness 4 — 4y
0
No problem! firestarroblox123 440 — 4y
Ad

Answer this question