How to make a function continue playing while it waits until a variable doesn't equal nil

Hello, I’m unsure on how I can make a function wait until a certain variable ~= nil

I need the function to wait until the Player variable ~= nil
but I don’t know how to do it without the enemy getting stuck waiting for the player to be found

I just don’t know how to go about this.

Here’s the script, its a server script inside a enemy

local function Walk(destination)
	local path = getPath(destination)
	
	if path.Status == Enum.PathStatus.Success then
		
		for i, waypoint in pairs(path:GetWaypoints()) do
			local target = FindTarget()
			if target and target.Humanoid.Health > 0 then
				spongebob.Humanoid.WalkSpeed = 22
				sound:Stop()
				local plr = game.Players:GetPlayerFromCharacter(target)
				remote:FireClient(plr)
				attack(target)
				break
			else
				humanoid:MoveTo(waypoint.Position)
				spongebob.Humanoid.WalkSpeed = 14
				local plr = game.Players:GetPlayerFromCharacter(target) -- This line of code
				remote2:FireClient(plr) -- this line of code
				sound:Play()
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(destination.Position - (spongebob.HumanoidRootPart.CFrame.LookVector * 5))
	end
end

I’ve tried

repeat wait() until plr ~= nil

but the enemy just gets stuck in place, and the rest of the code doesn’t play
I am new to scripting, so I’m not sure if there is an easy way to solve this
Any help is appreciated

You need to spawn a new thread (using task.spawn) it would be like this:

task.spawn(function()
    repeat task.wait() until plr ~= nil 
    -- Code that you want to run after the player waits
    remote2:FireClient(plr) -- this line of code
    sound:Play()
    humanoid.MoveToFinished:Wait()
end)
1 Like

This works but it doesn’t fire to the client (i know this isnt what i asked in the question, sorry)

what does FindTarget() look like?

I suspect that’s the issue.

local function FindTarget()
	local players = game.Players:GetPlayers()
	local maxdist = 70
	local neartarget = nil
	local nearplayer = nil
	
	for i, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local dist = (spongebob.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			
			if dist < maxdist and CanSeeTarget(target) then
				neartarget = target
				maxdist = dist
			end
		end
	end
	
	return neartarget
end

when i print the plr’s name it works, but it doesnt fire to the client

If it prints the plr that makes sense… But at the same time, how is it getting the plr from target if target doesn’t exist…?

The code you’re worrying about shouldn’t even have the player related segment since it’s only executed if there is no found target or said target is dead, meaning there’s no character near enough to it, or the nearest is dead.

That makes no sense since he’s using that to search ALL players to find the nearest character within 70 studs.

I realized that was not the remote function after my reply, my apologies.

1 Like

This is what your code should look like: See the comments for reasoning;

local function Walk(destination)
	local path = getPath(destination)
	
	if path.Status == Enum.PathStatus.Success then
		
		for i, waypoint in pairs(path:GetWaypoints()) do
			local target = FindTarget()
			if target and target.Humanoid.Health > 0 then
				spongebob.Humanoid.WalkSpeed = 22
				sound:Stop()
				local plr = game.Players:GetPlayerFromCharacter(target)
				remote:FireClient(plr)
				attack(target)
				break
			else
				humanoid:MoveTo(waypoint.Position)
				spongebob.Humanoid.WalkSpeed = 14
				-- You shouldn't have the plr segment anyways
				-- This is the "else" code block, meaning this executes when target is nil or dead. Meaning there is no player.
				sound:Play()
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(destination.Position - (spongebob.HumanoidRootPart.CFrame.LookVector * 5))
	end
end
1 Like

Yes, i am having the enemy patrol around different parts when the player variable = nil.
and when the player is no longer nil it follows the player that is given, im trying to fire to the client that the enemy was originally chasing when he is no longer chasing said player. thats where im confused on how i can get the player variable to fire to the client while the player variable = nil, idk if i need to make a table that adds the player when he follows it and when he is done following the player it looks through the table to find the last player he chased and fire to that players client. sorry if this is a bit confusing mb lol

Just cache the player in a variable outside the scope

local function Walk(destination)
	local path = getPath(destination)
	local plr = nil
	
	if path.Status == Enum.PathStatus.Success then
		
		for i, waypoint in pairs(path:GetWaypoints()) do
			local target = FindTarget()
			if target and target.Humanoid.Health > 0 then
				spongebob.Humanoid.WalkSpeed = 22
				sound:Stop()
				plr = game.Players:GetPlayerFromCharacter(target)
				remote:FireClient(plr)
				attack(target)
			else
				humanoid:MoveTo(waypoint.Position)
				spongebob.Humanoid.WalkSpeed = 14
				if plr then
					remote2:FireClient(plr)
				end
				sound:Play()
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(destination.Position - (spongebob.HumanoidRootPart.CFrame.LookVector * 5))
	end
end
1 Like

I’m having the same issue it doesn’t fire to the client, im not sure if i am doing this wrong, sorry. ill see if i can try some other methods and come back, i appreciate the help though.

Try the edited version of my last reply.

This works, but it fires the client multiple times, and when the player is no longer in range it doesnt fire it only fires when the player has died*

fireing to the client multiple times isnt an issue though

Then unfortunately I’m out of ideas, my apologies but I don’t know why it’s not working.

1 Like

No, you’re good i really appreciate the help alot!