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 NPCs walk by themselves?

May I ask how could I make NPCs walk by themselves? I have seen Roblox Forums but it does not help.

2 comments

0

Also, could you mark this as an answer please? I spent ages on it :D

0

Thank you for spending so much time. I really appreciate it!

1 answer

✓ Accepted answer

[Before you start reading, Here's the model if you're too confused: https://web.roblox.com/library/5001846573/Walking-NPC Also, I took ages on this, please mark it as an asnwer PLZ!]

Step 1: Make a group named something like "Walking NPC"

Step 2: Insert 5 parts inside it, or as many points you would like the NPC to walk to. Then, insert a dummy into the model. Put whatever you like on it to make it look cool.

Step 3: Name the parts you inserted in, to: PointA, PointB, PointC, ect.

Step 4: Insert 2 scripts into the dummy, and name them "Animate" and "Walk"

Step 5: Insert this into the animate script:



function waitForChild(parent, childName)
	while true do
		local child = parent:findFirstChild(childName)
		if child then
			return child
		end
		parent.ChildAdded:wait()
	end
end





local Figure = script.Parent
local Torso = waitForChild(Figure, "Torso")
local RightShoulder = waitForChild(Torso, "Right Shoulder")
local LeftShoulder = waitForChild(Torso, "Left Shoulder")
local RightHip = waitForChild(Torso, "Right Hip")
local LeftHip = waitForChild(Torso, "Left Hip")
local Neck = waitForChild(Torso, "Neck")
local Humanoid = waitForChild(Figure, "Humanoid")
local pose = "Standing"

local toolAnim = "None"
local toolAnimTime = 0

local isSeated = false



function onRunning(speed)
	if isSeated then return end

	if speed>0 then
		pose = "Running"
	else
		pose = "Standing"
	end
end

function onDied()
	pose = "Dead"
end

function onJumping()
	isSeated = false
	pose = "Jumping"
end

function onClimbing()
	pose = "Climbing"
end

function onGettingUp()
	pose = "GettingUp"
end

function onFreeFall()
	pose = "FreeFall"
end

function onFallingDown()
	pose = "FallingDown"
end

function onSeated()
	isSeated = true
	pose = "Seated"
	print("Seated")
end



function moveJump()
	RightShoulder.MaxVelocity = 0.5
	LeftShoulder.MaxVelocity = 0.5
	RightShoulder.DesiredAngle = 3.14
	LeftShoulder.DesiredAngle = -3.14
	RightHip.DesiredAngle = 0
	LeftHip.DesiredAngle = 0
end

function moveFreeFall()
	RightShoulder.MaxVelocity = 0.5
	LeftShoulder.MaxVelocity = 0.5
	RightShoulder.DesiredAngle = 1
	LeftShoulder.DesiredAngle = -1
	RightHip.DesiredAngle = 0
	LeftHip.DesiredAngle = 0
end


function moveClimb()
	RightShoulder.MaxVelocity = 0.5
	LeftShoulder.MaxVelocity = 0.5
	RightShoulder.DesiredAngle = -3.14
	LeftShoulder.DesiredAngle = 3.14
	RightHip.DesiredAngle = 0
	LeftHip.DesiredAngle = 0
end

function moveSit()
	print("Move Sit")
	RightShoulder.MaxVelocity = 0.15
	LeftShoulder.MaxVelocity = 0.15
	RightShoulder.DesiredAngle = 3.14 /2
	LeftShoulder.DesiredAngle = -3.14 /2
	RightHip.DesiredAngle = 3.14 /2
	LeftHip.DesiredAngle = -3.14 /2
end

function getTool()
	
	kidTable = Figure:children()
	if (kidTable ~= nil) then
		numKids = #kidTable
		for i=1,numKids do
			if (kidTable[i].className == "Tool") then return kidTable[i] end
		end
	end
	
	return nil
end

function getToolAnim(tool)

	c = tool:children()
	for i=1,#c do
		if (c[i].Name == "toolanim" and c[i].className == "StringValue") then
			return c[i]
		end
	end
	return nil
end

function animateTool()
	
	if (toolAnim == "None") then
		RightShoulder.DesiredAngle = 1.57
		return
	end

	if (toolAnim == "Slash") then
		RightShoulder.MaxVelocity = 0.5
		RightShoulder.DesiredAngle = 0
		return
	end

	if (toolAnim == "Lunge") then
		RightShoulder.MaxVelocity = 0.5
		LeftShoulder.MaxVelocity = 0.5
		RightHip.MaxVelocity = 0.5
		LeftHip.MaxVelocity = 0.5
		RightShoulder.DesiredAngle = 1.57
		LeftShoulder.DesiredAngle = 1.0
		RightHip.DesiredAngle = 1.57
		LeftHip.DesiredAngle = 1.0
		return
	end
end

function move(time)
	local amplitude
	local frequency

	if (pose == "Jumping") then
		moveJump()
		return
	end

	if (pose == "FreeFall") then
		moveFreeFall()
		return
	end

	if (pose == "Climbing") then
		moveClimb()
		return
	end

	if (pose == "Seated") then
		moveSit()
		return
	end


	RightShoulder.MaxVelocity = 0.15
	LeftShoulder.MaxVelocity = 0.15
	if (pose == "Running") then
		amplitude = 1
		frequency = 9
	else
		amplitude = 0.1
		frequency = 1
	end

	desiredAngle = amplitude * math.sin(time*frequency)

	RightShoulder.DesiredAngle = desiredAngle
	LeftShoulder.DesiredAngle = desiredAngle
	RightHip.DesiredAngle = -desiredAngle
	LeftHip.DesiredAngle = -desiredAngle


	local tool = getTool()

	if tool ~= nil then
	
		animStringValueObject = getToolAnim(tool)

		if animStringValueObject ~= nil then
			toolAnim = animStringValueObject.Value
		
			animStringValueObject.Parent = nil
			toolAnimTime = time + .3
		end

		if time > toolAnimTime then
			toolAnimTime = 0
			toolAnim = "None"
		end

		animateTool()

		
	else
		toolAnim = "None"
		toolAnimTime = 0
	end
end




Humanoid.Died:connect(onDied)
Humanoid.Running:connect(onRunning)
Humanoid.Jumping:connect(onJumping)
Humanoid.Climbing:connect(onClimbing)
Humanoid.GettingUp:connect(onGettingUp)
Humanoid.FreeFalling:connect(onFreeFall)
Humanoid.FallingDown:connect(onFallingDown)
Humanoid.Seated:connect(onSeated)



local nextTime = 0
local runService = game:service("RunService");

while Figure.Parent~=nil do
	time = runService.Stepped:wait()
	if time > nextTime then
		move(time)
		nextTime = time + 0.1
	end
end

Step 6: Insert this into the walk script:


model = script.Parent.Parent
hum = script.Parent.Humanoid
torso = script.Parent.Torso
while true do
	if model.PointA ~= nil then
		a = model.PointA
		hum:MoveTo(a.Position, a)
		repeat
			wait(0.1)
		until (a.Position - torso.Position).magnitude <= 5
	else
		print("No Point A.")
	end
wait(0.1)
	if model.PointB ~= nil then
		b = model.PointB
		hum:MoveTo(b.Position, b)
		repeat
			wait(0.1)
		until (b.Position - torso.Position).magnitude <= 5
	else
		print("No Point B.")
	end
wait(0.1)
	if model.PointC ~= nil then
		c = model.PointC
		hum:MoveTo(c.Position, c)
		repeat
			wait(0.1)
		until (c.Position - torso.Position).magnitude <= 5
	else
		print("No Point C.")
	end
wait(0.1)
	if model.PointD ~= nil then
		d = model.PointD
		hum:MoveTo(d.Position, d)
		repeat
			wait(0.1)
		until (d.Position - torso.Position).magnitude <= 5
	else
		print("No Point D.")
	end
wait(0.1)
	if model.PointE ~= nil then
		e = model.PointE
		hum:MoveTo(e.Position, e)
		repeat
			wait(0.1)
		until (e.Position - torso.Position).magnitude <= 5
	else
		print("No Point E.")
	end
wait(0.1)
end
	

Your done!

1 comment

0

Thanks a lot!

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