How do I create an NPC that copies the player's movements but delayed

I think you should make it a serverscript since the monster’s movement should be visible to all players

Remove the wait(5) before destroying, while testing I see that it just makes it slow and stupid. It will only do the :Destroy() after :MoveTo() is completed anyway

1 Like

forgot to explain this but it’s a chase sequence so i kind of have to set it locally

1 Like

it works, only one problem is that the npc gets stuck on a thin stair section, and i’m afraid it might fall into the void as well since the map does have a few jumps

local npc = game.Workspace.Rig
local interval = 0.5
while wait(interval) do
	if interval > 0 then
		interval -= 1
	else
		interval = 0.1
	end
	npc.Humanoid.WalkSpeed += 1
	if npc == nil then break end
	if npc.Humanoid.WalkSpeed < script.Parent.Humanoid.WalkSpeed then
		local part = Instance.new('Part')
		part.Size = Vector3.new(0.1,0.1,0.1)
		part.Position = script.Parent.HumanoidRootPart.Position
		npc.Humanoid:MoveTo(part.Position)
		part:Destroy()
	else
		npc.Humanoid:MoveTo(script.Parent.PrimaryPart.Position)
		script.Parent.PrimaryPart.Touched:Connect(function(hit)
			if hit == npc then
				script.Parent.Humanoid:TakeDamage(100)
			end
		end)
	end
end
1 Like

the code spawns blocks more frequently until the monster is faster than the player
then the monster targets the player directly rather than the block
during testing I see that it eventually catches up and touches the player
however the .Touched commnd here is broken. You’ll have to fix it

1 Like

i’ll change it so that the npc stays at a constant speed to keep it consistent

oh, i see what you’re trying to do but I want the npc’s speed to be consistent, i can make it kill on touch, all i want is for it to follow behind the player

Are you trying to create an enemy similar to the cosmic clones from Mario?

yeah, thats basically what i’m trying to do

My approach to this was to use run service, store the player’s cframe, and apply those cframes to the npc after a delay.
Since this doesn’t use :moveto() or any physics based method, I anchored every part of the Rig.

local runservice = game:GetService("RunService")
local npc = game.Workspace.Rig
local character = script.Parent

local enemydelay = 2

runservice.Heartbeat:Connect(function(dt)
	for index, child in pairs(npc:GetChildren()) do
		if child:IsA("BasePart") then
			local correspondingPart = character:FindFirstChild(child.Name)
			if correspondingPart then
				task.spawn(function() -- create separate threads so task.wait(enemydelay) doesn't yield everything
					local oldcframe = correspondingPart.CFrame
					task.wait(enemydelay)
					child.CFrame = oldcframe
				end)
			end
		end
	end
end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.