This error makes no sense.

Hello, I am trying to do something like this:


AddStickTool("Move Entity Position", {
	Description = "Moves the position of the target entity",
	Icon = "icon16/bug.png",
	CanTarget = anything,
	OnRun = function(Player, Trace)
		if (Trace.Entity == Entity(0)) then // Removing the world crashes the game. No need to do that.
			return
		end
		if local varCopied = true then -- Place of error, line 249.
			Trace.Entity:SetPos(str)
			local varCopied = false
		else
			local pos = Trace.Entity:GetPos()
			local str = (" .. pos.x .. ", " .. pos.y .. ", " .. pos.z .. ")
			local varCopied = true
		return 
	end
})

But I am getting this error:



[ERROR] addons/admin_stick/lua/admin_stick_base_funcs.lua:249: unexpected symbol near 'local'
  1. unknown - addons/admin_stick/lua/admin_stick_base_funcs.lua:0


Edit: I want to be able swing the stick at an entity, then swing it again to move the entity to that place.

which is line 249

[editline]19th September 2015[/editline]

Oh I’m dumb,



		if local varCopied = true then -- Place of error, line 249.


That’s not how lua works, my brain is dying so I’m struggling to find words to explain how but read the basics of glua/programming and change it to just if varCopied - local is used for initially defining stuff, not when it is called

wha?

oh, now i see the edit lol

The local keyword is used to declare a local, not make a reference to it. What you’re supposed to do is define a local once and then don’t use the word local to refer to it again, just refer to it with the name you gave it. In addition to this, you’re using = to check for equality.

= is for assigning a variable a value.
== is to check for equality.

I’m guessing the CanTarget part is also supposed to be a string, and your str var is wrong too. The whole thing is a mess. I encourage you to read up a little more about the syntax.

Now I have


if varCopied = true then

But its giving me this



[ERROR] addons/admin_stick/lua/admin_stick_base_funcs.lua:248: 'then' expected near '='
  1. unknown - addons/admin_stick/lua/admin_stick_base_funcs.lua:0


[editline]19th September 2015[/editline]

Oh I see. No, the cantarget is correct. The administration stick addon by top hatted cat uses those fields to create new tools on the stick. Yeah I know it’s a mess, I just wanted to resolve that error.

Yeah, what hat man said



local variable = 0

if variable == 0 then


Less of a mess now, but does anyone know how to actually make this work?


AddStickTool("Move Entity Position", {
	Description = "Moves the position of the target entity",
	Icon = "icon16/bug.png",
	CanTarget = anything,
	OnRun = function(Player, Trace)
		if (Trace.Entity == Entity(0)) then // Removing the world crashes the game. No need to do that.
			return
		end
		if varCopied == true then
			local pos = Trace.Hitpos
			entity:SetPos(pos.x,pos.y,pos.z)
			local varCopied = false
		else
			local entity = Trace.Entity
			local varCopied = true
			return 
		end
	end
})

[editline]19th September 2015[/editline]

Removed the locals from varCopied = true and = false, still nothing, no errors, it just doesn’t do anything.

-snip-

Doesn’t seem to do much of anything either.

It isn’t really clear what you are trying to achieve.
Your error has been fixed. Fixing your code is your responsibility.

However, if you tell us what exactly this is supposed to do, someone might be able to help.

Huh?
Pretty obvious that it’s my responsibility, but I’m still asking for help because I can’t figure it out. Is that not allowed…?

Also, right in the main post:

Edit: I want to be able swing the stick at an entity, then swing it again to move the entity to that place.

You asked us to fix your error, and we did.

We can’t make your code work for you.
If you have other questions, then feel free to ask.
But throwing out code and saying “it doesnt do what I want it to do” won’t get you very far. Nobody is just going to give you working code.

We can only help you if you are willing to help us help you. What you gave us is a piece of code with a non-stock function, so we don’t even know what to start with.

Like Niandra said - learn a bit more of GLua and general programming and come here with more concrete questions. Giving us code and saying “it doesn’t work” is not a very good question.

You aren’t making sense. “Feel free to ask” I just did. The non-stock stuff is completely irrelevant. All that matters is everything after the first if statement, that code is run when you swing a stick.

Your variables are being reset every time the OnRun function is called. I’d recommend attaching the variables to the player object. Also since you’d be attaching the variables to the player object, which many other addons do, you want to have unique variable names to avoid conflicts.
[lua]
if Player.varCopied and IsValid(Player.moveEntity) then
local pos = Trace.Hitpos
Player.moveEntity:SetPos(pos)
Player.varCopied = false
Player.moveEntity = nil
else
Player.moveEntity = Trace.Entity
Player.varCopied = true
return
end
[/lua]

Sorry if I’m coming off as an asshole. I’m just saying that in order to get better help you have to ask better questions than simply “it doesn’t work”, simply because it isn’t likely to get you relevant answers (the earlier asnwers did provide a fix for your error, but they didn’t help you make the code do what you want it to)

However, Jeezy’s solution should do what you wanted it to do.


AddStickTool("Move Entity Position", {
	Description = "Moves the position of the target entity",
	Icon = "icon16/bug.png",
	CanTarget = anything,
	OnRun = function(Player, Trace)
		if (Trace.Entity == Entity(0)) then // Removing the world crashes the game. No need to do that.
			return
		end
		if Player.varCopied and IsValid(Player.moveEntity) then
			local pos = Trace.Hitpos
			Player.moveEntity:SetPos(pos)
			Player.varCopied = false
			Player.moveEntity = nil
		else
			Player.moveEntity = Trace.Entity
			Player.varCopied = true
			return 
		end
	end
})

Still just does literally nothing. I don’t get it either.

If you gave us all your code or at least a way to run this code ourselves then we might be able to help you better. I suggest printing to console when something happens (e.g. in all the if loops) to make sure it’s actually executing what you want it to. Also printing all your variable values is a good idea to see if they are all what you expect them to be.

I mean, do you want ALL of it? It is from ScriptFodder but heavily modified and licensed under http://creativecommons.org/licenses/by/4.0/ so… I am allowed to give it to you aren’t I?

[editline]20th September 2015[/editline]

Uh, what’s the disagree for?

Are you sure it’s under that license? Scriptfodder stuff gets shipped with it’s own license that prevents you from giving out the code.

What is licensed under CC BY 4.0?

If it’s on scriptfodder I don’t recommend posting the code here. It was just a suggestion because it would make it possible for us to test it ourselves. Anyways, did you try what I suggested?