I’m trying to make an NPC wander around
everything works fine, except the npc keeps moving in this particular direction no matter the case, it moves around as expected but keeps preferring this side

It’s supposed to be random but I don’t think it is
-- relevant parts
a1.onEnter:Connect(function(npc)
print("npc in")
task.wait(3)
inRegion = true
end)
a1.onLeave:Connect(function(npc)
print("npc out")
inRegion = false
controller:MoveTo(spawnOrigin)
end)
while task.wait(3 * math.random(0.5, 1.5)) do
local x = math.random(-50, 50)
local z = math.random(-50, 50)
if inRegion == true then
controller:MoveTo(Vector3.new(x, 0, z))
elseif inRegion == false then
controller:MoveTo(spawnOrigin)
end
end
1 Like
try this:
-- relevant parts
a1.onEnter:Connect(function(npc)
print("npc in")
task.wait(3)
inRegion = true
end)
a1.onLeave:Connect(function(npc)
print("npc out")
inRegion = false
controller:MoveTo(spawnOrigin)
end)
while task.wait(3 * math.random(0.5, 1.5)) do
math.randomseed(os.time()) -- generate a new randomseed
local x = math.random(-50, 50)
local z = math.random(-50, 50)
if inRegion == true then
controller:MoveTo(Vector3.new(x, 0, z))
elseif inRegion == false then
controller:MoveTo(spawnOrigin)
end
end
i added a math.randomseed in the hopes that changes it
1 Like
You didn’t put a point of reference when giving a random position. You wrote math.random(-50, 50)
for the X and Z coordinate axis, which means it will go to the position (0, 0, 0) and to whatever the randomized axis positions are.
If you’re trying to make the NPC move around in a certain area, you need to add a reference point.
local x = workspace.Area.Position.X + random.new(-50, 50)
local z = workspace.Area.Position.Z + random.new(-50, 50)
...
if inRegion == true then
controller:MoveTo(Vector3.new(x, 0, z))
elseif inRegion == false then
controller:MoveTo(spawnOrigin)