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

Why won't this NPC follow me or attack me after I load it in from Replicated Storage?

Hi, this is an NPC that attacks you when you get near it. It works fine when I leave it in Workspace but once I put it in a map that I load in from Replicated Storage it stops working.

function findNearestPlayer(Position)
	wait(0.3)
 	local List = game.Workspace:children()
 	local Torso = nil
 	local Distance = 30
 	local Temp = nil
 	local Human = nil
 	local Temp2 = nil
 	for x = 1, #List do
  		Temp2 = List[x]
  		if (Temp2.className == "Model") and (Temp2 ~= script.Parent) then
   			Temp = Temp2:findFirstChild("HumanoidRootPart")
   			Human = Temp2:findFirstChild("Humanoid")
   			if (Temp ~= nil) and (Human ~= nil) and (Human.Health > 0) then
    			if (Temp.Position - Position).magnitude < Distance then
     				Torso = Temp
     				Distance = (Temp.Position - Position).magnitude
    			end
   			end
 		end
 	end
	return Torso
end




while true do
 	local target = findNearestPlayer(script.Parent.HumanoidRootPart.Position)
 	if target ~= nil then
  		script.Parent.Humanoid:MoveTo(target.Position, target)
		
	end
end

Follow ^

local rarm = script.Parent:FindFirstChild("Tool")
local humanoid  = script.Parent.Humanoid
local animationTrack = humanoid:LoadAnimation(script.Parent.Attack)

function dmg(hit)
    if hit.Parent ~= nil then
	animationTrack:Play()
        local hum = hit.Parent:findFirstChild("Humanoid")
        if hum ~= nil then
            hum.Health = hum.Health -100
        end
    end
end
 
rarm.Handle.Touched:connect(dmg)

Damage^

Please and Thanks

This is the script that loads the map from replicated storage (the NPC is in the map)

local status = game.ReplicatedStorage.EventsAndValues.Status
local maps = game.ReplicatedStorage.map:GetChildren()
local EndParts = {game.Workspace.TEndPart, game.Workspace.DEndPart, game.Workspace.SEndPart, game.Workspace.Neon1EndPart, game.Workspace.DessertEndPart, game.Workspace.ASHEndPart,
game.Workspace.BHEndPart}
local Players = game:GetService("Players")

while true do
	for i = 1,10 do
		status.Value = "Intermission "..10-i
		wait(1)
	end
	
	local rand = math.random(1, #maps)
	
	local map = maps[rand]:Clone()
	map.Parent = workspace
	
	status.Value = "We'll Be Playing "..map.Name
	wait(4)
	local players = game.Players:GetChildren()
	for i = 1, #players do
		if players[i].Character ~= nil then
			local spawnLocation = math.random(1,#workspace.Teleports:GetChildren())
			players[i].Character:MoveTo(workspace.Teleports:GetChildren()[spawnLocation].Position)
			players[i].Character.Parent = workspace.InGame
		end
	end
	
	local roundLength = 60
	local canWin = true
	local Debounce = false
	local ReadySetGo = 5
	
	if map:FindFirstChild("Obby") then
	for _,v in pairs(EndParts) do
		v.Touched:Connect(function(hit)
			if not Debounce then 
				Debounce = true
			canWin = false
			if hit.Parent:FindFirstChild("Humanoid") then
				status.Value = hit.Parent.Name.." Has Won!"
				local humanoid = hit.Parent:FindFirstChild("Humanoid")
		wait(5)
		Debounce = false
		
					end
				end
			end)
		end
end
	
	
	    repeat
		roundLength = roundLength -1
		status.Value = "Time Left: "..roundLength
		wait(1)
	until roundLength == 0 or canWin == false or #workspace.InGame:GetChildren() == 0
	
	
	
	wait(15)
	map:Destroy()
	
end



6 comments

0

Please show how you load it from replicated storage as well.

0

Done @Exanut

0

Havn't read anything but are you doing it on the server?

0

no they're arent any remote events or functions

0

Are you loading the NPC from a localscript?

0

You mean the follow script? @Fake

1 answer

the follow one is easy to fix. just put

local parentHumanoid = script.Parent:FindFirstChild("Humanoid")
if parentHumanoid then
	parentHumanoid:MoveTo(Temp.Position)
end

but im not good on attacking npcs

2 comments

0

dosent work

0

im trash at this ok im sorry ;(

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