#Invalid Argument To Random (Interval is Empty)

6 messages · Page 1 of 1 (latest)

spring rune
#

tried to make my npc ai wander around to waypoints

here was this script:

local function wander()
local waypoints = workspace:WaitForChild("Waypoints"):GetChildren()
local randomNum = math.random(1, #waypoints)
hum:MoveTo(waypoints[randomNum].Position)
end

while task.wait(0.2) do
wander()
end

everytime i run, the npc takes the first step and then it breaks and giving me this error

charred lark
#

Here is an example of a script that will make an NPC move to a series of waypoints in order:

#
local currentWaypoint = 1 -- start at the first waypoint

function moveToNextWaypoint()
  local character = game.Workspace.NPC -- the NPC character
  local destination = waypoints[currentWaypoint].Position -- the position of the current waypoint
  
  -- move the NPC to the current waypoint
  character:MoveTo(destination)
  
  -- increment the current waypoint index
  currentWaypoint = currentWaypoint + 1
  
  -- if we have reached the final waypoint, start over at the first waypoint
  if currentWaypoint > #waypoints then
    currentWaypoint = 1
  end
end

-- call the function to start the NPC moving
moveToNextWaypoint()```
severe mountain
#

You're dynamically getting waypoints, and this may be 0 waypoints, or anything less than 1 (maybe even equal to 1) so this error is thrown. Your second argument passed to math.random() must be greater than (I'm not sure if it can be equal to or not, maybe idk..) the first. Print out #waypoints right before you call to math.random() to see what it is.

normal daggerBOT
#

studio** You are now Level 7! **studio