Need help in getting death position

Hello whoever is reading this, hope you have a great day first of all. So, I’m learning Lua right now, it’s pretty fun but I seem to be stuck on something I’m trying to do and can’t find how to do it.

Currently, I’m trying to make a simple command (or at least, I’d think it is simple) for ULX that gets the position that the target player last died at and saves it (this will update on each death) so that when the command is used it will send them back to their death position. The dilemma here is I can’t find out how to get the death position and store it so that it can be used later to set the position of the target player.

If anyone could shed some light here, I’d much appreciate it.



hook.Add('PlayerDeath', 'ULX.Deathpos', function(ply)
	ply.DeathPos = ply:GetPos()
end)

hook.Add('PlayerSpawn', 'ULX.DeathposC', function(ply)
	ply.DeathPos = nil
end)


Thanks but I still have one issue, how do I set the position of the player being targeted to the DeathPos? I’ve tried a variety of things (with SetPos()) all with different errors associated.

From my understanding the death pos will only be a pos while the player is dead and can you post come code snippets to what you have tried

A bunch of things with SetPos, trying to get it to set the pos to the deathpos. Although now that you mention it the second hook would delete the deathpos.


v:SetPos( DeathPos )
v:SetPos( ply.DeathPos )
v:SetPos( ULX.Deathpos )
v:SetPos( "ULX.Deathpos" )

V is the target player for clarification. Also, I have tried putting quotation marks on each attempt above and tried it without quotation marks, no difference. It’s probably completely wrong, but I can’t seem to find much info on SetPos. I looked at the wiki which confuses me some more because it says SetPos takes vector arguments yet I’ve looked at other scripts and they have used things like


v:SetPos( newpos )

so I know its doable.

Thanks for the help by the way :slight_smile:

Your soo close
[LUA]
v:SetPos( v.DeathPos )
[/LUA]
should work by my understanding

This returns an error too in the server console.


bad arguement #1 to 'SetPos' (Vector expected, got nil)

Don’t create the PlayerSpawn hook.

Untested, but its how you want it to look



