Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how to make this mouse click script move multiple units (NPCs)?

this should work for what im trying to do, but it doesnt... it doesnt even move one (and yes, if anybody asks, this is in a localscript)

local player=game.Players.LocalPlayer --Get the player
local mouse=player:GetMouse() --Get the mouse


for i,v in pairs (game.Workspace.Units:GetChildren()) do

if v:IsA ("Model") then 

mouse.Button1Down:connect(function() --When the player clicks..
    v.Humanoid:MoveTo((mouse.Hit).p)
end)
end
end

the script below moves just one unit when 3 is summoned

local player=game.Players.LocalPlayer --Get the player
local mouse=player:GetMouse() --Get the mouse
local warrior=workspace:WaitForChild("Warrior")--Get the warrior

mouse.Button1Down:connect(function() --When the player clicks..
    warrior.Humanoid:MoveTo((mouse.Hit).p)
end)

i added workspace.Units to the first one for the getChildren() function

-- thanks in advance for anybody that helps

1 answer

✓ Accepted answer

You've put the functions and loops in the wrong order..

local player = game.Players.LocalPlayer --Get the player
local mouse = player:GetMouse() --Get the mouse

mouse.Button1Down:connect(function() --Hey, I should be at the start!
	for i,v in pairs (game.Workspace.Units:GetChildren()) do --Then me!
		if v:IsA ("Model") then --Don't forget me!
			v.Humanoid:MoveTo((mouse.Hit).p)
		end
	end
end)


3 comments

0

that works. thanks. is there a way to make them not go behind each other though?

1

Due to collisions, they won't be able to pass through each other, so I'm unsure of how to fix that. And also if this helped you, please remember to click the "Accept Answer" button underneath my name!

0

i did. and i mean like instead of behind each other, touching, they would be spaced a little

This discussion is preserved as it was. New answers, comments, and votes are closed.