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

How could I go about making this code more simple while still doing the same thing?

Asked by
Vxpper 101
6 years ago

This is my code:

local am = player:WaitForChild("CashRecieved").Value

script.Parent.Text = "$"..player:WaitForChild("CashRecieved").Value

player:WaitForChild("CashRecieved").Changed:Connect(function()
    script.Parent.Text = "$"..player:WaitForChild("CashRecieved").Value


if player:WaitForChild("CashRecieved").Value == 1000 then
    script.Parent.Text = "1,000"
        else if player:WaitForChild("CashRecieved").Value == 1100 then
        script.Parent.Text = "1,100"
        elseif player:WaitForChild("CashRecieved").Value == 1200 then
        script.Parent.Text = "1,200"
        elseif player:WaitForChild("CashRecieved").Value == 1300 then
        script.Parent.Text = "1,300"
        else if player:WaitForChild("CashRecieved").Value == 1400 then
        script.Parent.Text = "1,400"
        elseif player:WaitForChild("CashRecieved").Value == 1500 then
        script.Parent.Text = "1,500"
        elseif player:WaitForChild("CashRecieved").Value == 1600 then
        script.Parent.Text = "1,600"
        else if player:WaitForChild("CashRecieved").Value == 1700 then
        script.Parent.Text = "1,700"
        elseif player:WaitForChild("CashRecieved").Value == 1800 then
        script.Parent.Text = "1,800"
        elseif player:WaitForChild("CashRecieved").Value == 1900 then
        script.Parent.Text = "1,900"
        else if player:WaitForChild("CashRecieved").Value == 2000 then
        script.Parent.Text = "2,000"
        elseif player:WaitForChild("CashRecieved").Value == 2100 then
        script.Parent.Text = "2,100"
        elseif player:WaitForChild("CashRecieved").Value == 2200 then
        script.Parent.Text = "2,200"
        else if player:WaitForChild("CashRecieved").Value == 2300 then
        script.Parent.Text = "2,300"
        elseif player:WaitForChild("CashRecieved").Value == 2400 then
        script.Parent.Text = "2,400"
        elseif player:WaitForChild("CashRecieved").Value == 2500 then
        script.Parent.Text = "2,500"
        else if player:WaitForChild("CashRecieved").Value == 2600 then
        script.Parent.Text = "2,600"
        elseif player:WaitForChild("CashRecieved").Value == 2700 then
        script.Parent.Text = "2,700"
        elseif player:WaitForChild("CashRecieved").Value == 2800 then
        script.Parent.Text = "2,800"
        elseif player:WaitForChild("CashRecieved").Value == 2900 then
        script.Parent.Text = "2,900"
        elseif player:WaitForChild("CashRecieved").Value == 3000 then
        script.Parent.Text = "3,000"                    
                            end
                        end     
                    end
                end
            end
        end
    end
end)
0
you can use a for loop btw is this not the full code bc player is undefined radusavin366 617 — 6y
0
It was defined it was the only line i forgot to copy Vxpper 101 — 6y
0
I know this is not what the comments are for, but how can i send you a message via roblox. You account's settings dosent let me. gueli844 -1 — 5y
0
Send a friend request to my alt "thatdevlife" and message me there Vxpper 101 — 5y
0
i sent you a friend request gueli844 -1 — 5y

4 answers

Log in to vote
0
Answered by 6 years ago
local am = player:WaitForChild("CashRecieved").Value

script.Parent.Text = "$"..player:WaitForChild("CashRecieved").Value

local CashRecievedValue = player:WaitForChild("CashRecieved")

CashRecievedValue.Changed:connect(function(difference)
    local value = tostring(CashRecievedValue.Value) -- converts the CashRecievedValue to string

    if string.sub(value, 2, 4) == "000" then -- checks if last three digits of the value is '000' ex: 3000
        script.Parent.Text = value

    elseif string.sub(value, 3, 4) == "00" then -- checks if last two digits of the value is '00' ex: 1100
        script.Parent.Text = value
    end
end)
0
Says there is an error, Error attempt to index local 'CashCollectedValue' (a number value) that's what it says when i test it ingame and testing in studio Vxpper 101 — 6y
0
I tested the script in studio and it works for me. Maybe it's something else in your script that you did not post here? awesomeipod 607 — 6y
Ad
Log in to vote
1
Answered by 6 years ago

Check out this function from lua-users.org

-- from sam_lie
-- Compatible with Lua 5.0 and 5.1.
-- Disclaimer : use at own risk especially for hedge fund reports :-)

---============================================================
-- add comma to separate thousands
-- 
function comma_value(amount)
  local formatted = amount
  while true do  
    formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
    if (k==0) then
      break
    end
    wait() -- I added this because in regular Lua there is no wait function
  end
  return formatted
end
0
Would this replace my whole script that i have? I have it in a local script Vxpper 101 — 6y
0
Or how would i format this to work, i tried just using that and adding lines 1-6 from the script on my question and it still doesn't add commas. Vxpper 101 — 6y
0
This was just an example, it's not exactly copy-paste friendly. You can concatenate it. User#19524 175 — 6y
Log in to vote
0
Answered by
iSvenDerp 233 Moderation Voter
6 years ago

Hi, instead of using so many else if's, just use a while true do loop to display the value.

-- you said you already had player defined and forgot to paste it, so I won't add it here, but dont forget it! 

while true do

    script.Parent.Text = player:WaitForChild("CashRecieved").Value  
    wait(0.1)
end

Hope this helped! Let me knwo if you have questions or if it didn't work. It should work though!

0
No, don't use a loop to check if a value changed. You should be using Changed events, and OP wants commas. User#19524 175 — 6y
Log in to vote
0
Answered by
hmurban 16
6 years ago

i don't know if this would work cause i can't test it right now but here it is

toggle = false
player:WaitForChild("CashRecieved").Changed:Connect(function()
toggle = true
if toggle == true then
script.Parent.Text = player:WaitForChild("CashRecieved").Value
toggle = false
end
end)

Answer this question