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

How can I modify the script so It follows other players but not you?

I've been wanting to make a zombie staff that summons a zombie that defends for you, I am trying to find Information but It just shows the opposite, I did manage to find one but I want It so that the NPC finds targets by Itself when being summoned

Problems with the script:

  1. The script follows everyone In the server currently
  2. The script (ServerScript) won't work when being cloned In ReplicaStorage

Script:

local NPC = script.Parent
local Pathfinding = game:GetService("PathfindingService")
local player

game.Players.PlayerAdded:Connect(function(client)
	player = client
end)

repeat wait() until player ~= nil

while wait() do
	local path = Pathfinding:CreatePath()
	
	path:ComputeAsync(NPC.Torso.Position, player.Character.PrimaryPart.Position)
	
	if path.Status == Enum.PathStatus.Success then
		
		local Positions = path:GetWaypoints()
		
		for i,v in pairs(Positions) do
			
			local position = Instance.new("Part")
			
			position.Parent = game.Workspace
			
			position.Size = Vector3.new(1,1,1)
			
			position.Position = v.Position
			
			position.Transparency = 1
			
			NPC.Humanoid:MoveTo(position.Position)
			
			NPC.Humanoid.MoveToFinished:Wait(2)
			
			position:Destroy()
			
		end
		
	end
end

1 answer

you would need a way to tell the zombies to go somewhere, using a find enemy torso function like this fixes the problem but it attacks you to prevent this add a simple check when getting the torso

if torso == thisplayerstorso then return end 
local Creator = script.Parent.Creator
function GetPlayerNames()
	local players = game:GetService('Players'):GetChildren()
	local name = nil
	for _, v in pairs(players) do
		if v:IsA'Player' then
			name = tostring(v.Name)
		end
	end
	return name
end
function GetTorso(part)
	local chars = game.Workspace:GetChildren()
	local torso = nil
	for _, v in pairs(chars) do
		if v:IsA'Model' and v ~= script.Parent and v.Name == GetPlayerNames() and not v.Name == Creator.Value then
			local charRoot = v:FindFirstChild'HumanoidRootPart'
			if (charRoot.Position - part).magnitude < SearchDistance then
				torso = charRoot
			end
		end
	end
	return torso
end
while wait() do
    local path = Pathfinding:CreatePath()

    path:ComputeAsync(NPC.Torso.Position, GetTorso(NPC.Torso.Position))

    if path.Status == Enum.PathStatus.Success then

        local Positions = path:GetWaypoints()

        for i,v in pairs(Positions) do

            local position = Instance.new("Part")

            position.Parent = game.Workspace

            position.Size = Vector3.new(1,1,1)

            position.Position = v.Position

            position.Transparency = 1

            NPC.Humanoid:MoveTo(position.Position)

            NPC.Humanoid.MoveToFinished:Wait(2)

            position:Destroy()

        end

    end
end

4 comments

0

Alright thanks! Just a question, where should I place the script?

0

in the zombie

0

I mean't the script Inside of the pathfinding

0

?

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