Jump to content

Something wrong with this script (Hospital Respawn)


Recommended Posts

Well, while testing this script, and making a few changes to it (With or without making changes) when the player is wasted, it makes the player re-spawn to only one location (Jefferson) out of the 8 provided in the table. I don't understand what's wrong with this script, would be very helpful if you guys give me a hand in checking.

Also, this script isn't showing any debug error/warning in both In game debugger and on the console. (Most annoying)

hosp_loc = {
	{-2655, 635, 14, 180}, -- San Fierro
	{2038, -1411, 17, 130}, --Jefferson
	{1173, -1323, 15, 270}, -- All Saints
	{1242, 327, 19, 340}, -- Red County
	{1609, 1816, 12, 360}, -- Las Venturas
	{-2200, -2311, 30, 320}, -- Angel Pine
	{-320, 1049, 20, 0}, --Fort Carson - Bone County
	{-1514.5, 2524, 56, 0} --Tierra Robada
}

function getDistance(thePlayer)
	local px, py, pz = getElementPosition(thePlayer)
	local closestDistance
	local closestPoint

	for i = 1, #hosp_loc do
		local hx, hy, hz = hosp_loc[i][1], hosp_loc[i][2], hosp_loc[i][3]
		local distance = getDistanceBetweenPoints3D(hx, hy, hz, px, py, pz)
		if (not closestDistance or distance < closestDistance) then 
			closestDistance = distance
			closestPoint = hosp_loc[i]
		end
	end
	local x = (tostring(closestPoint[1])) --x
	local y = (tostring(closestPoint[2])) --y
	local z = (tostring(closestPoint[3])) --z
	local r = (tostring(closestPoint[4])) --rotation

	skin = getElementData(thePlayer, "respawn-skin")
	spawnPlayer(thePlayer, x, y, z, r, skin)
	setElementModel(thePlayer, skin)
	fadeCamera(thePlayer, true, 2.5)
end

function Respawn()
	
	local skinid = getElementModel(source)
	setElementData(source, "respawn-skin", skinid)
	setTimer(getDistance, 7500, 1, source)
end
addEventHandler("onPlayerWasted", root, Respawn)

 

Link to comment
hosp_loc = {
	{-2655, 635, 14, 180}, -- San Fierro
	{2038, -1411, 17, 130}, --Jefferson
	{1173, -1323, 15, 270}, -- All Saints
	{1242, 327, 19, 340}, -- Red County
	{1609, 1816, 12, 360}, -- Las Venturas
	{-2200, -2311, 30, 320}, -- Angel Pine
	{-320, 1049, 20, 0}, --Fort Carson - Bone County
	{-1514.5, 2524, 56, 0} --Tierra Robada
}


function getDistance(thePlayer)
	local dist = 9999999
	local data = nil
	for _, v in ipairs ( hosp_loc ) do
		local x, y, z = getElementPosition (thePlayer)
		local c = getDistanceBetweenPoints3D ( x, y, z, v[1], v[2], v[3] )
		if ( c <= dist ) then
			dist = c
			data = { v[1], v[2], v[3], v[4]}
		end
	end	
	if (data) then
		skin = getElementData(thePlayer, "respawn-skin")
		spawnPlayer(thePlayer, data[1], data[2], data[3], data[4], skin)
		setElementModel(thePlayer, skin)
		fadeCamera(thePlayer, true, 2.5)
	end
end

function Respawn()
	local skinid = getElementModel(source)
	setElementData(source, "respawn-skin", skinid)
	setTimer(getDistance, 7500, 1, source)
end
addEventHandler("onPlayerWasted", root, Respawn)

 

Link to comment
hosp_loc = {
	{-2655, 635, 14, 180}, -- San Fierro
	{2038, -1411, 17, 130}, --Jefferson
	{1173, -1323, 15, 270}, -- All Saints
	{1242, 327, 19, 340}, -- Red County
	{1609, 1816, 12, 360}, -- Las Venturas
	{-2200, -2311, 30, 320}, -- Angel Pine
	{-320, 1049, 20, 0}, --Fort Carson - Bone County
	{-1514.5, 2524, 56, 0} --Tierra Robada
}

addEventHandler("onPlayerWasted", root, function()
    setTimer(function(thePlayer, position, model)
        local positions = {}
        local px, py, pz = unpack(position)
        for index = 1, #hosp_loc do
            table.insert(positions, {getDistanceBetweenPoints3D(px, py, pz, unpack(hosp_loc[index], 1, 3)), hosp_loc[index]})
        end
        table.sort(positions, function(a, b)
            return a[1] < b[1]
        end)
        local x, y, z, r = unpack(positions[1][2])
        spawnPlayer(thePlayer, x, y, z, r, skin)
        fadeCamera(thePlayer, true, 2.5)
    end, 7500, 1, source, {getElementPosition(source)}, getElementModel(source))
end)

 

