Repeat loop problem

You can write your topic however you want, but you need to answer these questions:

  1. I Want to make the bot attack one Target at a time.

  2. When the Remote event fire more than one time it Start attack more than one target.

The bot is a pet and This is a part of the script of the bot.

local function Attack(Target)
	if FunctionRunning then return end
	FunctionRunning = true
	repeat
		wait(0.5)
		print(Target)
		NoobModule:Attack(Character, Target)
	until Attacking ~= true or Following == true or Target.Humanoid.Health == 0 or FunctionRunning == false
	FunctionRunning = false
end

game.ReplicatedStorage["Remote events."]["Attack resever."].OnServerEvent:Connect(function(Player, Target, PlayerName)
	if Character["Main Pet."].Value == false and PlayerName == Owner then
		print("Message reseved.")
		print(Target.PrimaryPart)
		Character:SetAttribute("Attacking", true)
		Character:SetAttribute("Following", false)
		print(FunctionRunning)
		Character:SetAttribute("Target", Target.Name)
			Attack(Target)
		
		Character:GetAttribute("Attacking", false)
		FollowPlayer()
	end
end)
1 Like

Your code does not check the attributes at all. Attacking and Following are both considered global variables not attributes, which is what you want.

local Attacking = Character:GetAttribute("Attacking")
local Following = Character:GetAttribute("Following")
local Damage = Character:GetAttribute("Damage")
local Owner = Character:GetAttribute("Owner")

This is a part of the start of the script, They get updated in a while loop every frame.

In that case the problem is likely something like this:

  1. Following is set to false via the RemoteEvent
  2. the Attack function is still sleeping
  3. Following is set to true by the loop you mentioned
  4. You end up with this condition: until true or true or true or true
  5. Attack is now running twice

Because of how or works, your check to see if FunctionRunning is false is skipped.

Try something like this:

until FunctionRunning == false and (Attacking ~= true or Following == true or Target.Humanoid.Health == 0)

I Knew it, Attack is working twice.