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

Making this leaderboard work?

Asked by 9 years ago

Hey. I'm working on a high score system for my game. It's the final feature and I really need it done. So far, I have this script, and it works perfectly to add players to the leader board, however every now and then it fails to update the scores. There is never any output and I can't work out why it doesn't work. One thing I have found is that if they player has already set a score, and there is a higher ranking player, that's when it fails to update. Please help!

local scores = script:GetChildren()
local DataStore = game:GetService("DataStoreService"):GetDataStore("HighScore_V7")

for i = 1,#scores do
    local value = scores[i].Name
    local setter = value.."Setter"
    scores[i].Value = DataStore:GetAsync(value)
    if DataStore:GetAsync(setter) ~= nil then
        scores[i].Setter.Value = DataStore:GetAsync(setter)
    end
end

game.Players.PlayerAdded:connect(function(player)
    local highScore = player:FindFirstChild("highScore")
    if highScore then
        highScore.Changed:connect(function(val)
            updateScores(player.Name,highScore.Value)
        end)
    end
end)

function updateScores(player,val)
    for i = 1,#scores do
        if val >= scores[i].Value then
            if scores[i].Setter.Value == player then
                scores[i].Value = val
                for b = 1,#scores do
                    if scores[b].Setter.Value == player then
                        if b ~= i then
                            local tempVal = scores[b].Value
                            local tempSetter = scores[b].Setter.Value
                            scores[b].Value = 0
                            scores[b].Setter.Value = ""
                            moveUp(b,tempSetter,tempVal)
                        end
                    end
                end
                break
            else
                local good = true
                for b = 1,#scores do
                    if scores[b].Setter.Value == player then
                        good = false
                    end
                end
                if good == true then
                    moveBack(i,player,val)
                end
                break
            end
        end
    end
end

function moveBack(enterPos,setter,val)
    local prevVal = val
    local prevValTemp
    local prevSetter = setter
    local prevSetterTemp
    for i = enterPos,#scores do
        prevValTemp = scores[i].Value
        prevSetterTemp = scores[i].Setter.Value
        scores[i].Value  = prevVal
        scores[i].Setter.Value = prevSetter
        prevVal = prevValTemp
        prevSetter = prevSetterTemp
    end
end

function moveUp(enterPos,setter,val)
    local prevVal = val
    local prevValTemp
    local prevSetter = setter
    local prevSetterTemp
    for i = enterPos,#scores do
        local b = #scores - i
        if scores[b] then
            prevValTemp = scores[i].Value
            prevSetterTemp = scores[i].Setter.Value
            scores[i].Value  = prevVal
            scores[i].Setter.Value = prevSetter
            prevVal = prevValTemp
            prevSetter = prevSetterTemp
        end
    end
end

function mashUp(newScores)
    for i = 1,#newScores do
        updateScores(newScores[i].Setter.Value,newScores[i].Value)
    end
end

game.OnClose = function()
    local newScores = script.Parent.newScores:GetChildren()
    for i = 1,#scores do
        local val = scores[i].Name
        local setter = val.."Setter"
        newScores[i].Value = DataStore:GetAsync(val)
        newScores[i].Setter.Value = DataStore:GetAsync(setter)      
    end
    wait(1)
    for i = 1,#scores do
        local value = scores[i].Name
        local setter = value.."Setter"
        DataStore:SetAsync(value,scores[i].Value)
        DataStore:SetAsync(setter,scores[i].Setter.Value)
    end
end

Sorry it's a long script, but it's all necessary... I think...

0
Anything in the output? Grenaderade 525 — 9y
0
None Protoduction 216 — 9y

Answer this question