Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

5 comments

0

you can use a for loop btw is this not the full code bc player is undefined

0

It was defined it was the only line i forgot to copy

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.

0

Send a friend request to my alt "thatdevlife" and message me there

0

i sent you a friend request

4 answers

✓ Accepted answer
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)

2 comments

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

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?

###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

3 comments

0

Would this replace my whole script that i have? I have it in a local script

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.

0

This was just an example, it's not exactly copy-paste friendly. You can concatenate it.

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!

1 comment

0

No, don't use a loop to check if a value changed. You should be using Changed events, and OP wants commas.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.