Jump to content

Respawn script not working


delete.

Recommended Posts

Hey, just scoped around the forum and I've seen that solidsnake14 has submitted a script that upon death it respawns you to the nearest hospital:

hospitalsTable = { 
{ 1177.7994384766, -1323.0667724609, 14.088536262512 }, 
{ -2656.2421875, 635.99420166016, 14.453125 }, 
{ 1607.1225585938, 1817.8732910156, 10.8203125 }, 
} 
  
    function findNearestHostpital(thePlayer) 
      local nearest = nil 
      local min = 999999 
      for key,val in pairs(hospitalsTable) do 
        local xx,yy,zz=getElementPosition(thePlayer) 
        local x1=val[1] 
        local y1=val[2] 
        local z1=val[3] 
        local dist = getDistanceBetweenPoints2D(xx,yy,x1,y1) 
        if distthen 
          nearest = val 
          min = dist 
        end 
      end 
      return nearest[1],nearest[2],nearest[3] 
      end 
  
function spawn(player) 
local xx,yy,zz = findNearestHostpital(player) 
  spawnPlayer( player, xx,yy,zz) 
  fadeCamera(player,true,3.0) 
end 
  
addEventHandler("onPlayerWasted", root, 
    function() 
    fadeCamera(source,false,6.0,0,0,0) 
    setTimer(spawn, 8000, 1, source) 
    end 
) 

I have tried to use it on my server and it doesn't work, all it does is just infinite loop of camera going left and right. No debug output messages at all.

Link to comment
hospitalsTable = { 
{ 1177.7994384766, -1323.0667724609, 14.088536262512 }, 
{ -2656.2421875, 635.99420166016, 14.453125 }, 
{ 1607.1225585938, 1817.8732910156, 10.8203125 }, 
} 
  
    function findNearestHostpital(thePlayer) 
      local nearest = nil 
      local min = 999999 
      for key,val in pairs(hospitalsTable) do 
        local xx,yy,zz=getElementPosition(thePlayer) 
        local x1=val[1] 
        local y1=val[2] 
        local z1=val[3] 
        local dist = getDistanceBetweenPoints2D(xx,yy,x1,y1) 
        if dist then 
          nearest = val 
          min = dist 
        end 
      end 
      return nearest[1],nearest[2],nearest[3] 
      end 
  
function spawn(player) 
local xx,yy,zz = findNearestHostpital(player) 
  spawnPlayer( player, xx,yy,zz) 
  fadeCamera(player,true,3.0) 
end 
  
addEventHandler("onPlayerWasted", root, 
    function() 
    fadeCamera(source,false,6.0,0,0,0) 
    setTimer(spawn, 8000, 1, source) 
    end 
) 

you had then together with dist so that not work try now

Link to comment
4 hours ago, Dimos7 said:

hospitalsTable = { 
{ 1177.7994384766, -1323.0667724609, 14.088536262512 }, 
{ -2656.2421875, 635.99420166016, 14.453125 }, 
{ 1607.1225585938, 1817.8732910156, 10.8203125 }, 
} 
  
    function findNearestHostpital(thePlayer) 
      local nearest = nil 
      local min = 999999 
      for key,val in pairs(hospitalsTable) do 
        local xx,yy,zz=getElementPosition(thePlayer) 
        local x1=val[1] 
        local y1=val[2] 
        local z1=val[3] 
        local dist = getDistanceBetweenPoints2D(xx,yy,x1,y1) 
        if dist then 
          nearest = val 
          min = dist 
        end 
      end 
      return nearest[1],nearest[2],nearest[3] 
      end 
  
function spawn(player) 
local xx,yy,zz = findNearestHostpital(player) 
  spawnPlayer( player, xx,yy,zz) 
  fadeCamera(player,true,3.0) 
end 
  
addEventHandler("onPlayerWasted", root, 
    function() 
    fadeCamera(source,false,6.0,0,0,0) 
    setTimer(spawn, 8000, 1, source) 
    end 
) 

you had then together with dist so that not work try now

Uh, yea kinda fixed.

The reason I added this script was because when I died I wasn't even spawning, as I said there was an infinite loop going right and left. Just yesterday, it worked fine and it respawned me at LV Hospital, with running no script or no gamemode (I'm running default MTA:SA gamemode, not play). And also when somebody registers it sets him at position 0, 0, 0 again, no script is running for that to occur. Upon death, it also changes my skin and removes all of my guns. Any way to fix this? @Dimos7

Edited by testchannel50
Link to comment
38 minutes ago, CodyJ(L) said:

Save skin and guns in a table when he dies, give him the skins and guns in said table when he respawns.

