Okay, so it’s always saying that on line 38 that I did an error. I tried it with a character first, but it didn’t work. So I debugged it to only show the player’s name, but it also doesn’t work. What’s wrong with this code?
Note; Sorry for the unorganized coding! Still new to stack overflow AND luau.
local ServerStorage = game:GetService(“ServerStorage”)
local Players = game:GetService(“Players”)
local enemy = script.Parent
local humanoid = enemy:FindFirstChildOfClass(“Humanoid”)
if enemy then
local range = 50
local nearestPlayer = nil
function FetchNearestPlayer()
for i, player in ipairs(Players:GetChildren()) do
if player:IsA("Player") then -- Just setting up necessary properties.
local character = player.Character or player.CharacterAdded:Wait()
local primaryPart = character.PrimaryPart
local pPartName = primaryPart.Name
local balancedPrimaryPart = enemy:FindFirstChild(pPartName)
if balancedPrimaryPart then
local distance = (primaryPart.Position - balancedPrimaryPart.Position).Magnitude
if distance < range then
range = distance
nearestPlayer = player
return nearestPlayer
end
else
enemy.Parent = ServerStorage.Quarantine
warn("Enemy doesn't have same balanced primary part. Moved to Quarantine folder.")
end
end
end
end
while wait(0.01) do
local player = FetchNearestPlayer()
print(player.Name)
end
else
enemy.Parent = ServerStorage.Quarantine
warn(“Enemy doesn’t have a humanoid. Moved to Quarantine folder.”)
return enemy
end
Error: Workspace.Enemies.Enemy.Movement:38: attempt to index nil with ‘Name’ – Server – Movement:38
== What did I do? Idiotic stuff. Teh idiotic stuff is listed below!! :3 ==
I tried to fix it by debugging. It showed that the variable local player = FetchNearestPlayer() fetched a player instead of a character. But, I don’t want the character as of now. All I want is the player, and their name.
So, can someone plz tell me how to fix this?
Help is appreciated 😀
— Newbie Programmer called lag
lag is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.