function ulx.diepos( calling_ply, target_ply )
	--if not calling_ply:IsValid() then
	--	Msg( "You are the console, you can't teleport or teleport others since you can't see the world!
" )
	--	return
	--end

	--if ulx.getExclusive( target_ply, calling_ply ) then
	--	ULib.tsayError( calling_ply, ulx.getExclusive( target_ply, calling_ply ), true )
	--	return
	--end

	if not target_ply:Alive() then
		ULib.tsayError( calling_ply, target_ply:Nick() .. " is dead!", true )
		return
	end

	if not target_ply.DeathPos then
		ULib.tsayError( calling_ply, target_ply:Nick() .. " haven't died yet!", true )
		return
	end

	target_ply.ulx_prevpos = target_ply:GetPos()
	target_ply.ulx_prevang = target_ply:EyeAngles()

	if target_ply:InVehicle() then
		target_ply:ExitVehicle()
	end

	target_ply:SetPos( target_ply.DeathPos )
	target_ply:SetLocalVelocity( Vector( 0, 0, 0 ) ) -- Stop!

	ulx.fancyLogAdmin( target_ply, "#T was returned to his last dead position." )
	target_ply.DeathPos = nil
end
local diepos = ulx.command( CATEGORY_NAME, "ulx diepos", ulx.teleport, {"!dp", "!diepos"} )
diepos:addParam{ type=ULib.cmds.PlayerArg, ULib.cmds.optional }
diepos:defaultAccess( ULib.ACCESS_ADMIN )
diepos:help( "Teleports target." )


Just an edited ULX teleport function.

try commenting out the code
[LUA]
hook.Add(‘PlayerSpawn’, ‘ULX.DeathposC’, function(ply)
ply.DeathPos = nil
end)
[/LUA]
that iJohnny posted(im not saying its wrong i just think thats not what you want)
or replace it with
[LUA]
hook.Add(‘PlayerInitialSpawn’, ‘ULX.DeathposC’, function(ply)
ply.DeathPos = ply:GetPos()
end)
[/LUA]
so when you spawn initially it sets your “last death pos” to where you spawned

Eew, mixed tabs and spaces! :vomit:

Ya, sorry, i hate it too. Just used an online editor :sick:

[editline]A[/editline]
Fixed! Lets take care about eyes and nerves of each other :v:

You missed a spot.

:nope:
Totally fixed!

Rejoice! :dance:

Tried a bunch of things, including your code Johnny, didn’t work. In the end, the error came down to:


bad arguement #1 to 'SetPos' (Vector expected, got nil)

Happened when I tried using SetPos to set to DeathPos like Johnny did and attempted it the way rtm told me to, both returned that same issue.

I know nothing about ULX but my guess is that this:[lua] target_ply:SetPos( target_ply.DeathPos )[/lua]was meant to be:[lua] target_ply:SetPos( target_ply.ulx_prevpos )[/lua]

Edit: and actually all occurences of DeathPos in that snippet are supposed to be changed to ulx_prevpos

Append your lua/ulx/modules/sh/teleport.lua



if SERVER then
	hook.Add('PlayerDeath', 'ULX.Deathpos', function(ply)
		ply.DeathPos = ply:GetPos()
	end)
end

function ulx.diepos( calling_ply )
	--if not calling_ply:IsValid() then
	--	Msg( "You are the console, you can't teleport or teleport others since you can't see the world!
" )
	--	return
	--end

	--if ulx.getExclusive( target_ply, calling_ply ) then
	--	ULib.tsayError( calling_ply, ulx.getExclusive( target_ply, calling_ply ), true )
	--	return
	--end

	if not calling_ply:Alive() then
		ULib.tsayError( calling_ply, calling_ply:Nick() .. " is dead!", true )
		return
	end

	if not calling_ply.DeathPos or not isvector(calling_ply.DeathPos) then
		ULib.tsayError( calling_ply, calling_ply:Nick() .. " haven't died yet!", true )
		return
	end

	calling_ply.ulx_prevpos = calling_ply:GetPos()
	calling_ply.ulx_prevang = calling_ply:EyeAngles()

	if calling_ply:InVehicle() then
		calling_ply:ExitVehicle()
	end

	calling_ply:SetPos( calling_ply.DeathPos )
	calling_ply:SetLocalVelocity( Vector( 0, 0, 0 ) ) -- Stop!

	ulx.fancyLogAdmin( calling_ply, "#T was returned to his last dead position." )
	calling_ply.DeathPos = nil
end
local diepos = ulx.command( CATEGORY_NAME, "ulx diepos", ulx.diepos, {"!dp", "!diepos"} )
diepos:defaultAccess( ULib.ACCESS_ADMIN )
diepos:help( "Teleports target." )


Sorry, missed some things before, now its tested and works (edit some s#1 words in errors)

So, after a bunch of things this is the code I have now (took Neat-Nit’s advice on replacing deathpos with ulx_prevpos, I believe he is correct):



local CATEGORY_NAME = "Utility"

function ulx.deathreturn( calling_ply, target_plys )

	local affected_plys = {}

	for i=1, #target_plys do
		local v = target_plys[ i ]

	if not v:Alive() then
		ULib.tsayError( calling_ply, v:Nick() .. " is dead!", true )
		return
	end

	if not v.ulx_prevpos then
		ULib.tsayError( calling_ply, v:Nick() .. " hasn't died yet!", true )
		return
	end

	v.ulx_prevpos = v:GetPos()
	v.ulx_prevang = v:EyeAngles()

	if v:InVehicle() then
		v:ExitVehicle()
	end

	v:SetPos( v.ulx_prevpos )
	v:SetLocalVelocity( Vector( 0, 0, 0 ) ) -- To stop the player.

	ulx.fancyLogAdmin( target_plys, "#T was returned to his last position of death." )
		 v.ulx_prevpos = nil
	end
end

local deathreturn = ulx.command ( CATEGORY_NAME, "ulx deathreturn", ulx.deathreturn, "!deathreturn")
deathreturn:addParam{ type=ULib.cmds.PlayersArg }
deathreturn:defaultAccess( ULib.ACCESS_ADMIN )
deathreturn:help( "Returns target to their last position of death." )

The for loop at the top and the local variables near it are taken from the fun.lua code (fun.lua comes with ULX), if it’s not added some errors will happen (like target_ply:Alive() will cause some error to do with indexing and nil values). Now, this code has no errors except in-game it doesn’t work. It WILL work with the "Name is dead’, “Name has no death position” but it won’t actually teleport you to the death position. Any ideas or anything wrong you can spot (probably a lot :D)?

Did you miss my last post? Watch above :weeb:

This is now solved, thank you, everyone, for your help. Have a good day :smiley: