So, I’m having an issue while trying to split strings into tables (players into teams). When there are two players only, it works like a charm, but when there are 3+ players, this pops up: “Init Error : transformice.lua:7: bad argument: table expected, got nil”. Everything seems to be ok, I really don’t know what’s wrong. Can you guys please help me? Thanks! Here is my code:
ps = {"Player1","Player2","Player3","Player4"}
local teams={{},{},{}}
--[[for name,player in pairs(tfm.get.room.playerList) do
table.insert(ps,name)
end]]
table.sort(ps,function() return math.random()>0.5 end)
for i,player in ipairs(ps) do
table.insert(teams[i%#teams],player)
end
Lua arrays start at index 1, not 0. In the case of when you have 3 players this line:
table.insert(teams[i%#teams],player)
Would evaluate to:
table.insert(teams[3%3],player)
Which then would end up being:
table.insert(teams[0],player)
And teams[0] would be nil. You should be able to write it as:
table.insert(teams[i%#teams+1],player)
instead.
Related
function teleportTo(placeCFrame)
local plyr = game.Players.LocalPlayer;
if plyr.Character then
return plyr.Character.HumanoidRootPart.CFrame = placeCFrame;
end
end
teleportTo(game:GetService("Workspace").game:GetService("Workspace").Zeppelin.FuelTank1.Tank.CFrame)
my code is here, idk much about coding thanks for helping
and if you told me how to make the player teleport to a moving object it would be so super
The error is telling you what is going on.
When the code was being interpreted line by line, it expected the next symbol to be end, but instead it got =.
That means that something about how you're using the equals sign is incorrect. So when we look at the line :
return plyr.Character.HumanoidRootPart.CFrame = placeCFrame
You cannot assign a value on the same line as a return command. return is used to pipe a value out of a function so it can be used where the function was called.
But I'm pretty sure that isn't what you intended, you just want to set a player's position. So to fix your issue, remove the return.
if plyr.Character then
plyr.Character.HumanoidRootPart.CFrame = placeCFrame
end
The reason you are getting an error is because = is usually defined as setting a value, and no code can be executed after a return or it will error
If you wanted to, you could add return after all the code is done executing
Alright I'll get straight to the point. I am currently working on a script for the RedM and I have run into a bit of trouble. I have a table I need to call but I need to be able to call it with a string and despite trying to figure this out for days I have had no luck. Here's a quick example of what I am trying to do.
Table = {
"Value 1",
"Value 2",
"Value 3"
}
local _Table = "Table"
for key, value in pairs(_Table) do
print(value)
end
What I am wanting that to do is print out everything in that table however when you try to use a string you get the following error
$lua main.lua
lua: main.lua:9: bad argument #1 to 'for iterator' (table expected, got string)
stack traceback:
[C]: in function 'next'
main.lua:9: in main chunk
[C]: in ?
And obviously the script works when you don't use a string however that won't work for me. To be totally honest I'm not even sure what I'm trying to do is even possible, but if it is and anyone has a solution it would be VERY appreciated lol.
If I understand correctly, you want to get a table by its variable name. This is only possible at all if the variable is global, which it shouldn't (Always make your variables local, children) and even then it wouldn't be the most elegant solution.
A better alternative might be: put your tables in another table:
local Tables = {
Table_1 = { 1, 2, 3, }
Table_2 = { 2, 3, 4, }
}
print(Tables["Table_1"])
This keeps the global scope clean and allows you to pass the whole set of tables around as a single value.
you cannot call a table unless its metatable implements __call. I assume you're confusing some basic terminology here.
your code doesn't make sense
-- this line assigns the string "Table" to _Table
local _Table = "Table"
-- now you put into pairs which may only take a table as input
for key, value in pairs(_Table) do
print(value)
end
If Table is a global variable as in your example you can access it through the global environment table _G.
for key, value in pairs(_G.Table) do print(value) end
or
local _Table = "Table"
for key, value in pairs(_G[_Table]) do print(value) end
The following code works as expected:
local t = {}
print(t[1])
The above will print nil.
How come the following code results in an error?
print({}[1])
What is the logic behind this?
You can:
print(({"a", "b", "c"})[2]) -- "b"
Ok so I've been searching for a while and didn't get the answer. I imagine someone has this same problem but I was not able to solve this problem. I'm new to Lua, having some experience with Python but not being a programmer :S.
So I'm doing a metatable to handle complex numbers, following the tutorials here: http://www.dcc.ufrj.br/~fabiom/lua/
so I implement creation, addition, printing and equal comparison:
local mt={}
local function new(r,i)
return setmetatable({real = r or 0, im = i or 0},mt)
end
local function is_complex (v)
return getmetatable(v)==mt
end
local function add (c1,c2)
if not is_complex(c1) then
return new(c1+c2.real,c2.im)
end
if not is_complex(c2) then
return new(c1.real+c2,c1.im)
end
return new(c1.real + c2.real,c1.im + c2.im)
end
local function eq(c1,c2)
return (c1.real==c2.real) and (c1.im==c2.im)
end
local function modulus(c)
return (math.sqrt(c.real^2 + c.im^2))
end
local function tos(c)
return tostring(c.real).."+"..tostring(c.im).."i"
end
mt.new=new
mt.__add=add
mt.__tostring=tos
mt.__eq=eq
mt.__len=modulus
return mt
Then I make a small tests:
complex2=require "complex2"
print (complex2)
c1=complex2.new(3,2)
c2=complex2.new(3,4)
print (c1)
print (c2)
print(#{1,2})
print(#c2)
print(complex2.__len(c2))
print(#complex2.new(4,3))
and I get:
table: 0000000003EADBC0
3+2i
3+4i
2
0
5
0
So, What am I dong wrong? is something with calling the #, when i try to debug in the other cases the program goes to the module into the function but the # operand gets like ignored. Modulus function is working and can be called in the module... I'm sorry for such a long and I'm sure obvious question but I tried everything I could already. Thank you
May be the problem is about Lua version.
http://www.lua.org/manual/5.2/manual.html#2.4
"len": the # operation
works since Lua 5.2
and it works only with tables
So, Thank you, and you are right. I thought I had selected 5.2 but in the interpreter it was running 5.1 :S. Now i did:
complex2=require "complex2"
print (complex2)
c1=complex2.new(3,2)
c2=complex2.new(3,4)
print (c1)
print (c2)
print (c1+c2)
print(#{1,2})
print(#c2)
print(complex2.__len(c2))
print(complex2.__len(complex2.new(3,3)))
print(#complex2.new(4,3))
print(getmetatable(c2))
And i got:
table: 000000000047E1C0
3+2i
3+4i
6+6i
2
5
5
4.2426406871193
5
table: 000000000047E1C0
Everything under control ^^, at least the code was working as supposed xD
I'm trying to get a handle on how OOP is done in Lua, and I thought I had a simple way to do it but it isn't working and I'm just not seeing the reason. Here's what I'm trying:
Person = { };
function Person:newPerson(inName)
print(inName);
p = { };
p.myName = inName;
function p:sayHello()
print ("Hello, my name is " .. self.myName);
end
return p;
end
Frank = Person.newPerson("Frank");
Frank:sayHello();
FYI, I'm working with the Corona SDK, although I am assuming that doesn't make a difference (except that's where print() comes from I believe). In any case, the part that's killing me is that inName is nil as reported by print(inName)... therefore, myName is obviously set to nil so calls to sayHello() fail (although they work fine if I hardcode a value for myName, which leads me to think the basic structure I'm trying is sound, but I've got to be missing something simple). It looks, as far as I can tell, like the value of inName is not being set when newPerson() is called, but I can't for the life of me figure out why; I don't see why it's not just like any other function call.
Any help would be appreciated. Thanks!
Remember that this:
function Person:newPerson(inName)
Is equivalent to this:
function Person.newPerson(self, inName)
Therefore, when you do this:
Person.newPerson("Frank");
You are passing one parameter to a function that expects two. You probably don't want newPerson to be created with :.
Try
Frank = Person:newPerson("Frank");