false lua error

i get my position with GetPos(), in another spawn hooked function i use SetPos(getposvariable)

GetPos returns as vector.
when i spawn
SetPos sets my pos as i wanted, works nice.
in console, it says vector expected on setpos.
when i do it like


SetPos(Vector(getposvariable))

it returns nil. so it sets my position to 0.00,0.00,0.00 when i spawn.
if i just ignore lua error, code doesnt run.

what do i do?

Exact lua error and code…

Also it just needs to be SetPos( variable )

when player says something:


pPos = ply:GetPos()

when player spawns:


ply:SetPos(pPos)

lua error


[ERROR] lua/build.lua:33: bad argument #1 to 'SetPos' (Vector expected, got nil)

when he said full code, he meant full code and not just snippets of it

he didnt said full code.

Full code.

But from what it seems, pPos is nil when SetPos is called, when is it defined?



if SERVER then
local setBaseN = 0
local pPos
for k,v in pairs(player.GetAll()) do


function buildm()
v:PrintMessage( HUD_PRINTTALK,"Build mod started! Please wait 5 seconds!")
timer.Create("startbuild", 5,1, function()




v:Spawn()
RunConsoleCommand("sbox_noclip", 1)
RunConsoleCommand("sbox_godmode", 1)
RunConsoleCommand("sbox_maxprops", 200)	end)
end

concommand.Add( "buildmode", buildm)

 end

--Hooks
hook.Add( "PlayerSay", "Setbase", function( ply, text, public )
	text = string.lower( text ) -- Make the chat message entirely lowercase
	if ( text == "!setbase" ) then
		setBaseN = 1
		 pPos = Vector(ply:GetPos())
		ply:PrintMessage( HUD_PRINTTALK,"Your start position set to " .. tostring(pPos))
		return ""
	end
end )
hook.Add("PlayerSpawn", "pOnSpawn", function(ply)
	ply:PrintMessage( HUD_PRINTTALK,"Your  position set to " .. tostring(pPos))
		ply:StripWeapons()
ply:Give("gmod_tool")
ply:Give("weapon_physgun")
	ply:SetPos(pPos)
	end)


--ConsoleCommands


end



iirc, Entity:GetPos() already returns a vector, so Vector(ply:GetPos()) is nil.

Just do pPos = ply:GetPos()

[lua]if SERVER then
local pPos[/lua]

Wat.

Also what he said ^

  1. qPos isn’t defined when you spawn in … its nil. Its trying to spawn a player at an unknown position, when you just do “local qPos”.
  2. concommand.Add is serverside and global … you don’t have to run it in a loop on everyone. Run it once.
  3. qPos is global … everyone can set your position in the chat. Do “ply.qPos” instead to save it onto the player.

lol, that was for try. of course i tried “pPos = ply:GetPos()”, that was first thing i did. as i said “GetPos returns as vector.”

Also please dont try to find my mistakes. I can fix them. i just need what i said.

When the server starts up there are no entities … therefor the position is unknown. You should tell it to ignore it if its nil:



hook.Add("PlayerSpawn", "pOnSpawn", function(ply)
		ply:PrintMessage( HUD_PRINTTALK,"Your  position set to " .. tostring(pPos))
		ply:StripWeapons()
		ply:Give("gmod_tool")
		ply:Give("weapon_physgun")
		if type(pPos)!="nil" then
			ply:SetPos(pPos)
		end
	end)


Be happy that someone took their time to help you instead of getting their coffee.

I shouldn’t have give my code. people trying to fix other stuff in my code.
Read this if you will post a comment.
The problem is at GetPos which i get it with !setbase, and when i die, even though it sets my position, it gives me a lua error.

IT DOESNT SET MY LOCATION TO NIL, AND I DONT WANT THAT

[editline]17th August 2015[/editline]

Alright i fixed my problem myself. next time i wont open thread.