Edited by #,xiRocKyz
Link to comment
hosp_loc = {
    {-2655, 635, 14, 180}, -- San Fierro
    {2038, -1411, 17, 130}, --Jefferson
    {1173, -1323, 15, 270}, -- All Saints
    {1242, 327, 19, 340}, -- Red County
    {1609, 1816, 12, 360}, -- Las Venturas
    {-2200, -2311, 30, 320}, -- Angel Pine
    {-320, 1049, 20, 0}, --Fort Carson - Bone County
    {-1514.5, 2524, 56, 0} --Tierra Robada
}

function Respawn()
  local randPosition = math.random(1, #hosp_loc)
    local skinid = getElementModel(source)
  outputChatBox("You respawn in 10 second!", source, 255, 255, 255, true)
    setTimer(function()
      spawnPlayer(source, hosp_loc[randPosition][1], hosp_loc[randPosition][2], hosp_loc[randPosition][3], hosp_loc[randPosition][4], skinid, 0, 0)
  end, 10000, 1)
end
addEventHandler("onPlayerWasted", root, Respawn)

 

Edited by holuzs
Link to comment

@HassoN

Check your code again

@holuzs

That's not what he wanted

 

Try this

1 hour ago, #,xiRocKyz said:

hosp_loc = {
	{-2655, 635, 14, 180}, -- San Fierro
	{2038, -1411, 17, 130}, --Jefferson
	{1173, -1323, 15, 270}, -- All Saints
	{1242, 327, 19, 340}, -- Red County
	{1609, 1816, 12, 360}, -- Las Venturas
	{-2200, -2311, 30, 320}, -- Angel Pine
	{-320, 1049, 20, 0}, --Fort Carson - Bone County
	{-1514.5, 2524, 56, 0} --Tierra Robada
}

addEventHandler("onPlayerWasted", root, function()
    setTimer(function(thePlayer, position, model)
        local positions = {}
        local px, py, pz = unpack(position)
        for index = 1, #hosp_loc do
            table.insert(positions, {getDistanceBetweenPoints3D(px, py, pz, unpack(hosp_loc[index], 1, 3)), hosp_loc[index]})
        end
        table.sort(positions, function(a, b)
            return a[1] < b[1]
        end)
        local x, y, z, r = unpack(positions[1][2])
        spawnPlayer(thePlayer, x, y, z, r, skin)
        fadeCamera(thePlayer, true, 2.5)
    end, 7500, 1, source, {getElementPosition(source)}, getElementModel(source))
end)

 

 

Link to comment
2 hours ago, #,xiRocKyz said:

hosp_loc = {
	{-2655, 635, 14, 180}, -- San Fierro
	{2038, -1411, 17, 130}, --Jefferson
	{1173, -1323, 15, 270}, -- All Saints
	{1242, 327, 19, 340}, -- Red County
	{1609, 1816, 12, 360}, -- Las Venturas
	{-2200, -2311, 30, 320}, -- Angel Pine
	{-320, 1049, 20, 0}, --Fort Carson - Bone County
	{-1514.5, 2524, 56, 0} --Tierra Robada
}

addEventHandler("onPlayerWasted", root, function()
    setTimer(function(thePlayer, position, model)
        local positions = {}
        local px, py, pz = unpack(position)
        for index = 1, #hosp_loc do
            table.insert(positions, {getDistanceBetweenPoints3D(px, py, pz, unpack(hosp_loc[index], 1, 3)), hosp_loc[index]})
        end
        table.sort(positions, function(a, b)
            return a[1] < b[1]
        end)
        local x, y, z, r = unpack(positions[1][2])
        spawnPlayer(thePlayer, x, y, z, r, skin)
        fadeCamera(thePlayer, true, 2.5)
    end, 7500, 1, source, {getElementPosition(source)}, getElementModel(source))
end)


 

replace "skin" on line 23 to "model"

 

24 minutes ago, HassoN said:

Player's position isn't going to change if the player dies, unless you spawn him somewhere else. So nope. My code should work.

I know that the position is not going to change

but if the position is not changed then his code is working already

so he must have another script that changes the position

Edited by #,xiRocKyz
  • Thanks 1
Link to comment

Well, checked them all, HassoN's  script isn't working, same problem as mine. Rockyz's script is working, however, thanks Holuzs for helping, but I guess, Rockyz script is more stable to use, however, I would like to see if HassoN's script is fixable too, as his scripting style matches with mine the most, would be easy for me to make further changes in future.

 

Thanks all!

Good work, Rockyz.

  • Like 1
Link to comment
3 minutes ago, Master_11 said:

Well, checked them all, HassoN's  script isn't working, same problem as mine. Rockyz's script is working, however, thanks Holuzs for helping, but I guess, Rockyz script is more stable to use, however, I would like to see if HassoN's script is fixable too, as his scripting style matches with mine the most, would be easy for me to make further changes in future.

 

Thanks all!

Good work, Rockyz.

I've just tested it. It works for me.

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...