Npc keeps going past a certain position!

I’m making a trying to make a fighting game as one of my projects, and I am at the point where I need to script the npc ai’s movement and I put invisible wall so the npc doesn’t leave the fight ground, the problem is I’m trying to make the noob not go past a certain position like Vector3.new(12,0,0) but the npc is always passing it by just a little bit so instead of it’s max position on the x axis being 12 it’s 12.542357 so that means it can touch the invisible wall and knock the noob out of the correct position on the z axis.

The video - YouTube

It’s been a week trying to figure this out
Here’s the script

local noob = game.Workspace:WaitForChild(“Dummy”)
local hrp = noob:WaitForChild("HumanoidRootPart")
local hum = noob:WaitForChild("Humanoid")
local botPressed = script.Parent.BotPressed
local runservice = game:GetService("RunService")

local move = coroutine.create(function()
	while task.wait() do 
		botPressed.Changed:Wait()
		if botPressed.Value == "LeftButton" then
			while task.wait() do
				if hrp.Position.X <= -9.5 then
					print("noob to the left")
					print(hrp.Position.X)
					break
				else
					if botPressed.Value == "Lifted" then
						print("Lifted")
						break hum:Move(Vector3.new(-0.5,0,0))
                                end
					print("moving")
				end
			end
		end

		if botPressed.Value == "RightButton" then
			while task.wait() do
				if hrp.Position.X >= 12.5 then
					print("noob to the Right")
					print(hrp.Position.X)
					break
				else
					if botPressed.Value == "Lifted" then
						print("Lifted")
  					        break                                hum:Move(Vector3.new(0.5,0,0))
					end
					print("moving")
				end
			end
		end
	end
end)	

coroutine.resume(move)

while task.wait(1) do
	local randomNum = math.random(3,10)
	botPressed.Value = "LeftButton"
	task.wait(randomNum)
	botPressed.Value = "Lifted"
	task.wait(1)
	local randomNum = math.random(3,10)
	botPressed.Value = "RightButton"
	task.wait(randomNum)
	botPressed.Value = "Lifted"
end```

This happens because you are moving the humanoid 0.5 and only after that in the next cycle of the while task.wait() do loop you check if it’s past 12.5, instead when you’re checking if it’s too far, do 12.5 - offset, which in this case is 0.5 because that’s how much it moves. That way you’ll be sure it wont get past 12.5 in the future. So all you have to do is change the if else statement so that it checks for 12.5 - 0.5 (12) and also you want to do the same kind of thing for the other if else statement where you don’t want it to go past -9.5

Edit, you can also check if the humanoidrootpart position + move_offset >= 12.5. That should give the same result

It works, it still hits the invisible walls but I’ll just have to move them further away

1 Like

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