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
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
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!
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)