Jump to content

[HELP] Spawn after death


Recommended Posts

1 hour ago, stPatrick said:

onPlayerWasted event => call the respawn function => find the cloesest point from the array with for loop => spawnPlayer

I understand the algorithm, but I don 't understand how to write it in code. I have x1, y1, z2 - the last coordinates before death, and there are x2, y2, z2 - the coordinates of spawn after death. I got the data, and then what? (How it should look to code)

Link to comment
  • Moderators
19 hours ago, Mr. Maxilian said:

I understand the algorithm, but I don 't understand how to write it in code. I have x1, y1, z2 - the last coordinates before death, and there are x2, y2, z2 - the coordinates of spawn after death. I got the data, and then what? (How it should look to code)

 

Looks like something like this

local respawnPoints = {
	{0, 0, 3},
}

local function findNearestRespawnPoint(x, y, z)
	local nearestPoint = { distance = 99999, index = 1 }
	
	for i, v in ipairs(respawnPoints) do
		local distance = getDistanceBetweenPoints3D(x, y, z, v[1], v[2], v[3])
		
		if distance < nearestPoint.distance then
			nearestPoint = { distance = distance, index = i }
		end
	end
	
	return respawnPoints[nearestPoint.index]
end

local function respawnPlayer(player)
	local x, y, z = getElementPosition(player)
	local point = findNearestRespawnPoint(x, y, z)
	
	spawnPlayer(player, point[1], point[2], point[3])
end

addEventHandler("onPlayerWasted", root, function()
	setTimer(respawnPlayer, 1000, 1, source)
end)

 

  • Thanks 2
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...