How do I spawn Models/npc near me and make them attack

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    As the title says I want to spawn a model next to me and make them attack everyone except for me. So I made a class system and in that class system there is a set of skills and movesets that goes to the player that picks that class and one of the classes has a skill that he summons some soldier that attacks everyone except for me

  2. What is the issue? Include screenshots / videos if possible!
    I don’t know how to teleport them and I don’t know how to make the npc attack everyone else but me.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried searching this up in forums and youtube but I cant find anything. For the teleport part I tried putting the model somewhere like in the serverstorage and then when I press Q I did something like

UIS.InputBegan:Connect(function(input,isTyping)
	if isTyping then 
		return
	elseif input.KeyCode == Enum.KeyCode.Q then
Model.Parent = game,workspace
Model.HumanoidRootPart.CFrame = Model.HumanoidRootPart.CFrame*CFrame.new(0,0,5)

I tried something like this for the teleport part but it doesn’t do anything.(I didn’t put the exact script I have because my script is a lot messier than this)

As for the npc attacking part I watched a tutorial about this but I can’t find any tutorials about making it not attack/follow me
But the script goes like

local RomanRoot = script.Parent.HumanoidRootPart
local RomanHumanoid = script.Parent.Humanoid


local function findenemy()
	local aggro = 500
	local target = nil
	for i,v in pairs(game.Workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local root = v:FindFirstChild("HumanoidRootPart")
		if human and root and v ~= script.Parent then
			if (RomanRoot.Position - root.Position).Magnitude < aggro then
				aggro = (RomanRoot.Position - root.Position).Magnitude
				target = root
			end
		end
	end
	return target
end

while wait(1) do
	local torso = findenemy()
	if torso then
		RomanHumanoid:MoveTo(torso.Position)
	end
end

Also I’m pretty new so I don’t know how to script and I probably only know like half the basics.

3 Likes

For the NPC spawning are you doing it in a local script, if so I would recommend using remote events in replicated storage(probably a better way though), that way you can spawn the npc server side so other players can see it as well, this could also help with the follow/attack script, where once the npc is spawned in you would have a value or something containing the player that fired the remote event or the name of the player, and have the npc check if whatever they are targeting is not the player

So I am using a local script but other players can’t see the model. How do I fix this?

That is because it is in a local script, because it’s in a local script it will only affect the client running the script, not the server, which is why I would suggest using remote events, so that you can trigger an event to let the server know what to do to the server, so other players can see it, this would have to be done in a script in serverscriptservice

So Im using remote events rn but now it’s not teleporting the soldiers to me. Do you know what the problem is?

local rp = game:GetService("ReplicatedStorage")
local remote = rp:WaitForChild("Teleport")
local player = game.Players:WaitForChild("HumanoidRootPart")

local function spawnsoldier()
	
	local ssoldier = rp:WaitForChild("RomanSoldier")
	local Soldier = ssoldier:Clone()
	Soldier.Parent = game.Workspace
	wait(0.4)
	Soldier.HumanoidRootPart.CFrame = player.CFrame
	
end

print("test1")

remote.OnServerEvent:Connect(function(player)
	spawnsoldier()
	print("test2")
end)
1 Like

Nevermind I fixed it. Thanks anyways

2 Likes