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

How do I add commas in a IntValue?

Probably going to get flagged for not constructive but hopefully, I can get an answer.

I want to figure out how to add commas, this script is in a local script through a TextLabel, I'm wanting it to add a comma connected to an IntValue. This is my script which will probably get flagged for it but I have no idea on how to go about doing this.

local plr = game:GetService("Players")
local player = plr.LocalPlayer

while wait(1) do
	script.Parent.Text = "$".. player.RebirthNeeds.Value
end

4 comments

0

so you mean like instead of 1000000, it would be 1,000,000?

0

and you should use a Changed event instead of a loop, and wait() should not be a condition

0

Can you post the script on how I would do that?

1 answer

✓ Accepted answer

Heres one way:

function addComas(str)
	return #str % 3 == 0 and str:reverse():gsub("(%d%d%d)", "%1,"):reverse():sub(2) or str:reverse():gsub("(%d%d%d)", "%1,"):reverse()
end

--example:
local IntValue = workspace.IntValue

print(addComas(tostring(IntValue.Value))
--the function will only work with a string. You can convert numbers to string using tostring(value)

--You can use the 'Changed' event instead of a loop:
player.RebirthNeeds.Changed:Connect(function()
	script.Parent.Text = "$" .. addComas(tostring(player.RebirthNeeds.Value))
end)

3 comments

0

Is this all i need to put in the local script, and it will make the RebirthNeeds Value with commas?

0

Also, how do i make it update, when the RebirthNeeds value goes up

0

you can use the Changed event: player.RebirthNeeds.Changed:Connect(function() [code] end) I'll edit my script to show it formatted.

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