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

How to display ammo plus letters in text label?

So I have a decal that sits next to a gun to display the ammo. It works fine when displaying just a number, but I want to have the word "ammo" in front of the number. I can get it to display "ammo" or the number, but not both at the same time. Here's my script:

if Ammo > 1 then
	Ammo = Ammo - 1
	Gun.DecalPart.SurfaceGui.TextLabel.Text = "ammo", Ammo
else
	tool:Destroy()
end

Thanks in advance

2 answers

✓ Accepted answer

String Concatenation


In lua, string concatenation is done using two periods, or..

For example:

"Hello ".."World" -> "Hello World"

To use this for your purpose, you would do:

if Ammo > 1 then
    Ammo = Ammo - 1
    Gun.DecalPart.SurfaceGui.TextLabel.Text = "ammo "..Ammo
else
    tool:Destroy()
end

Formatting


Another way you can do this, is with formatting , with the string.format function

An example of this would be :

local blank = "Ammo: %d"
if Ammo > 1 then
    Ammo = Ammo - 1
    Gun.DecalPart.SurfaceGui.TextLabel.Text = blank:format(Ammo)
else
    tool:Destroy()
end

the format function (in method "mode") takes in a string/number that is then substituted for a given pattern, for more information, you can look at this article here

Hopefully this helped!

2 comments

1

Guess i wasn't efficient/quick enough when answering. :SadFace:

0

Thanks for the quick answer, I was using a comma instead of the .. my bad. It worked perfectly after that little change :)

This is basic concatenation, which is really simple and easy to learn

First we take our strings that we want to show

local String1 = "Hello"
local String2 = "World"

then we combine them using two periods .. inside of parentheses

print(String1..String2)

this would output: HelloWorld

To add a space we can just do the following

print(String1.." "..String2)

In your case you would do it like this

("Ammo: "..Ammo)

1 comment

0

Thanks for the answer, I didn't think to use a .. instead of a comma. Easy fix and it worked perfectly after that :)

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