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

My leader board is missing a value and will not track kills or anything. What is wrong?

Asked by 10 years ago

My leaderboard is missing one stat (StatPoints) and also will not count kills or deaths or anything. Here is the script.

--rbxsig%XYC6H9oKVLF//1itmbaGn5wI5a4YNtkf4beIsIVZsrT29WveOsrP4YfpYDED5Xa8A1oBA/5oDwtjy4/wlvl/DH0rigLCzfNVjszNya26GajrkOvCFifnNhOCVAOLBySGJ7z5gMoZR/XQjW5EO+K1zDWiQi5CLNwBOxOTMDLS8dU=%
--rbxassetid18966%
print("LinkedLeaderboard script version 5.00 loaded")

goldKey = "PlayerGold"
killsKey = "PlayerKills"
deathsKey = "PlayerDeaths"
XPKey = "PlayerXP"
statpointKey = "PlayerStatPoints"
lvlKey = "PlayerLevel"

game.Players.PlayerAdded:connect(function(player)
    if player:WaitForDataReady() then

        -- create leaderboard
        local ls = Instance.new("IntValue")
        ls.Name = "leaderstats"
        ls.Parent = player

        --create the score stat
        local gold = Instance.new("IntValue")
        gold.Name = "Gold"
        gold.Parent = ls
        gold.Value = player:LoadNumber(goldKey)

        --create the score stat
        local kills = Instance.new("IntValue")
        kills.Name = "Kills"
        kills.Parent = ls
        kills.Value = player:LoadNumber(killsKey)

        --create the score stat
        local deaths = Instance.new("IntValue")
        deaths.Name = "Deaths"
        deaths.Parent = ls
        deaths.Value = player:LoadNumber(deathsKey)

        --create the score stat
        local lvl = Instance.new("IntValue")
        lvl.Name = "Level"
        lvl.Parent = ls
        lvl.Value = player:LoadNumber(lvlKey)

        --create the score stat
        local XP = Instance.new("IntValue")
        XP.Name = "XP"
        XP.Parent = ls
        XP.Value = player:LoadNumber(xPKey)

        --create the score stat
        local statpoint = Instance.new("IntValue")
        statpoint.Name = "StatPoints"
        statpoint.Parent = ls
        statpoint.Value = player:LoadNumber(statpointKey)
    end
end)

game.Players.PlayerRemoving:connect(function(player)
    if player:FindFirstChild("leaderstats") then
        player:SaveNumber(goldKey, player.leaderstats.Gold.Value)
        player:SaveNumber(killsKey, player.leaderstats.Kills.Value)
        player:SaveNumber(deathsKey, player.leaderstats.Deaths.Value) 
        player:SaveNumber(statpointKey, player.leaderstats.StatPoints.Value)
        player:SaveNumber(XPKey, player.leaderstats.XP.Value)
        player:SaveNumber(lvlKey, player.leaderstats.Level.Value)  
    end
end)

function onxPChanged(player, XP, level) 
if XPKey.Value>=lvlKey.Value * 50 then --XP needed to level up.
XPKey.Value = XPKey.Value - XP.Value --XP lost on level up.
lvlKey.Value = lvlKey.Value + 1 --Levels added on level up.
StatPointsKey.Value = StatPointsKey.Value + 1
end 
end 

function onLevelUp(player, XPKey, lvlKey)
    if player.Character~=nil then
        for i = 1,10 do
            local fireworks = Instance.new("Part")
            fireworks.Shape = "Ball" --Shape of XP points.
            fireworks.formFactor = "Symmetric" 
            fireworks.Reflectance = 1 --How shiny?
            fireworks.Transparency = 0 --How transparent?
            fireworks.Size = Vector3.new(2,2,2)
            fireworks.BrickColor = BrickColor.Random()
            fireworks.CFrame = player.Character.Head.CFrame + Vector3.new(0,2,0)
            fireworks.Parent = game.Workspace
            game:GetService("Debris"):AddItem(fireworks, 2)
            fireworks.Velocity = Vector3.new(math.random(-30,30),math.random(-30,30),math.random(-30,30))
        end
    end
    local m = Instance.new("Hint")
    m.Parent = game.Workspace
    m.Text = "Level up!:"..player.Name.." is now level"..level.Value
    wait(2) --How long well hint show?
    m.Parent = nil
end




function onHumanoidDied(humanoid, player)
    local stats = player:findFirstChild("leaderstats")
    if stats ~= nil then
        local deathsKey = stats:findFirstChild("Deaths")
        deathsKey.Value = deathsKey.Value + 1

        -- do short dance to try and find the killer

        local killer = getKillerOfHumanoidIfStillInGame(humanoid)

        handleKillCount(humanoid, player)
    end
end

function onPlayerRespawn(property, player)
    -- need to connect to new humanoid

    if property == "Character" and player.Character ~= nil then
        local humanoid = player.Character.Humanoid
            local p = player
            local h = humanoid
            humanoid.Died:connect(function() onHumanoidDied(h, p) end )
    end
end

function getKillerOfHumanoidIfStillInGame(humanoid)
    -- returns the player object that killed this humanoid
    -- returns nil if the killer is no longer in the game

    -- check for kill tag on humanoid - may be more than one - todo: deal with this
    local tag = humanoid:findFirstChild("creator")

    -- find player with name on tag
    if tag ~= nil then

        local killer = tag.Value
        if killer.Parent ~= nil then -- killer still in game
            return killer
        end
    end

    return nil
end

function handleKillCount(humanoid, player)
    local killer = getKillerOfHumanoidIfStillInGame(humanoid)
    if killer ~= nil then
        local stats = killer:findFirstChild("leaderstats")
        if stats ~= nil then
            local killsKey = stats:findFirstChild("Kills")
            local goldKey = stats:findFirstChild("Gold")
            local XPKey = stats:findFirstChild("XP")
            if killer ~= player then
                killsKey.Value = killsKey.Value + 1
                goldKey.Value = goldKey.Value + 10
                XPKey.Value = XPKey.Value + 10

            else
                killsKey.Value = killsKey.Value - 1

            end
        end
    end
end

Also, in some games like Guest Defense, the money, xp, and health are in their own GUI. I was wonder if someone could teach me how to do that or point me in the right direction.

Thanks for the help but with your fix could you please tell me why it happened, what happened, how to prevent it in the future, and basically anything important that will help me become a better scripter because I REALLY REALLY want to master this. Thanks again mates :)!

Answer this question