Reasons why the NPC flies away

every time I make the NPC jump using Humanoid:ChangeState(Enum.HumanoidStateType.Jumping), it goes flying, sometimes it jumps very high and other times it jumps up to the sky and continues… the only way to lower it is to change the current to server and downloading it manually, all the MoveTo are executed on the server, the jump power is at 60, it should be noted that there are high parts, but not as far as it goes up, it is a problem that occurs from time to time, if you need Well, I’ll pass the code on without a problem, but the variables are in Spanish and the code is very long, I don’t know what the problem is. I would appreciate the help, by the way someone help me for the first time in this forum

3 Likes

it makes me angry at the same time the simple fact that people look at my topic and nobody makes EVEN THE MOST MINIMAL CONTRIBUTION, not even an advice.

2 Likes

Whoa, hold up. People read this to see if they can help your topic. If they can’t then they shouldn’t be replying anyway.
We aren’t paid to work here, so if nobody answers your question in 15 minutes you need to be patient. I don’t expect to have answers for any of my questions here for a day or so.

This is the Scripting Support forum. Post your script here otherwise we can’t help you at all.
Remember to put 3 backticks (```) before and after your copy/pasted script so it formats properly and is easy to read.

1 Like

First thing that I think of, by reading your results, is that perhaps you are calling the jump repeatedly over and over.

Also, as @Scoffifly said, you should add some code, so we can see if we spot anything that might be wrong.

It can be frustrating when you just want your code to WORK, and it seems like you have to wait forever, before you get an answer, even if you get an answer. But thats just how it is, sometimes I get no reply, or sometimes I get junk replies, we all help the best we can, but its rarely instant.

1 Like
function module:NPC(Bot,Armaequipada)--CurrentTarget tiene que ser un character.
	local Ruta = PathfindingService:CreatePath()
	local Range = 50
	local Hecho = false
	Bots[Bot.Name] = {
		CurrentTarget = nil;
		ArmaEquipada = Armaequipada;
		Disparo = nil;
		Char = Bot;
		municion = 30;
	}
	local ArmaEquipada = Armaequipada
	local States = {
		Lento = 22;
		Normal = 50;
		Rapido = 65;
	}
	local DisparosAFallar = 0
	local function DisparoBot(Bot,CurrentTarget,arma)
		RayParamsBot.FilterDescendantsInstances = {Bot}
		local Result = math.random(1,3)
		Bots[Bot.Name].municion -= 1
		if Result == 2 and DisparosAFallar == 0  then
			local Disparo = workspace:Raycast(Bot[ArmaEquipada].Firepart.Position,CFrame.new(Bot[ArmaEquipada].Firepart.Position,CurrentTarget.Character.HumanoidRootPart.Position).LookVector * 500,RayParamsBot)
			if Disparo then
				if Disparo.Instance and Disparo.Instance.Parent:FindFirstChild("Humanoid") then
					local damage = math.random(DataOfWeaponsBots.Sicario[arma].mindano,DataOfWeaponsBots.Sicario[arma].dano)
					Disparo.Instance.Parent.Humanoid:TakeDamage(damage)
					DisparoR:FireAllClients("BotD",Bot[arma].Firepart:FindFirstChildOfClass("Sound"),Bot[arma].Firepart,Bot,Disparo.Instance.Parent,Disparo.Position,Disparo.Normal,Disparo.Instance,damage)
				else
					DisparoR:FireAllClients("Fallado",Bot[arma].Firepart:FindFirstChildOfClass("Sound"),Bot[arma].Firepart,Disparo.Position,Disparo.Normal,Disparo.Instance,Bot,arma)	
				end	
			else
				print("arrivaa")
			end
		elseif Result == 3 and DisparosAFallar == 0 then
			local Disparo = workspace:Raycast(Bot[ArmaEquipada].Firepart.Position,CFrame.new(Bot[ArmaEquipada].Firepart.Position,CurrentTarget.Character.HumanoidRootPart.Position).LookVector * 500,RayParamsBot)
			if Disparo then
				if Disparo.Instance and Disparo.Instance.Parent:FindFirstChild("Humanoid") then
					local damage = math.random(DataOfWeaponsBots.Sicario[arma].mindano,DataOfWeaponsBots.Sicario[arma].dano) + DataOfWeaponsBots.Sicario[arma].Extra
					Disparo.Instance.Parent.Humanoid:TakeDamage(damage)
					DisparoR:FireAllClients("BotD",Bot[arma].Firepart:FindFirstChildOfClass("Sound"),Bot[arma].Firepart,Bot,Disparo.Instance.Parent,Disparo.Position,Disparo.Normal,Disparo.Instance,damage)
				else
					DisparoR:FireAllClients("Fallado",Bot[arma].Firepart:FindFirstChildOfClass("Sound"),Bot[arma].Firepart,Disparo.Position,Disparo.Normal,Disparo.Instance,Bot,arma)	
				end	
			end
		else
			if DisparosAFallar > 0 then
				DisparosAFallar -= 1
			end
			local Disparo = workspace:Raycast(Bot.HumanoidRootPart.Position,CFrame.new(CurrentTarget.Character.HumanoidRootPart.Position - Bot.HumanoidRootPart.Position + Vector3.new(tick() * 0.2,tick() * 0.1,tick() * 0.4)).LookVector * 500,RayParamsBot)
			if Disparo then
				if Disparo.Instance and Disparo.Instance.Parent:FindFirstChild("Humanoid") then
					local damage = math.random(DataOfWeaponsBots.Sicario[arma].mindano,DataOfWeaponsBots.Sicario[arma].dano)
					Disparo.Instance.Parent.Humanoid:TakeDamage(damage)
					DisparoR:FireAllClients("BotD",Bot[arma].Firepart:FindFirstChildOfClass("Sound"),Bot[arma].Firepart,Bot,Disparo.Instance.Parent,Disparo.Position,Disparo.Normal,Disparo.Instance,damage)
				else
					DisparoR:FireAllClients("Fallado",Bot[arma].Firepart:FindFirstChildOfClass("Sound"),Bot[arma].Firepart,Disparo.Position,Disparo.Normal,Disparo.Instance,Bot,arma)	
				end	
			else 
			end

		end
	end
	local AgacharAnimation = Bot.Humanoid.Animator:LoadAnimation(Bot.Humanoid.Agachar)
	local AgacharAnimationM = Bot.Humanoid.Animator:LoadAnimation(Bot.Humanoid.AgacharM)
	local Parado = Bot.Humanoid.Animator:LoadAnimation(Bot.Humanoid.Parado)
	local Correr = Bot.Humanoid.Animator:LoadAnimation(Bot.Humanoid.Run) 
	local Deslizamiento = Bot.Humanoid.Animator:LoadAnimation(Bot.Humanoid.Deslizamiento)
	local Impactado = false
	while task.wait() do
		print(Bots[Bot.Name].CurrentTarget)
		if Bot.Humanoid:GetState() == Enum.HumanoidStateType.Dead then  return "Muerto"  end
		if Bots[Bot.Name].CurrentTarget and not Bots[Bot.Name].CurrentTarget:IsA("Part") then
			if Bots[Bot.Name].municion > 0 then
				local part
				if players[Bots[Bot.Name].CurrentTarget.Name].VelocidadBot == "Rapido" then
					part =  workspace:Raycast(Bot.Head.Position,CFrame.new(Bot.HumanoidRootPart.Position,Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.Position).LookVector * 500,RayParamsBot)
				elseif (Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.Position - Bot.HumanoidRootPart.Position).Magnitude < 20 then
					part =  workspace:Raycast(Bot.Head.Position,CFrame.new(Bot.HumanoidRootPart.Position,Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.Position).LookVector * 500,RayParamsBot)
				elseif players[Bots[Bot.Name].CurrentTarget.Name].VelocidadBot == "Normal" or players[Bots[Bot.Name].CurrentTarget.Name].VelocidadBot == "Lento" then
						part =  workspace:Raycast(Bot.Head.Position,CFrame.new(Bot.HumanoidRootPart.Position,Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.Position).LookVector * 500,RayParamsBot)		
				elseif  Bot.HumanoidRootPart.CFrame:ToObjectSpace(Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.CFrame).Z < 0 then
					part =  workspace:Raycast(Bot.Head.Position,CFrame.new(Bot.HumanoidRootPart.Position,Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.Position).LookVector * 500,RayParamsBot)		
				else
					Bots[Bot.Name].CurrentTarget = nil
				end
				if part then
					if part.Instance.Parent == Bots[Bot.Name].CurrentTarget.Character then
						while task.wait() do
							task.wait(DataOfWeaponsBots.Sicario[ArmaEquipada].Time)
							if Bots[Bot.Name].Disparo ~= nil and Bots[Bot.Name].Disparo ~= Bots[Bot.Name].CurrentTarget then break end--hacer movimiento
							DisparoBot(Bot,Bots[Bot.Name].CurrentTarget,Armaequipada)
							if players[Bots[Bot.Name].CurrentTarget.Name].VelocidadBot == "Rapido" then
								part =  workspace:Raycast(Bot.Head.Position,CFrame.new(Bot.HumanoidRootPart.Position,Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.Position).LookVector * 500,RayParamsBot)
							elseif (Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.Position - Bot.HumanoidRootPart.Position).Magnitude < 20 then
								part =  workspace:Raycast(Bot.Head.Position,CFrame.new(Bot.HumanoidRootPart.Position,Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.Position).LookVector * 500,RayParamsBot)
							elseif players[Bots[Bot.Name].CurrentTarget.Name].VelocidadBot == "Normal" or players[Bots[Bot.Name].CurrentTarget.Name].VelocidadBot == "Lento" then
								part =  workspace:Raycast(Bot.Head.Position,CFrame.new(Bot.Head.Position,Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.Position).LookVector * 500,RayParamsBot)		
							elseif  Bot.HumanoidRootPart.CFrame:ToObjectSpace(Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.CFrame).Z < 0 then
								part =  workspace:Raycast(Bot.Head.Position,CFrame.new(Bot.HumanoidRootPart.Position,Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.Position).LookVector * 500,RayParamsBot)		
							else
								local Desicion = math.random(2)
								if Desicion == 2 then
									part = game.Workspace.Mapa.Part
								else
									part = Bots[Bot.Name].CurrentTarget.Character.Head
								end
							end
							if Bots[Bot.Name].CurrentTarget.Character.Humanoid:GetState() == Enum.HumanoidStateType.Dead then return "Continue" end
							if part and part.Instance.Parent ~= Bots[Bot.Name].CurrentTarget.Character then break end
							if Bots[Bot.Name].Disparo ~= nil  and Bots[Bot.Name].Disparo ~= Bots[Bot.Name].CurrentTarget  then break end
						end
					else
						--//aca hay que hacer que el torso rote hacia el char
						local PositionT = Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.Position 
						Ruta:ComputeAsync(Bot.HumanoidRootPart.Position,Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.Position)
						if Ruta.Status == Enum.PathStatus.NoPath then--Quitar
							Ruta:ComputeAsync(Bot.HumanoidRootPart.Position,Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.Position)
						end
						for i , Punto in pairs(Ruta:GetWaypoints()) do
							if  (Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.Position -Bot.HumanoidRootPart.Position).Magnitude < 5 then break end--hacer q se deslize
							local ValueO = 5
							repeat 
								if ValueO == 0 then break end
								task.wait()
								if Punto.Action == Enum.PathWaypointAction.Jump then
									Bot.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
									task.wait(0.2)
									AgacharAnimation:Play()
									Bot.HumanoidRootPart.CanQuery = false
									Bot.HumanoidRootPart.CanCollide = false
									task.delay(1,function()
										AgacharAnimation:Stop()
										Bot.HumanoidRootPart.CanCollide = true
										Bot.HumanoidRootPart.CanQuery = true
									end)
								end
								Bot.Humanoid:MoveTo(Punto.Position)
								ValueO -= 1
							until (Bot.HumanoidRootPart.Position - Vector3.new(Punto.Position.X,Bot.HumanoidRootPart.Position.Y,Punto.Position.Z)).Magnitude <= 1
							if ValueO == 0 then break end
							if Bots[Bot.Name].CurrentTarget.Character.Humanoid:GetState() == Enum.HumanoidStateType.Dead then Bots[Bot.Name].CurrentTarget = nil break end
							Tabla = PlayersService:GetPlayers()
							part =  workspace:Raycast(Bot.HumanoidRootPart.Position,CFrame.new(Bot.HumanoidRootPart.Position,Bots[Bot.Name].CurrentTarget.Character.HumanoidRootPart.Position).LookVector * 500,RayParamsBot)
							if part.Instance.Parent == Bots[Bot.Name].CurrentTarget.Character  then break end
							if Bots[Bot.Name].Disparo ~= nil and Bots[Bot.Name].Disparo ~= Bots[Bot.Name].CurrentTarget then  break end--hacer que ruede  
							Bot.Humanoid.MoveToFinished:Wait()
							--//hay que hacer q tambien verifique que no hayan bots cerca ni a la vista
						end	
					end
				end
			else
				task.wait(1.1)
				--reproduce sonido de recarga
				Bots[Bot.Name].municion = 30
			end
		else
			local desicion = math.random(5)
			local part
			if desicion == 2 then
				Parado:Play()
				local Value = tick()
				while task.wait() do
					if Bots[Bot.Name].Disparo ~= nil then
						Bots[Bot.Name].CurrentTarget = Bots[Bot.Name].Disparo 
						Bots[Bot.Name].Disparo  = nil
						--hacer que mueva el torso dentro dle bucle
						break
					elseif tick() - Value > 1 then
						break
					end
				end
			else
				Bots[Bot.Name].CurrentTarget = TablaConObjetos[math.random(#TablaConObjetos)]
				Ruta:ComputeAsync(Bot.HumanoidRootPart.Position,Bots[Bot.Name].CurrentTarget.Position)
				if Ruta.Status == Enum.PathStatus.NoPath then--Quitar
					Ruta:ComputeAsync(Bot.HumanoidRootPart.Position,Bots[Bot.Name].CurrentTarget.Position)
				end
				Correr:Play()
				for i , Punto in pairs(Ruta:GetWaypoints()) do
					if Bots[Bot.Name].Disparo ~= nil or (Bots[Bot.Name].CurrentTarget.Position -Bot.HumanoidRootPart.Position).Magnitude < 10 then  break end
					if Punto.Action == Enum.PathWaypointAction.Jump then
						Bot.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
						task.wait(0.2)
						AgacharAnimationM:Play()
						Bot.HumanoidRootPart.CanQuery = false
						Bot.HumanoidRootPart.CanCollide = false
						task.delay(1,function()
							AgacharAnimation:Stop()
							Bot.HumanoidRootPart.CanCollide = true
							Bot.HumanoidRootPart.CanQuery = true
						end)
					end
					local Pos = Bot.HumanoidRootPart.Position
					local Value0 = 5
					repeat 
						if Value0 == 0 then break end
						if Punto.Action == Enum.PathWaypointAction.Jump then
							Bot.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
							task.wait(0.2)
							AgacharAnimation:Play()
							Bot.HumanoidRootPart.CanQuery = false
							Bot.HumanoidRootPart.CanCollide = false
							task.delay(1,function()
								AgacharAnimation:Stop()
								Bot.HumanoidRootPart.CanCollide = true
								Bot.HumanoidRootPart.CanQuery = true
							end)
						end
						task.wait()
						Bot.Humanoid:MoveTo(Punto.Position)
					until (Bot.HumanoidRootPart.Position - Vector3.new(Punto.Position.X,Bot.HumanoidRootPart.Position.Y,Punto.Position.Z)).Magnitude <= 1
					if Value0 == 0 then Correr:Stop() break end
					Tabla = PlayersService:GetPlayers()
					for i , Player in pairs(Tabla) do
						if (Player.Character.HumanoidRootPart.Position - Bot.HumanoidRootPart.Position).Magnitude < States[players[Player.Name].VelocidadBot]  then
							Bots[Bot.Name].CurrentTarget = Player
							break 
						elseif Bot.HumanoidRootPart.CFrame:ToObjectSpace(Player.Character.HumanoidRootPart.CFrame).Z < 0 then
							part =  workspace:Raycast(Bot.Head.Position,CFrame.new(Bot.HumanoidRootPart.Position,Player.Character.HumanoidRootPart.Position).LookVector * 500,RayParamsBot)
							if part.Instance.Parent == Player.Character then Bots[Bot.Name].CurrentTarget = Player break end
						end
					end
					Bot.Humanoid.MoveToFinished:Wait()
					if not Bots[Bot.Name].CurrentTarget:IsA("Part") then break end
					--//hay que hacer q tambien verifique que no hayan bots cerca ni a la vista
				end
			end
		end
	end
endrofiles in studio testing

@Scottifly

I think you’re right, in one part there’s a Repeat until if the moveto is not executed and in there the NPC jumps, until there is no movement to the point it will keep jumping, I think that’s the problem.
edit don’t you have any solution?

I really never use pathfinding service, and all the little nested conditions you have to do to keep it moving to the correct waypoint, so I am not the best a solving you code issue with this. If you see where the jumping could be repeating, and you think that might be the issue, put a print statement where its jumping, and look in output to see how many times that print is being outputted, to see if that is truly the problem.

1 Like

thanks, while wait for @Scottifly what posibility he help me

1 Like

Hey, I saw that you were able to see my publication but the question is, don’t you have another suggestion?

I don’t. The script isn’t something I could fix.
If @SelDraken solved the problem then that’s great.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.