0

# How to store a highest variable and add to leaderboard stat?

Edited 4 days ago

So, I've got a game where you fling yourself off a ramp. I've got some code to detect magnitude and put it into a leaderstat, but I want to find the highest magnitude before death, and then add that to the leaderstat. Sort of like a currency system. Below is the code for the magnitude stat that I currently have.

local Players = game:GetService("Players")

local point = workspace.Wedge

local function find_distance(speed: number, character: Model)
local player = Players:GetPlayerFromCharacter(character)
local root_part = character:WaitForChild("HumanoidRootPart")

magnitude.Value = (root_part.Position - point.Position).Magnitude
end

print(character.ClassName)
local humanoid = character:WaitForChild("Humanoid")

humanoid.Running:Connect(function(speed)
find_distance(speed, character)
end)
end

local magnitude = Instance.new("IntValue")
magnitude.Name = "Magnitude"
end


0
Record whatever the last known highest magnitude was, then do not update the IntValue unless the calculated magnitude exceeded the record. Update the record as you update the IntValue. Ziffixture 6863 — 4d
0
That does sound like it could work, but im just not sure how to do that. NightminerYT 0 — 4d
0
Actually, your IntValue already acts as the record. Ziffixture 6863 — 3d

0
A_Mp5 221
4 days ago

I don't have a perfect match but..

local magnitudes = {}
local best = nil
local score = 0

for i,v in pairs(magnitudes) do
if v.Value > score then
score = v.Value
best = v
end
end

print(best.."  Had the largest value, with a magnitude of "..score.." !"



You'd need to store all the magnitude in a table and then run this with "score" being the largest magnitude!

0
Storing magnitudes over time is super inefficient. You can also simplify that logic to: math.max(unpack(magnitudes)) Ziffixture 6863 — 4d
0
Ziffixture 6863
3 days ago
Edited 3 days ago
local magnitude_record = leaderstats.Magnitude
local magnitude = player:DistanceFromCharacter(ramp)

if magnitude_record.Value < magnitude then
magnitude_record.Value = magnitude
end

0
This just completely made the magnitude leaderstat vanish NightminerYT 0 — 1d