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

How to display ammo plus letters in text label?

Asked by 5 years ago

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

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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!

1
Guess i wasn't efficient/quick enough when answering. :SadFace: Mr_Pure 129 — 5y
0
Thanks for the quick answer, I was using a comma instead of the .. my bad. It worked perfectly after that little change :) tygerupercut3 68 — 5y
Ad
Log in to vote
1
Answered by
Mr_Pure 129
5 years ago

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)
0
Thanks for the answer, I didn't think to use a .. instead of a comma. Easy fix and it worked perfectly after that :) tygerupercut3 68 — 5y

Answer this question