16:07:47.929 Workspace.EPICPET.exe.Part.Script:5: attempt to index nil with ‘Character’ - Server
local epicfacething = script.Parent
local area = 50
function findnear(player)
if player.Character.HumanoidRootPart.Position == epicfacething.Position * area then
epicfacething.Epic.Transparency = 0
end
end
local runservice = game:GetService(“RunService”)
runservice.Heartbeat:Connect(function()
findnear()
end)
this is my script
it is changing decals when a player is near
i was gonna add more but i was testing first
local PathFindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
local Enemy = script.Parent
local Players = game:GetService("Players")
local Path = PathFindingService:CreatePath()
local CurrentTarget = nil
local Area = 50
local function FindTarget()
local nearestTarget = nil
local nearestDistance = 500
for _, player in pairs(Players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local distance = (Enemy.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).Magnitude
if distance < nearestDistance then
nearestTarget = player.Character
nearestDistance = distance
end
end
end
return nearestTarget
end
RunService.Heartbeat:Connect(function()
local Target = FindTarget()
if Target then
CurrentTarget = Target
local success, errorMessage = pcall(function()
Path:ComputeAsync(Enemy.HumanoidRootPart.Position, Target.HumanoidRootPart.Position)
end)
if success then
for _, waypoint in pairs(Path:GetWaypoints()) do
Enemy.Humanoid:MoveTo(waypoint.Position)
Enemy.Humanoid.MoveToFinished:Wait()
end
end
end
end)
1st thing first, please format code so its easier to read.
When creating a post with code, click the highlighted button or use Crtl + E to create a code block.
2nd thing, you aren’t sending through any variables when you call findnear(), meaning that player is nil in the function.
This is what you should do:
function findnear(player)
--// Cancel the function if the player's character or root part doesn't exist to avoid error
if not player.Character or not player.Character:FindFirstChild("HumanoidRootPart") then return end
--// You'll want to rewrite this as this will be near impossible to trigger
--// Reminder that Vector3s have decimals, and they are factored when checking this.
if player.Character.HumanoidRootPart.Position == epicfacething.Position * area then
epicfacething.Epic.Transparency = 0
end
--// Although, reading the function, you appear to be wanting to check if the player is within an area of it
--// So really, this is what the check should be if I am correct here:
if (player.Character.HumanoidRootPart.Position - epicfacething.Position).Magnitude <= area then
epicfacething.Epic.Transparency = 0
end
--// Given I am correct, remove the original check from this code. Else, remove this check.
end
game:GetService("RunService").Heartbeat:Connect(function()
for _, player in game.Players:GetPlayers() do --// Loop through all players currently in the server
--// You have to tell the function what the player argument is.
findnear(player)
end
end)
Path-finding is fundamentally separate from determining the nearest player to a specified position. Compared to minimum linear search algorithm, path-finding is assuredly more complex
It cannot help. It is a fundamentally different procedure. The minimum linear search algorithm is embedded within your code, so all you’re doing is potentially solving OP’s problem, then taking it in an unrelated direction