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

Shortening Currencies Value Doesn't Work For Second Currency?

Asked by 4 years ago

I'm trying to shorten the values for the currency,

It works for Coins but no the Chi?

Leaderstats

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder", plr)
    leaderstats.Name = "leaderstats"

    local MainCurrency = Instance.new("Folder", plr)
    MainCurrency.Name = "MainCurrency"

    local Coins = Instance.new("IntValue", MainCurrency)
    Coins.Name = "Coins"
    Coins.Value = 0

    local Chi = Instance.new("IntValue", MainCurrency)
    Chi.Name = "Chi"
    Chi.Value = 0

    local CoinsDis = Instance.new("StringValue", leaderstats)
    CoinsDis.Name = "Coins"
    CoinsDis.Value = 0

    local Gems = Instance.new("IntValue", leaderstats)
    Gems.Name = "Gems"
    Gems.Value = 0

    local ChiDisp = Instance.new("StringValue", leaderstats)
    ChiDisp.Name = "Chi"
    ChiDisp.Value = 0

    local Strength = Instance.new("IntValue", leaderstats)
    Strength.Name = "Strength"
    Strength.Value = 0

    local MaxStrength = Instance.new("IntValue", leaderstats)
    MaxStrength.Name = "MaxStrength"
    MaxStrength.Value = 10

    while wait() do --COIN COVERT
        local Coin = tostring(Coins.Value)
        if #Coin >= 10 then -- Bill
            CoinsDis.Value = Coin:sub(0,(#Coin-9)).."."..(Coin:sub(#Coin-7,(#Coin-7))).."B+"
        elseif #Coin >= 7 then -- Mill
            CoinsDis.Value = Coin:sub(0,(#Coin-6)).."."..(Coin:sub(#Coin-5,(#Coin-5))).."M+"
        elseif #Coin >= 4 then -- Thos
            CoinsDis.Value = Coin:sub(0,(#Coin-3)).."."..(Coin:sub(#Coin-2,(#Coin-2))).."K+"
        else
            CoinsDis.Value = tostring(Coins.Value)
        end         
    end

    while wait() do --CHI COVERT
        local Chi = tostring(Chi.Value)
        if #Chi >= 10 then -- Bill
            ChiDisp.Value = Chi:sub(0,(#Chi-9)).."."..(Chi:sub(#Chi-7,(#Chi-7))).."B+"
        elseif #Chi >= 7 then -- Mill
            ChiDisp.Value = Chi:sub(0,(#Chi-6)).."."..(Chi:sub(#Chi-5,(#Chi-5))).."M+"
        elseif #Chi >= 4 then -- Thos
            ChiDisp.Value = Chi:sub(0,(#Chi-3)).."."..(Chi:sub(#Chi-2,(#Chi-2))).."K+"
        else
            ChiDisp.Value = tostring(Chi.Value)
        end         
    end


end)
0
Your using 2 while loops, so the first while loop will only run, try combining the loops. killerbrenden 1537 — 4y
0
Any errors? moo1210 587 — 4y
0
No errors Jomeliter 55 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

Your using 2 while loops, so the game reads it from top to bottom, and it will loop over the first While Loop until it breaks. If it doesn't break, it'll just loop over that current code for the rest of the game being live.

To fix this, I used a ModuleScript with functions and just looped those. I may have done it a bit more complicated and memory consuming than it had to be, but I just found this way to be easier for me.

In the ModuleScript that I named NumericalShortener I changed it around.

local numericalShortener = {}

function numericalShortener.ShortenChi(player)
    local chi = tostring(player:WaitForChild("MainCurrency"):FindFirstChild("Chi").Value)
    local chiDis = player:WaitForChild("leaderstats"):FindFirstChild("Chi")

    if #chi >= 10 then
        chiDis.Value = chi:sub(0,(#chi - 9)).."."..(chi:sub(#chi - 7,(#chi - 7))).."B+"
    elseif #chi >= 7 then
        chiDis.Value = chi:sub(0,(#chi - 6)).."."..(chi:sub(#chi - 5,(#chi - 5))).."M+"
    elseif #chi >= 4 then
        chiDis.Value = chi:sub(0,(#chi - 3)).."."..(chi:sub(#chi - 2,(#chi - 2))).."K+"
    else
        chiDis.Value = chi
    end
end

function numericalShortener.ShoternCoins(player)
    local coins = tostring(player:WaitForChild("MainCurrency"):FindFirstChild("Coins").Value)
    local coinsDis = player:WaitForChild("leaderstats"):FindFirstChild("Coins")

    if #coins >= 10 then
        coinsDis.Value = coins:sub(0,(#coins - 9)).."."..(coins:sub(#coins - 7,(#coins - 7))).."B+"
    elseif #coins >= 7 then
        coinsDis.Value = coins:sub(0,(#coins - 6)).."."..(coins:sub(#coins - 5,(#coins - 5))).."M+"
    elseif #coins >= 4 then
        coinsDis.Value = coins:sub(0,(#coins - 3)).."."..(coins:sub(#coins - 2,(#coins - 2))).."K+"
    else
        coinsDis.Value = coins
    end
end

return numericalShortener

In a ServerScript that I named CurrencyHandler I added a loop for the functions of the ModuleScript while requiring it.

local moduleShortener = require(script.NumericalShortener)

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder", plr)
    leaderstats.Name = "leaderstats"

    local MainCurrency = Instance.new("Folder", plr)
    MainCurrency.Name = "MainCurrency"

    local Coins = Instance.new("IntValue", MainCurrency)
    Coins.Name = "Coins"
    Coins.Value = 0

    local Chi = Instance.new("IntValue", MainCurrency)
    Chi.Name = "Chi"
    Chi.Value = 0

    local CoinsDis = Instance.new("StringValue", leaderstats)
    CoinsDis.Name = "Coins"
    CoinsDis.Value = 0

    local Gems = Instance.new("IntValue", leaderstats)
    Gems.Name = "Gems"
    Gems.Value = 0

    local ChiDisp = Instance.new("StringValue", leaderstats)
    ChiDisp.Name = "Chi"
    ChiDisp.Value = 0

    local Strength = Instance.new("IntValue", leaderstats)
    Strength.Name = "Strength"
    Strength.Value = 0

    local MaxStrength = Instance.new("IntValue", leaderstats)
    MaxStrength.Name = "MaxStrength"
    MaxStrength.Value = 10

    while wait() do
        moduleShortener.ShortenChi(plr)
        moduleShortener.ShoternCoins(plr)
    end
end)

This is how I have it set up in ServerScriptService, here.

Hope this helped! I'm pretty sure this should work as I have tested it myself.

If this is what you're looking for and it helped, let me know by selecting this as the answer!

0
I put a ServerScript inside of ServerScriptService, and then I put a ModuleScript inside of that ServerScript. killerbrenden 1537 — 4y
0
Thx, it works! Jomeliter 55 — 4y
0
No problem! killerbrenden 1537 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Tried This But The Chi's Value Won't Change Now?

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder", plr)
    leaderstats.Name = "leaderstats"

    local MainCurrency = Instance.new("Folder", plr)
    MainCurrency.Name = "MainCurrency"

    local Coins = Instance.new("IntValue", MainCurrency)
    Coins.Name = "Coins"
    Coins.Value = 0

    local Chi = Instance.new("IntValue", MainCurrency)
    Chi.Name = "Chi"
    Chi.Value = 0

    local CoinsDis = Instance.new("StringValue", leaderstats)
    CoinsDis.Name = "Coins"
    CoinsDis.Value = 10

    local Gems = Instance.new("IntValue", leaderstats)
    Gems.Name = "Gems"
    Gems.Value = 0

    local ChiDisp = Instance.new("StringValue", leaderstats)
    ChiDisp.Name = "Chi"
    ChiDisp.Value = 10

    local Strength = Instance.new("IntValue", leaderstats)
    Strength.Name = "Strength"
    Strength.Value = 0

    local MaxStrength = Instance.new("IntValue", leaderstats)
    MaxStrength.Name = "MaxStrength"
    MaxStrength.Value = 10

    while wait() do --COIN COVERT
        local Coin = tostring(Coins.Value)
        if #Coin >= 10 then -- Bill
            CoinsDis.Value = Coin:sub(0,(#Coin-9)).."."..(Coin:sub(#Coin-7,(#Coin-7))).."B+"
        elseif #Coin >= 7 then -- Mill
            CoinsDis.Value = Coin:sub(0,(#Coin-6)).."."..(Coin:sub(#Coin-5,(#Coin-5))).."M+"
        elseif #Coin >= 4 then -- Thos
            CoinsDis.Value = Coin:sub(0,(#Coin-3)).."."..(Coin:sub(#Coin-2,(#Coin-2))).."K+"
        else
            CoinsDis.Value = tostring(Coins.Value)
        end 

        local Chi = tostring(Chi.Value)
        if #Chi >= 10 then -- Bill
            ChiDisp.Value = Chi:sub(0,(#Chi-9)).."."..(Chi:sub(#Chi-7,(#Chi-7))).."B+"
        elseif #Chi >= 7 then -- Mill
            ChiDisp.Value = Chi:sub(0,(#Chi-6)).."."..(Chi:sub(#Chi-5,(#Chi-5))).."M+"
        elseif #Chi >= 4 then -- Thos
            ChiDisp.Value = Chi:sub(0,(#Chi-3)).."."..(Chi:sub(#Chi-2,(#Chi-2))).."K+"
        else
            ChiDisp.Value = tostring(Chi.Value)
        end         
    end

end)

Answer this question