Random Waypoints

hello ive been trying to do a function that automatically choose a waypoints that is not being chosen before and make an npc walk there, however i dont know how to make it so that it would not repeat a used waypoint.
image
image

local unit = script.Parent
local waypoints = workspace.Map.Control_Centre:GetChildren()
ReplicatedStorage = game:GetService("ReplicatedStorage")
NewDefender = ReplicatedStorage.Events:WaitForChild("NewDefender")

local function SpawnNewDefender()
	local notused = {}
	for points in pairs(notused) do
		notused[points] = nil
	end
	for waypoint in pairs(waypoints) do
		if waypoints[waypoint].Used.Value == false then
			table.insert(notused, waypoint)
		end
	end
	local gowaypoint = math.random(1,#notused)
	print(waypoints[gowaypoint])
	if waypoints[gowaypoint].Used.Value == false then
		unit.Humanoid:MoveTo(waypoints[gowaypoint].Position)
		unit.Humanoid.MoveToFinished:Wait()
		unit.Humanoid:MoveTo(waypoints[gowaypoint].Stand.Position)
		waypoints[gowaypoint].Used.Value = true
	end
end

NewDefender.Event:Connect(SpawnNewDefender)
local function getWaypoint()
  for _, v in next, waypoints:GetChildren() do
    if v.Used.Value then continue end
    return v
  end
end

I think that’s all you need

1 Like