Kill a player when near another player

Im trying to make a system where for example, player1 was chosen to kill everyone. Then player2 walks up to player1 and then dies. How can i do that? (I already have the chosen part done)

You have two ways.

First way is taking the player1’s position (killer) and subtract it to player2’s position (survivor) to get the Magnitude which shows the distance.

Second is raycasting, in which you get the direction of player1 facing player2 in Vector3 form, then shoot it in a certain amount of studs then constantly check if it hits the target.

1 Like

i did this


local RP = game:GetService("ReplicatedStorage")


local function canSeeTarget(playerI, target)
	local origin = playerI.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - playerI.HumanoidRootPart.Position).unit * 40
	local ray = Ray.new(origin, direction)

	local hit, pos = workspace:FindPartOnRay(ray, playerI)

	if hit then
		if hit:IsDescendantOf(target) then
			return true
		end
	else
		return false
	end
end


local function findTarget(playerI)
	local players = game.Players:GetPlayers()
	local maxDistance = 40
	local nearestTarget

	for i, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (playerI.HumanoidRootPart.Position - target.HumanoidRootPart.Position).magnitude

			if distance < maxDistance and canSeeTarget(target) then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end

	return nearestTarget
end

local function attack(playerI, target)
	local distance = (playerI.HumanoidRootPart.Position - target.HumanoidRootPart.Position).magnitude
		
	if distance > 8 then
		playerI.Character.Humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		local player = game.Players:GetPlayerFromCharacter(target)
		RP.Remotes.TriggerJumpscare:FireClient(player)
		local attackAnim = 	playerI.Character.Humanoid:LoadAnimation(script.Attack)
		attackAnim:Play()
		attackAnim.Stopped:Wait()
		target.Humanoid:TakeDamage(100)
	end
end


local function walkTo(player)
	local target = findTarget()
	if target and target.Humanoid.Health > 0 then
		print("TARGET FOUND", target.Name)
		attack(player, target)
	else
	end
end

local function walkAround(player)
	while wait() do
		walkTo(player)
	end
end

RP.Remotes.TriggerDonut.Event:Connect(function(player)
	coroutine.wrap(function()
		while wait() do
			walkAround(player)
		end
	end)()
end)

But nothing is happening is there a reason why?

there is a much simpler but funky way
just summon explosions in the player and make them not visible and not do anything
use explosion.hit

local part = part here
while true do
	local exp = Instance.new("Explosion")
	exp.Parent = part
	exp.Position = part.Position
	exp.Visible = false
	exp.BlastRadius = 5
	exp.BlastPressure = 0
	game:GetService("Debris"):AddItem(exp,0.4)
	exp.Hit:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
		if hit.Parent.Name ~= (murderer name) then
			hit.Parent:FindFirstChild("Humanoid"):TakeDamage(10)
		end
		end
	end)
	wait()
end