I thought of doing that (I don't even know how to do it but I know what you are talking about) but as I said, yesterday it just worked fine, it saved my guns and skins? No debug output at all.

 

Link to comment

Untested, but fixed a few things

-- Now finds closest hospital, other one just choose the last hospital

-- Saves and loads skin

-- Saves and loads weapons



function getPedWeapons(ped)
	local playerWeapons = {}
	if ped and isElement(ped) and getElementType(ped) == "ped" or getElementType(ped) == "player" then
		for i=2,9 do
			local wep = getPedWeapon(ped,i)
			local ammo = getPedTotalAmmo (ped,i)
			if wep and wep ~= 0 then
				table.insert(playerWeapons,{wep,ammo})
			end
		end
	else
		return false
	end
	return playerWeapons
end


Weapons = {}
Skins = {}

    hospitalsTable = { 
    { 1177.7994384766, -1323.0667724609, 14.088536262512 }, 
    { -2656.2421875, 635.99420166016, 14.453125 }, 
    { 1607.1225585938, 1817.8732910156, 10.8203125 }, 
    } 
      
        function findNearestHostpital(thePlayer) 
          local nearest = nil 
          local mini = 999999 
          for key,val in pairs(hospitalsTable) do 
            local xx,yy,zz=getElementPosition(thePlayer) 
            local x1=val[1] 
            local y1=val[2] 
            local z1=val[3] 
            local dist = getDistanceBetweenPoints2D(xx,yy,x1,y1) 
            if dist then 
              if dist < mini then
			  mini = dist
			  nearest = val
			  end
            end 
          end 
          return nearest[1],nearest[2],nearest[3] 
          end 
      
    function spawn(player) 
     xx,yy,zz = findNearestHostpital(player) 
      spawnPlayer( player, xx,yy,zz) 
      fadeCamera(player,true,3.0) 
	  
	  setElementModel(player,Skins[player] or 0)
	  
		for i,v in pairs(weapons[player]) do
			giveWeapon ( player, v[1], v[2] )
		end 
		
		weapons[player] = nil
		Skins[player] = nil
    end
    addEventHandler("onPlayerWasted", root, 
        function() 
		
		weapons[source] = getPedWeapons(source)
			Skins[source] = getElementModel(source)
	 
        fadeCamera(source,false,6.0,0,0,0) 
        setTimer(spawn, 8000, 1, source) 
        end 
    ) 

 

Edited by CodyJ(L)
Fixed source
Link to comment
4 minutes ago, CodyJ(L) said:

Untested, but fixed a few things

-- Now finds closest hospital, other one just choose the last hospital

-- Saves and loads skin

-- Saves and loads weapons



function getPedWeapons(ped)
	local playerWeapons = {}
	if ped and isElement(ped) and getElementType(ped) == "ped" or getElementType(ped) == "player" then
		for i=2,9 do
			local wep = getPedWeapon(ped,i)
			local ammo = getPedTotalAmmo (ped,i)
			if wep and wep ~= 0 then
				table.insert(playerWeapons,{wep,ammo})
			end
		end
	else
		return false
	end
	return playerWeapons
end


Weapons = {}
Skins = {}

    hospitalsTable = { 
    { 1177.7994384766, -1323.0667724609, 14.088536262512 }, 
    { -2656.2421875, 635.99420166016, 14.453125 }, 
    { 1607.1225585938, 1817.8732910156, 10.8203125 }, 
    } 
      
        function findNearestHostpital(thePlayer) 
          local nearest = nil 
          local mini = 999999 
          for key,val in pairs(hospitalsTable) do 
            local xx,yy,zz=getElementPosition(thePlayer) 
            local x1=val[1] 
            local y1=val[2] 
            local z1=val[3] 
            local dist = getDistanceBetweenPoints2D(xx,yy,x1,y1) 
            if dist then 
              if dist < mini then
			  mini = dist
			  nearest = val
			  end
            end 
          end 
          return nearest[1],nearest[2],nearest[3] 
          end 
      
    function spawn(player) 
     xx,yy,zz = findNearestHostpital(player) 
      spawnPlayer( player, xx,yy,zz) 
      fadeCamera(player,true,3.0) 
	  
	  setElementModel(player,Skins[player] or 0)
	  
		for i,v in pairs(weapons[player]) do
			giveWeapon ( player, v[1], v[2] )
		end 
		
		weapons[player] = nil
		Skins[player] = nil
    end
    addEventHandler("onPlayerWasted", root, 
        function() 
		
		weapons[source] = getPedWeapons(source)
			Skins[source] = getElementModel(source)
	 
        fadeCamera(source,false,6.0,0,0,0) 
        setTimer(spawn, 8000, 1, source) 
        end 
    ) 

 

ERROR: respawn\server.lua:63: attempt to index global 'weapons' (a nil value)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...