Player respawn in the same spot he died

I want a player who has died to respawn in the same spot he died.

How do I do that?

[lua]hook.Add(“PlayerDeath”,“PlayerDeathRespawnPos”,function(ply)
local pos = ply:GetPos()
ply:Spawn()
ply:SetPos(pos)
end)[/lua]

Thanks!

Note : i did not test it and i am not sure about the hook either.

No, that doesn’t work.

[lua]
hook.Add(“PlayerDeath”,“PlayerDeathRespawnPos”,function(ply)
ply.DeadPos = ply:GetPos()
end)

hook.Add(“PlayerSpawn”,“PlayerDeathRespawnPos2”,function(ply)
ply:SetPos(ply.DeadPos)
end)

[/lua]

;3

This would break as soon as the first player spawns since ply.DeadPos is not defined, also why would my code not work?

what

My thoughts exactly…

kevkev is right, the first time a player spawns ply.DeadPos will not be defined because they haven’t died yet.

[lua]
local function playerDies( victim, weapon, killer )
victim.NewSpawnPos = victim:GetPos()
end

local function playerSpawns(ply)
if victim.NewSpawnPos then ply:SetPos(victim.NewSpawnPos + Vector(0,0,16)) end
end

hook.Add(“PlayerSpawn”, “playerSpawn”, playerSpawns)
hook.Add(“PlayerDeath”, “playerDeath”, playerDies )
[/lua]

Spawning where you dies sounds like spawn camper hell soooooo
[lua]
local function playerDies( victim, weapon, killer )
victim.NewSpawnPos = victim:GetPos()
end

local function playerSpawns(ply)
if victim.NewSpawnPos then ply:SetPos(victim.NewSpawnPos + Vector(0,0,16)) end
ply:GodEnable() // noone likes spawn campers
timer.Create(“disablegodlater”, 8, 1, function() //change the 8 to how long you want the player to have spawnprotection
ply:GodDisable()
end )
end

hook.Add(“PlayerSpawn”, “playerSpawn”, playerSpawns )
hook.Add(“PlayerDeath”, “playerDeath”, playerDies )
[/lua]