Randomizing end position of Humanoid:MoveTo

  1. What do you want to achieve? Keep it simple and clear!
    So basically i’m need to randomly spawn in “Part” ducks and they must randomly go to the “Base”(It’s not base jk, just this easy to explain with it)

  2. What is the issue? Include screenshots / videos if possible!
    Currently all wave ducks goes to same barricade position.
    image

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? I tryed to use for loops and GetChildrens & Descendants, but nothing helped

My code:

Main(Script) One:

local ServerStorage = game:GetService("ServerStorage")
local Bindables = ServerStorage:WaitForChild("Bindables")
local GameOverEvent = Bindables:WaitForChild("GameOver")

local mob = require(script.Mob)

local gameOver = false



GameOverEvent.Event:Connect(function()
	gameOver = true
end)

for wave=1, 10 do
	print("WAVE STARTING:", wave)
	if wave <=	 2 then
		mob.Spawn("StandartDuck", 3 * wave)
		mob.Spawn("StandartDuck", 3 * wave)
		if wave <= 4 then
			mob.Spawn("StandartDuck", 3 * wave)
			mob.Spawn("StandartDuck", 3 * wave)
		end
	elseif wave == 10 then
		mob.Spawn("StandartDuck", 25)
		mob.Spawn("StandartDuck", 25)
	end
	
	repeat
		task.wait(1)
	until #workspace.DuckFolder:GetChildren() == 0 or gameOver
	
	print("WAVE ENDED")
task.wait(3)	
end

DuckSpawnScript(Module one)

local PhysicsService = game:GetService("PhysicsService")
local ServerStorage = game:GetService("ServerStorage")
local Bindables = ServerStorage:WaitForChild("Bindables")

local mob = {}
function mob.Move(mob, map)
	local Humanoid = mob:WaitForChild("Humanoid")
	local BarricadePos = game:GetService("Workspace").EventBarricade
	for i, barricadePos in ipairs(BarricadePos:GetChildren()) do
		Humanoid:MoveTo(barricadePos.Union.Position)
		Humanoid.MoveToFinished:Wait()
	end
	
end

function mob.Spawn(name, quantity, map)
	local mobExists = ServerStorage.Ducks:FindFirstChild(name)
	
	if mobExists then
		for i=1, quantity do
			task.wait(0.5)
			local NewMob = mobExists:Clone()
			NewMob.HumanoidRootPart.CFrame = game.Workspace.SpawnPos.CFrame
			NewMob.Parent = game.Workspace.DuckFolder
			NewMob.HumanoidRootPart:SetNetworkOwner(nil)
			
			for i, object in ipairs(NewMob:GetDescendants()) do

				if object:IsA("BasePart") then
					PhysicsService:SetPartCollisionGroup(object, "Duck")
				end
			end
			
			NewMob.Humanoid.Died:Connect(function()
				task.wait(0.5)
				NewMob:Destroy()
			end)
			
			coroutine.wrap(mob.Move)(NewMob, map)
		end
		
		
	else
	warn("Mob doesn't exist:", name)	
	end
end

return mob

1 Like

Could you explain the issue you’re having in more detail? Are there multiple set positions available that you need the ducks to go to? Or alternatively do you just want them to go to any random position on the map? Either way, from reading the information given I think you should just use math.random (math) to create a random number within either the given area, or between 1 and the number of set positions you have. You could then either directly move the duck to that position or with a number of set positions in a table you could index the set position with the random number.

Ok so i have a model called Event Barricade which includes every barricade placed and in barricade there is a tons of other models and one union, so i’m making for loop get this unions and randomize humanoid moveto:, but this makes entire wave of duck go to same barricade.That’s would be easier to use model pos, but i can’t get OriginPos from script

image – here is an explorer picture. Every duck not depends on barricade goes to this union

Is this what you’re referring to?

In which case the Humanoid will always be moved to the final barricade in the GetChildren() table.

If you want it to be randomised I would just do what I suggested, where you have a table of Barricade positions which you can then index from randomly.

Ughh yes this is what i reference to, but i don’t really understand you(sorry i didn’t work a lot with tables)

Imagine you have a table of positions:

local tab = [1,2,3,4,5]

You could use math.random to find a random index for that table:

local rand_index = math.random(5)

local rand_position = tab[rand_index]

You could then use your MoveTo() logic.

function mob.Move(mob, map)
	local Humanoid = mob:WaitForChild("Humanoid")
	local BarricadePos = game:GetService("Workspace").EventBarricade
	for i, barricadePos in ipairs(BarricadePos:GetChildren()) do
		local BarrTable = barricadePos
		local RNG_Index = math.random(1,BarrTable)
		local RNG_Pos = BarrTable[RNG_Index]
		Humanoid:MoveTo(RNG_Pos.Position)
		Humanoid.MoveToFinished:Wait()
	end
end

No. First of all, you don’t need the for loop if you’re using math.random. Secondly, you need to find the length of BarrTable (the number of barricades in the map) - you’re choosing a random number between 1 and the total number of Barricades available. So it should be more like:

local RNG_Index = math.random(1,#BarrTable)
local RNG_Pos = BarrTable[RNG_Index]
Humanoid:MoveTo(RNG_Pos.Position)
Humanoid.MoveToFinished:Wait()

edit: looking at the hierarchy you’re using it might be that you should use EventBarricade as BarrTable, then choose a random barricade child of EventBarricade, then choose a child position part to move to. At the moment you’re just moving to different part of the same barricade.

	local Humanoid = mob:WaitForChild("Humanoid")
	local BarricadePos = game:GetService("Workspace").EventBarricade
		local RNG_Index = math.random(1,#BarricadePos)
		local RNG_Pos = BarricadePos[RNG_Index]
		Humanoid:MoveTo(RNG_Pos.Position)
		Humanoid.MoveToFinished:Wait()
	end

Yes, you’re going to need to make it into a table of objects first. Objects and instances cannot be referenced as tables in Roblox luau. You could do something like this:

local tab = EventBarricade:GetChildren()
local RNG_Index = math.random(1,#tab)
local RNG_Pos = tab[RNG_Index]

Obviously the position now refers to a barricade model so you’ll also need to reference a part with a position in that model.