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

Leaderboard script failing to increase a value while yielding no errors?

Asked by 7 years ago
Edited 7 years ago

I've created a script that adds - or is supposed to add - a value to a players' gold count every five seconds. I've tried print debugging on certain parts, but I can't seem to find any errors. Where have I gone wrong?

Script, located in Workspace:

game.Players.PlayerAdded:connect(function(player)
    leaderstats = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"
    leaderstats.Value = 0

    gold = Instance.new("IntValue", leaderstats)
    gold.Name = "Gold"
    gold.Value = 0

    rank = Instance.new("StringValue", leaderstats)
    rank.Name = "Rank"
    rank.Value = player:GetRoleInGroup(3026186)

    leaderstats.Parent = player
    rank.Parent = leaderstats
end)

function getGold(P,rank)
        Gold = P.leaderstats.Gold.Value
        if rank == "[C] Peasant" then
            Gold = Gold + 2
        elseif rank == "[C] Merchant" then
            Gold = Gold + 15
        elseif rank == "[C] Shopkeeper" then
            Gold = Gold + 20
        elseif rank == "[C] Wealth Nobleman/Noblewoman" then
            Gold = Gold + 50
        elseif rank == "[M] Enlisted" then
            Gold = Gold.Value + 5
        elseif rank == "[M] Private" then
            Gold = Gold + 10
        elseif rank == "[M] Corporal" then
            Gold = Gold + 20
        elseif rank == "[M] First Sergeant" then
            Gold = Gold + 20
        elseif rank == "[M] Staff Officer" then
            Gold = Gold + 200
        elseif rank == "[M] 2nd Lieutenant" then
            Gold = Gold + 30
        elseif rank == "[M] 1st Lieutenant" then
            Gold = Gold + 30
        elseif rank == "[M] Captain" then
            Gold = Gold + 35
        elseif rank == "[M] Field Marshal" then
            Gold = Gold + 35
        elseif rank == "[M] High Constable" then
            Gold = Gold + 40
        elseif rank == "[N] Knight" then
            Gold = Gold + 45
        elseif rank == "[N] Baron/Baroness" then
            Gold = Gold + 45
        elseif rank == "[N] Count/Countess" then
            Gold = Gold + 60
        elseif rank == "[N] Duke/Duchess" then
            Gold = Gold + 70 
        elseif rank == "[N] King" then
            Gold = Gold + 100
        else
            print(rank)
    end
end

while true do
    wait(5)
    P = game.Players:GetChildren()
    for i, v in pairs(game.Players:GetChildren()) do
        rank = v.leaderstats.Rank.Value
     getGold(v,rank)
    end
end

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago
Edited 7 years ago

Your GetGold function doesn't modify P.leaderstats.Gold.Value -- it only modifies the Gold local variable.

In order to modify the IntValue, you have to somewhere explicitly set .Value somewhere:

-- always use local variables!
local Gold = P.leaderstats.Gold
-- i.e., NOT .Value up above -- we need to CHANGE .Value!

Gold.Value = Gold.Value + 10

Instead of using elseif, you should consider using a dictionary:

local Earning = {
    ["[C] Peasant"] = 2,
    ["[C] Merchant"] = 15,
    -- etc
    ["[N] King"] = 100,
}

.........

function getGold(P, rank)
    local Gold = P.leaderstats.Gold
    if Earning[rank] then
    Gold.Value = Gold.Value + Earning[rank]
    else
        print("unknown rank `" .. rank .. "`")
    end
end
0
Thank you, that definitely clears things up! Brinceous 85 — 7y
1
So was I creating a new local variable with the value of the initial Gold IntValue? And in your method you were stating the location of the variable, then declaring its new value? Brinceous 85 — 7y
1
That's exactly right. You made the variable Gold, set it to the old value, modified it, ... and then threw it away. The correct way says "this object holds a .Value, here's the new .Value for that object." BlueTaslem 18071 — 7y
0
Awesome, thank you so much for explaining that. I love learning things like this :) Brinceous 85 — 7y
View all comments (3 more)
0
After I've set up a dictionary, would it be appropriate to run an infinite loop (every 60 seconds) calling the getGold function as long as I have the P and rank variables defined before loop-calling the function? Brinceous 85 — 7y
0
Yea, that makes sense. BlueTaslem 18071 — 7y
0
Great! Brinceous 85 — 7y
Ad

Answer this question