function changeTransparency(changePart, newTransparency)
changePart.transparency = newTransparency
end
changeTransparency(game.workspace.functionPart.Transparency, 1)
wait(2)
changeTransparency(game.Workspace.functionPart.Transparency, 0.25)
The function is trying to access functionPart.Transparency.transparency, and since functionPart.Transparency is a number, it gives the error.
Try changing the function to this, and passing in the part as an argument instead of the part's transparency.
function changeTransparency(changePart, newTransparency)
changePart.Transparency = newTransparency
end
changeTransparency(game.Workspace.functionPart, 1)
task.wait(2) -- more accurate than wait(2)
changeTransparency(game.Workspace.functionPart, 0.25)
Also make sure case is correct, Part.Transparency and Part.transparency are different.
The problem here, was that you typed transparency and not Transparency; to note that part properties are case sensitive.
In your function, replace this
changePart.transparency = newTransparency
with this
changePart.Transparency = newTransparency
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
I am in quite a pickle right now.
I have a switch that I need to be able to turn on and off every second but I don't really know how I would do that because it needs to be applied on the same bit/function
If currently have a bit that functions as an on/off for a function, the problem is that I need a way for that function to turn itself on and off every 1 second.
I have tried this so far but it is not working for me.
Could anyone try and solve this issue for me please?
{$lua}
if not syntaxcheck then
local On = 1
local Off = 0
local Switch = OnOffController
local function OnOff()
if Switch = Off then
Switch = On
if Switch = On then
Switch = Off
end
end
end
OnOff()
if(Second == nil) then
Second = createTimer(getMainForm())
Second.Interval = 1000
Second.OnTimer = function(timer)
OnOff()
end
end
Second.setEnabled(true)
end
While there could be more wrong, there's definitely an issue with the OnOff() function:
local function OnOff()
if Switch = Off then
Switch = On
if Switch = On then
Switch = Off
end
end
end
The two nested if statements are not going to do what you want. First off, you're using the assignment operator = rather than the equals operator ==. Even if that were correct though the logic has a problem. If Switch is On going into the first if the flow will bypass both. If Switch is Off going into the first if it'll first get changed to On and then immediately hit the second condition and get changed back to Off.
You probably want something more like:
local function OnOff()
if Switch == Off then
Switch = On
else
Switch = Off
end
end
You don't need the second comparison at all assuming Switch can only be true or false.
Other bits of advice I'd give would be to use a boolean type for the Switch variable as that makes it explicit that it can only have two values. You'd probably also want to change its name to something like SwitchedOn or the like to make it obvious what true and false would mean in its context. Likewise a name like ToggleSwitch may make it a bit more obvious what the function presently called OnOff is intended to do.
I want to add an ability in my game where if you hold t then the enemies are slowed down. love.keyboard.isDown won't let me put the enemies back to their original speed once the t key has been released. Is their another way I could do this?
Use love.keyreleased.
Note that unlike love.keyboard.isDown, it's a callback function. Use it to register the action when the key t is released.
Using love.keyboard.isDown will let you put their original speed back if you check when it's false, like so:
if love.keyboard.isDown('t') then
enemy_speed = 15
else
enemy_speed = 30 -- 't' key has been released
end
but there is an another way to do this. Use love.keypressed and love.keyreleased, like so:
function love.keypressed(key)
if key == 't' then
enemy_speed = 15
end
end
function love.keyreleased(key)
if key == 't' then
enemy_speed = 30 -- 't' key has been released
end
end
if I understand, love.keyboard.isDown("t") is for to the love.update() function, and it will just repeat the function no matter what. so in this case create a function like this in your main.lua file:
function love.keypressed(k)
if k == "t" then
// Code goes in here
end
end
like this, it should activate once the key is pressed.
Instead of using long lists of arguments in my function definitions, I prefer to pass a few fixed parameters and a table of 'additional params' like this:
function:doit( text, params )
end
This is nice as it allows me to add new named parameters later without breaking old calls.
The problem I am experiencing occurs when I try to force default values for some of the params:
function:doit( text, params )
local font = params.font or native.systemBold
local fontSize = params.fontSize or 24
local emboss = params.emboss or true
-- ...
end
The above code works fine in all cases, except where I have passed in 'false' for emboss:
doit( "Test text", { fontSize = 32, emboss = false } )
The above code will result in emboss being set to true when I really wanted false.
To be clear, what I want is for the first non-NIL value to be assigned to emboss, instead I'm getting a first non-false and non-NIL.
To combat this problem I wrote a small piece of code to find the first non-NIL value in a table and to return that:
function firstNotNil( ... )
for i = 1, #arg do
local theArg = arg[i]
if(theArg ~= nil) then return theArg end
end
return nil
end
Using this function I would re-write the emboss assignment as follows:
local emboss = firstNotNil(params.emboss, true)
Now, this certainly works, but it seems so inefficient and over the top. I am hoping there is a more compact way of doing this.
Please note: I found this ruby construct which looked promising and I am hoping lua has something like it:
[c,b,a].detect { |i| i > 0 } -- Assign first non-zero in order: c,b,a
Lua's relational operators evaluate to the value of one of the operands (i.e. the value is not coerced to boolean) so you can get the equivalent of C's ternary operator by saying a and b or c. In your case, you want to use a if it's not nil and b otherwise, so a == nil and b or a:
local emboss = (params.emboss == nil) and true or params.emboss
Not as pretty as before, but you'd only need to do it for boolean parameters.
[snip - Lua code]
Now, this certainly works, but it seems so inefficient and over the top.
Please note: I found this ruby construct which looked promising and I am hoping lua has
something like it:
[c,b,a].detect { |i| i > 0 } -- Assign first non-zero in order: c,b,a
Your Lua function is no more over-the-top or inefficient. The Ruby construct is more succinct, in terms of source text, but the semantics are not really different from firstNotNil(c,b,a). Both constructs end up creating a list object, initialize it with a set of values, running that through a function that searches the list linearly.
In Lua you could skip the creation of the list object by using vararg expression with select:
function firstNotNil(...)
for i = 1, select('#',...) do
local theArg = select(i,...)
if theArg ~= nil then return theArg end
end
return nil
end
I am hoping there is a more compact way of doing this.
About the only way to do that would be to shorten the function name. ;)
If you really want to do it in a single line, you'll need something like this for a default value of true:
local emboss = params.emboss or (params.emboss == nil)
It's not very readable, but it works. (params.emboss == nil) evaluates to true when params.emboss is not set (when you would need a default value), otherwise it's false. So when params.emboss is false, the statement is false, and when it's true, the statement is true (true or false = true).
For a default of false, what you tried originally would work:
local emboss = params.emboss or false
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");