Ad
Log in to vote
0

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

Asked by 6 days ago
Edited 5 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")

    local leaderstats = player:WaitForChild("leaderstats")
    local magnitude = leaderstats.Magnitude

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

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

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

local function player_added(player: Player)
    player.CharacterAdded:Connect(character_added)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player 

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

Players.PlayerAdded:Connect(player_added)
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 — 5d
0
That does sound like it could work, but im just not sure how to do that. NightminerYT 0 — 5d
0
Actually, your IntValue already acts as the record. Ziffixture 6863 — 4d

2 answers

Log in to vote
0
Answered by
A_Mp5 221 Moderation Voter
5 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 — 5d
Ad
Log in to vote
0
Answered by
Ziffixture 6863 Moderation Voter Community Moderator
4 days ago
Edited 4 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 — 2d

Answer this question