Can anyone give me a clue as to why for i, v in pairs is only focusing on one player instead of going to random ones since im trying to make it focus on random ones for right now.
print('luther')
local npc = script.Parent
local botHumanoid = npc:FindFirstChild("Humanoid")
local botRootPart = npc:FindFirstChild("HumanoidRootPart")
--humanoid:MoveTo(workspace:WaitForChild("Orblordr"))
for i, v in pairs(workspace:GetChildren()) do
if v:IsA("Model") then
print("nuh")
print(v)
local rig = v
local humanoid = rig:FindFirstChild("Humanoid")
local rootPart = rig:FindFirstChild("HumanoidRootPart")
local delayTime = 0.1
while task.wait(delayTime) do
local direction = (rootPart.Position - botRootPart.Position)
local rayCastParams = RaycastParams.new()
rayCastParams.FilterType = Enum.RaycastFilterType.Exclude
rayCastParams.FilterDescendantsInstances = {workspace.Baseplate, npc}
local rayCastResult = workspace:Raycast(botRootPart.Position, direction, rayCastParams)
if rayCastResult and humanoid and direction.Magnitude <= 50 then
botHumanoid:MoveTo(rootPart.Position)
else
delayTime = 1.2
botHumanoid:MoveTo(botRootPart.Position + Vector3.new(math.random(-5,5),0, math.random(-5,5)))
end
end
end
end
Whenever you use workspace:GetChildren(), it returns the same table with the npcs in the same order. Instead, you could make a list that contains all the NPC’s/Models and then use math.random to make it pick an NPC randomly.
Here’s what you could do:
local listOfNpcs = {}
for i, v in pairs(workspace:GetChildren()) do
if v:IsA("Model") then
table.insert(listOfNpcs,v)
end
end
local randomNPC = listOfNpcs[math.random(0,#listOfNpcs)]
-- rest of your code here
But the problem is that its not returnning all of the npcs its only picking one specfic npc and whenever I print out the npcs in the loop it doesnt print all models.
Perhaps it’s due to the while loop? A loop in the first model in the for loop might be preventing the for loop from going to the next model. Furthermore, the loop doesn’t seem to stop or break.
If you want the while loop to occur for every model, I suggest using task.spawn() or task.defer().
it was the while loop cuz after I changed it to a couroutine or task.spawn() it worked but now it doesn’t work like intended with the while loop because it just moves like erratically.
Ok now I have another problem where every single time I add an inital clone it doesn’t track the npc immediately.
Also I commented out the table.insert as for right now since I don’t think I’ve reached that part of my script where I need it just for right now at least.
local npc = script.Parent
local botHumanoid = npc:FindFirstChild("Humanoid")
local botRootPart = npc:FindFirstChild("HumanoidRootPart")
local replicatedStorage = game:GetService("ReplicatedStorage")
local listOfModels = {}
replicatedStorage.RemoteEvents.Cloned.Event:Connect(function(character)
for i, v in pairs(workspace:GetChildren()) do
if v:IsA("Model") then
print(v)
--table.insert(listOfModels, v)
--print(listOfModels)
local rig = v
local humanoid = rig:FindFirstChild("Humanoid")
local rootPart = rig:FindFirstChild("HumanoidRootPart")
local delayTime = 0.1
local track = coroutine.create(function()
while task.wait(delayTime) do
local direction = (rootPart.Position - botRootPart.Position)
local rayCastParams = RaycastParams.new()
rayCastParams.FilterType = Enum.RaycastFilterType.Exclude
rayCastParams.FilterDescendantsInstances = {workspace.Baseplate, npc, character, }
local rayCastResult = workspace:Raycast(botRootPart.Position, direction, rayCastParams)
if rayCastResult and direction.Magnitude <= 50 and not rayCastResult.Instance.Parent:HasTag("Clone") then
botHumanoid:MoveTo(rootPart.Position)
else
delayTime = 1.2
botHumanoid:MoveTo(botRootPart.Position + Vector3.new(math.random(-5,5),0, math.random(-5,5)))
end
end
end)
coroutine.resume(track)
end
local randomNpc = listOfModels[math.random(0, #listOfModels)]
--print(randomNpc)
end
end)
Maybe change the network owner of your clone to nil? That way the server handles it. Not sure if it’ll fix your stuttering problem though.
As for the part where it chooses only one model, it’s because you’re doing a for loop and choosing the first model found in the for loop instead of actually randomly choosing one.
I recommend you get a table of the NPCs that your clone can target and then just pick a model from it randomly.
Here’s a code for reference:
local npc = script.Parent
local botHumanoid = npc:FindFirstChild("Humanoid")
local botRootPart = npc:FindFirstChild("HumanoidRootPart")
local replicatedStorage = game:GetService("ReplicatedStorage")
replicatedStorage.RemoteEvents.Cloned.Event:Connect(function(character)
local listOfModels = {}
for i , v in ipairs (workspace:GetChildren()) do
if v:IsA("Model") then
table.insert(listOfModels, v)
end
end
local randomNpc = listOfModels[math.random(1, #listOfModels)]
local rig = randomNpc
local humanoid = rig:FindFirstChild("Humanoid")
local rootPart = rig:FindFirstChild("HumanoidRootPart")
local delayTime = 0.1
rootPart:SetNetworkOwner(nil)
local track = coroutine.create(function()
while task.wait(delayTime) do
local direction = (rootPart.Position - botRootPart.Position)
local rayCastParams = RaycastParams.new()
rayCastParams.FilterType = Enum.RaycastFilterType.Exclude
rayCastParams.FilterDescendantsInstances = {workspace.Baseplate, npc, character, }
local rayCastResult = workspace:Raycast(botRootPart.Position, direction, rayCastParams)
if rayCastResult and direction.Magnitude <= 50 and not rayCastResult.Instance.Parent:HasTag("Clone") then
botHumanoid:MoveTo(rootPart.Position)
else
delayTime = 1.2
botHumanoid:MoveTo(botRootPart.Position + Vector3.new(math.random(-5,5),0, math.random(-5,5)))
end
end
end)
coroutine.resume(track)
end
I just made this out of the blue, so check if there’s any syntax errors or anything
I forgot to post that I found the solution to my problem a few hours before your post but I’m still going to put your reply as the solution since your code works. Also thank you for your response also can u quickly explain what SetNetworkOwnership does and its role in this code?
Also im just going to post my code here since im probably going to combine both my revised code and yours to make an improved version of my code.
local npc = script.Parent
local botHumanoid = npc:FindFirstChild("Humanoid")
local botRootPart = npc:FindFirstChild("HumanoidRootPart")
local replicatedStorage = game:GetService("ReplicatedStorage")
local listOfModels = {}
local target = {}
replicatedStorage.RemoteEvents.Cloned.Event:Connect(function(character)
for i, v in pairs(workspace:GetChildren()) do
if v:IsA("Model") and not v:HasTag("Clone") then
print(v)
--table.insert(listOfModels, v)
--print(listOfModels)
local rig = v
local humanoid = rig:FindFirstChild("Humanoid")
local rootPart = rig:FindFirstChild("HumanoidRootPart")
local delayTime = 0.01
local rayCastParams = RaycastParams.new()
rayCastParams.FilterType = Enum.RaycastFilterType.Exclude
rayCastParams.FilterDescendantsInstances = {workspace.Baseplate, npc, character}
local track = coroutine.create(function()
while task.wait(delayTime) do
local direction = (rootPart.Position - botRootPart.Position)
local rayCastResult = workspace:Raycast(botRootPart.Position, direction, rayCastParams)
if rayCastResult and direction.Magnitude <= 25 and rayCastResult.Instance.Parent:FindFirstChild("HumanoidRootPart") then
print(rayCastResult)
delayTime = 0.01
table.insert(target, rayCastResult.Instance.Parent.Name)
if rayCastResult.Instance.Parent.Name ~= target[1] then
return
end
botHumanoid:MoveTo(rayCastResult.Instance.Parent.HumanoidRootPart.Position)
else
delayTime = 1.2
botHumanoid:MoveTo(botRootPart.Position + Vector3.new(math.random(-5,5),0, math.random(-5,5)))
end
end
end)
coroutine.resume(track)
end
local randomNpc = listOfModels[math.random(0, #listOfModels)]
--print(randomNpc)
end
end)
SetNetworkOwner() lets the server handle the calculations of your NPC’s physics and replication so that its more consistent (making it appear less laggy)