Is there a way to uncall a function?

i am having a problem with this, everytime i call the function and the player dies the function still works on the player. is there a way to make it so it doesnt do that?
code:

local function infect()
	local last = nil
	for i,v in pairs(game.Players:GetChildren()) do
		if last == nil then
			last = v
		end
		if last.Character and v.Character then
			if last and last ~= v then
			local dist = (last.Character.HumanoidRootPart.Position-v.Character.HumanoidRootPart.Position).magnitude	
			         	if dist <= 5 then
						print("The player "..v.Name.." is close to "..last.Name)	
					local boolvalue = Instance.new("BoolValue")
							boolvalue.Parent = v.Character 
							local boolvalue2 = Instance.new("BoolValue")
					boolvalue2.Parent = last.Character 
						boolvalue.Name = "Infected"
							boolvalue = true
							boolvalue2 = true
					print("yes")
                        if v.Character:FindFirstChild("BoolValue") == true then
						       print("no")
						                 	end
						last = v
					 end	
				 end
			 end
		 end
	  end

So i think you should work with coroutines for this. You can read the article here. With this you can basically just pause Threads from working.

return (without any brackets) is used to instantly kill a function. You can also use it in more advanced functions to return some objects. For example:

local function findp()
    local part = workspace.Part
    return part
end

local Part = findp()
print(Part.Name) --> Part

Otherwise, you can write nothing after the return to instantly kill a function.
If I guess correctly, this should be your final version:

local function infect()
	local last = nil
	for i,v in pairs(game.Players:GetChildren()) do
		if last == nil then
			last = v
		end
		if last.Character and v.Character then
			if last and last ~= v then
			local dist = (last.Character.HumanoidRootPart.Position-v.Character.HumanoidRootPart.Position).magnitude	
			         	if dist <= 5 then
						print("The player "..v.Name.." is close to "..last.Name)	
					local boolvalue = Instance.new("BoolValue")
							boolvalue.Parent = v.Character 
							local boolvalue2 = Instance.new("BoolValue")
					boolvalue2.Parent = last.Character 
						boolvalue.Name = "Infected"
							boolvalue = true
							boolvalue2 = true
					print("yes")
                        if v.Character:FindFirstChild("BoolValue") == true then
						       print("no")
                               return
						                 	end
						last = v -- You should note that this won't work after return
					 end	
				 end
			 end
		 end
	  end

Will this work until the player dies?

I think yes, unless the script prints “no” while the player is alive.

its not even calling it for some reason
is there something wrong?
code:

local rs = game:GetService("RunService")
--local soundfolder = script.SoundFolder:GetChildren()
local random = math.random(1,2)
local debounce = true
local i = 2
local infected = false
--local value = game.StarterPlayer.StarterCharacterScripts.Infected.Value
local function infect()
	local last = nil
	for i,v in pairs(game.Players:GetChildren()) do
		if last == nil then
			last = v
		end
		if last.Character and v.Character then
			if last and last ~= v then
			local dist = (last.Character.HumanoidRootPart.Position-v.Character.HumanoidRootPart.Position).magnitude	
			         	if dist <= 5 then
						print("The player "..v.Name.." is close to "..last.Name)	
					local boolvalue = Instance.new("BoolValue")
							boolvalue.Parent = v.Character 
							local boolvalue2 = Instance.new("BoolValue")
					boolvalue2.Parent = last.Character 
						boolvalue.Name = "Infected"
							boolvalue = true
							boolvalue2 = true
					print("yes")
                        if v.Character:FindFirstChild("BoolValue") == true then
						       print("no")
                               return
						                 	end
						last = v -- You should note that this won't work after return
					 end	
				 end
			 end
	     end
end

		
local playerschildren = game.Players:GetChildren()
	game.ReplicatedStorage.characteradded.OnServerEvent:Connect(function(plr)
	local randomnum2 = math.random(1,2)
	print(randomnum2)
	if randomnum2 == 2 then
		infect(plr)
		end
end)
local rs = game:GetService("RunService")
--local soundfolder = script.SoundFolder:GetChildren()
local random = math.random(1,2)
local debounce = true
local i = 2
local infected = false
--local value = game.StarterPlayer.StarterCharacterScripts.Infected.Value
local function infect()
	local last
	for i,v in pairs(game.Players:GetChildren()) do
		if last == nil then
			last = v
		end
		if last.Character and v.Character  and v.Character.Humanoid.Health > 0 and last.Character.Humanoid.Health > 0 then
			if last and last ~= v then
			local dist = (last.Character.HumanoidRootPart.Position-v.Character.HumanoidRootPart.Position).magnitude	
			         	if dist <= 5 then
						print("The player "..v.Name.." is close to "..last.Name)	
					local boolvalue = Instance.new("BoolValue")
							boolvalue.Parent = v.Character 
							local boolvalue2 = Instance.new("BoolValue")
					boolvalue2.Parent = last.Character 
						boolvalue.Name = "Infected"
							boolvalue.Value = true
							boolvalue2.Value = true
					print("yes")
                        if v.Character:FindFirstChild("BoolValue") == true then
						       print("no")
						                 	end
						last = v
					 end	
				 end
			 end
	     end
end

		
local playerschildren = game.Players:GetChildren()
	game.ReplicatedStorage.characteradded.OnServerEvent:Connect(function(plr)
	local randomnum2 = math.random(1,2)
	print(randomnum2)
	if randomnum2 == 2 then
		infect(plr)
		end
end)

I’m not sure if this is what you wanted, but if any of 2 players mentioned in the script is dead, nothing happens. Plus I fixed some other stuff.