Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

4 comments

0

Thank you so much! I really appreciate it. It works just swell.

0

I hope you learned something :D

0

Haha. Sure did! Thanks again

0

No problem!

This discussion is preserved as it was. New answers, comments, and votes are closed.