Interesting, it might be your code that’s not working, do you mind providing a snippet of your code? On a side note, does the print statement inside the if statement print something in the console?
From this line, I can say it’s probably the zombie that is causing the error not the player, if the print statement prints, then it’s definitely the zombie.
First off, try printing the zombie variable, like this:
print(zombie)
What does it print?
Try to use :WaitForChild() to wait for the zombie to load, if that doesn’t work, show me how you declare the zombie variable, and where the zombie is located in the game hierarchy.
You’re almost there, for something that happens every time the character loads you, when the player joins, bind your function to CharacterAdded like so:
But then you can store it when it is indeed loaded no ?
I don’t know if i’m missreading it or just not getting your goal, can you explain your exact goal so i can better try and help you ?
Did you try out the things I recommended? It can’t be the players character if the players character is loaded. The code won’t run if it can’t find the players character, it would just error on that line.
Just try out the things I told you, and come back with the output in the console, how you are referencing the zombie, and if you used :WaitForChild() or not.
I’m referencing it from a parameter sent by a for loop that goes through each zombie. I tried print(zombie) and it returns with the name of the zombie. And what i said was that it prints “Character loaded” but only after an error, so it runs the code before the character is loaded. I also tried using the zombie separately and that did not give an error. The only option is it’s the script not waiting for the character to load.
Well then, how does that for loop look like? How are you setting the zombie variable? Did you wrap your code inside the if statement?
I have to many questions currently, can you provide a bigger snippet of your code? Specifically your loop, and the code around the if statement I provided.
while true do
for _, zombie in pairs(zombies:GetChildren()) do
zombie.Humanoid.Died:Connect(function()
removeZombie(zombie)
end)
moveZombie(zombie)
end
task.wait()
end
This goes to here:
local function moveZombie(zombie)
local path = PFS:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = false
})
local target = getClosest(zombie)
which finishes here:
local function getClosest(zombie)
local closest = math.huge
local target = nil
for _, player in pairs(game.Players:GetChildren()) do
local distance = (player.Character.HumanoidRootPart.Position - zombie.HumanoidRootPart.Position).Magnitude
I’ve had a similar issue and the way I got around it was:
while not Character.HumanoidRootPart do task.wait() end
-- code here
local distance = (player.Character.HumanoidRootPart.Position - zombie.HumanoidRootPart.Position).Magnitude
Looking at your code though, it looks like you’d be better off with an if statement as so:
local target = nil
for _, player in pairs(game.Players:GetChildren()) do
if not player.Character.HumanoidRootPart then continue end -- skip the current player if they havent fully loaded in yet
local distance = (player.Character.HumanoidRootPart.Position - zombie.HumanoidRootPart.Position).Magnitude
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid") task.wait(1) --line is stalling
Looks like you understand the appearance takes just a moment after the fact, and that is what the task.wait line is roughly covering, with a bonus reference on the Humanoid, used or not.
--ServerScript in ServerScriptService
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid") task.wait(1) --same stall
--
end)
end)
I’ll do Root too after that line most of the time because I use it most of the time and/or for more of a stall. It’s not 100 percent, but it’s got to be around 99.95 percent. I’ve never had any problems with this after adding these standard startings.