Attempt to concatenate global 'q101' (a nil value) - lua

i like to load text form a external .lua file in to my game, here is a little test i setup to test the principal, i know if i give "q101" numbers like so "q101 = 123456" the code displays the numbers 123456 but i don't understand why it doesn't with letters.
Can some one please explain how i can do this is the right way, as this is clearly not the way of doing it
q101 = HELLO
Q1 = display.newText("".. q101, 160, 20, MYRIADPRO, 30)
Q1:setTextColor( 255, 255, 2552)
Q1.x = display.contentWidth/2
Q1.y = display.contentHeight/2
screenGroup:insert(Q1)

This line q101 = HELLO is setting the value of the q101 variable to the value of the HELLO variable. And as the global variable with that name has no value (as it has never been defined) you are assigning nil to your q101 variable.
Numbers cannot be variables and so do not work that way.
You want to assign the string "HELLO" to your variable: q101 = "HELLO".

Related

Unable to assign property Text. string expected, got Instance

I was making random text script (dont mind the sounds thing) and I got this error: Unable to assign property Text. string expected, got Instance.
while thing == true do
wait(math.random(3, 12))
local txts = game.ServerStorage.CallTexts:GetChildren()
local Choices = script:GetChildren()
local RandomIndex = math.random(1, #Choices)
local RandomSound = Choices[RandomIndex]
local RandomTextIndex = math.random(1, #txts)
local RandomText = Choices[RandomTextIndex]
RandomSound:Play()
game.Workspace["Talking Ben's Room"].furniture.desk.phone["De Text"].BilBord.toxt.Text = RandomText
wait(RandomSound.TimeLength)
end
Text should be a string. RandomText is an Instance. Like the error told you. What type are the Choices, e.g. the children of CallTexts? Do they have a getter/toString?
If they are StringValues you will need RandomText.Value
If they are TextLabels (your case), you will need RandomText.ContentText

How do I use more then one pattern for gmatch

Hello I am trying to get some data from a text file and put it into a table.
Im not sure how to add more then one pattern while also doing what I want, I know this pattern by its self %a+ finds letters and %b{} finds brackets, but I am not sure how to combine them together so that I find the letters as a key and the brackets as a value and have them be put into a table that I could use.
text file :
left = {{0,63},{16,63},{32,63},{48,63}}
right = {{0,21},{16,21},{32,21},{48,21}}
up = {{0,42},{16,42},{32,42},{48,42}}
down = {{0,0},{16,0},{32,0},{48,0}}
code:
local function get_animations(file_path)
local animation_table = {}
local file = io.open(file_path,"r")
local contents = file:read("*a")
for k, v in string.gmatch(contents, ("(%a+)=(%b{})")) do -- A gets words and %b{} finds brackets
animation_table[k] = v
print("key : " .. k.. " Value : ".. v)
end
file:close()
end
get_animations("Sprites/Player/MainPlayer.txt")
This is valid Lua code, why not simply execute it?
left = {{0,63},{16,63},{32,63},{48,63}}
right = {{0,21},{16,21},{32,21},{48,21}}
up = {{0,42},{16,42},{32,42},{48,42}}
down = {{0,0},{16,0},{32,0},{48,0}}
If you don't want the data in globals, use the string library to turn it into
return {
left = {{0,63},{16,63},{32,63},{48,63}},
right = {{0,21},{16,21},{32,21},{48,21}},
up = {{0,42},{16,42},{32,42},{48,42}},
down = {{0,0},{16,0},{32,0},{48,0}},
}
befor you execute it.
If you insist on parsing that file you can use a something like this for each line:
local line = "left = {{0,63},{16,63},{32,63},{48,63}}"
print(line:match("^%w+"))
for num1, num2 in a:gmatch("(%d+),(%d+)") do
print(num1, num2)
end
This should be enough to get you started. Of course you wouldn't print those values but put them into a table.

How to access user typed text inside a text box to make it as a variable? (Roblox)

So, I want to make a converter GUI, converting Bitcoin to Dollar. I use a textbox to get the user input and a text button to submit. But, when I type number for example 8 to the textbox while test the game and print what is inside the textbox, it printed nothing. Even though I have type 8 to the text box. Thanks for all the answers! Here is the code I use.
-- text variable below
local input = script.Parent
local val = input.Text
-- button variable below
local submit = input:FindFirstChild("btcSubmit")
-- player variable below
local gams = game.Players.LocalPlayer
local ld = gams:WaitForChild("leaderstats")
local bitcoin = ld:WaitForChild("Bitcoin").Value
local dollar = ld:WaitForChild("Dollar").Value
-- function
function btcEx()
val = tonumber(val)
if val > bitcoin then
val = tostring(val)
val = "Sorry, your Bitcoin isn't enough"
wait(4)
val = "Input the number of bitcoin you want to exchange here!"
else
dollar = val * 8000
val = tostring()
end
end
submit.MouseButton1Click:Connect(btcEx)
When you set a variable to a value rather than a reference, it remains that value until you change it.
object.Value = 5
local myValue = object.Value
object.Value = 10
print(myValue) -- Prints 5.
This happens because they are not linked and thus changes do not carry over, like these variables below:
local a = 5
local b = a
a = 10
print(b) -- Prints 5, because b was never changed (but a was).
What you want to do is define your button and your value-objects as references, and simply access .Text or .Value when you need to read the value.
local myButton = button
button.Text = "Howdy!"
print(myButton.Text) -- Prints "Howdy!"
myButton.Text = "Hey there" -- this is the same as button.Text
print(myButton.Text) -- Prints "Hey there"

Lua : Storing in a table through a variable name

I understand why this mwe doesn't work but I don't know how make it works.
I'd like to use the variable content as reference name (not the variable name).
salade = {}
name = "tomato"
salade.name = "red"
print (salade.tomato) -- nil, should be red
print (salade.name) -- red, should be nil
Just use the normal table indexing syntax, rather than the tbl.key syntactic sugar:
salade = {}
name = "tomato"
salade[name] = "red"
print (salade.tomato) -- red
print (salade.name) -- nil

How Lua tables work

I am starting to learn Lua from Programming in Lua (2nd edition)
I didn't understand the following in the book. Its very vaguely explained.
a.) w={x=0,y=0,label="console"}
b.) x={math.sin(0),math.sin(1),math.sin(2)}
c.) w[1]="another field"
d.) x.f=w
e.) print (w["x"])
f.) print (w[1])
g.) print x.f[1]
When I do print(w[1]) after a.), why doesn't it print x=0
What does c.) do?
What is the difference between e.) and print (w.x)?
What is the role of b.) and g.)?
You have to realize that this:
t = {3, 4, "eggplant"}
is the same as this:
t = {}
t[1] = 3
t[2] = 4
t[3] = "eggplant"
And that this:
t = {x = 0, y = 2}
is the same as this:
t = {}
t["x"] = 0
t["y"] = 2
Or this:
t = {}
t.x = 0
t.y = 2
In Lua, tables are not just lists, they are associative arrays.
When you print w[1], then what really matters is line c.) In fact, w[1] is not defined at all until line c.).
There is no difference between e.) and print (w.x).
b.) creates a new table named x which is separate from w.
d.) places a reference to w inside of x. (NOTE: It does not actually make a copy of w, just a reference. If you've ever worked with pointers, it's similar.)
g.) Can be broken up in two parts. First we get x.f which is just another way to refer to w because of line d.). Then we look up the first element of that table, which is "another field" because of line c.)
There's another way of creating keys in in-line table declarations.
x = {["1st key has spaces!"] = 1}
The advantage here is that you can have keys with spaces and any extended ASCII character.
In fact, a key can be literally anything, even an instanced object.
function Example()
--example function
end
x = {[Example] = "A function."}
Any variable or value or data can go into the square brackets to work as a key. The same goes with the value.
Practically, this can replace features like the in keyword in python, as you can index the table by values to check if they are there.
Getting a value at an undefined part of the table will not cause an error. It will just give you nil. The same goes for using undefined variables.
local w = {
--[1] = "another field"; -- will be set this value
--["1"] = nil; -- not save to this place, different with some other language
x = 0;
y = 0;
label = "console";
}
local x = {
math.sin(0);
math.sin(1);
math.sin(2);
}
w[1] = "another field" --
x.f = w
print (w["x"])
-- because x.f = w
-- x.f and w point one talbe address
-- so value of (x.f)[1] and w[1] and x.f[1] is equal
print (w[1])
print ((x.f)[1])
print (x.f[1])
-- print (x.f)[1] this not follows lua syntax
-- only a function's has one param and type of is a string
-- you can use print "xxxx"
-- so you print x.f[1] will occuur error
-- in table you can use any lua internal type 's value to be a key
-- just like
local t_key = {v=123}
local f_key = function () print("f123") end
local t = {}
t[t_key] = 1
t[f_key] = 2
-- then t' key actualy like use t_key/f_key 's handle
-- when you user t[{}] = 123,
-- value 123 related to this no name table {} 's handle

Resources